http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

楼主: johnson.sia

[Winform] 窗体继承的问题

[复制链接]
 楼主| 发表于 2013-6-14 16:11:35 | 显示全部楼层
站长苏飞 发表于 2013-6-14 16:02
你注册两次是肯定会执行两次的呀,这没办法,在子窗体中修改一下绑定的事件名称就行了

怎么没法动继承的方法,我把btnSubmit事件(和父类同名)删了,定义了一个btnAddSubmit事件,可是报错,执行的时候调用的父类中的方法,怎么这样呢


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-6-14 16:18:03 | 显示全部楼层
johnson.sia 发表于 2013-6-14 16:11
怎么没法动继承的方法,我把btnSubmit事件(和父类同名)删了,定义了一个btnAddSubmit事件,可是报错, ...

在子窗体中肯定没有办法修改主窗体的事件,只能重写,这个你网上找一下方法很多,
或者你在主窗体中不要处理事件,到子窗体再处理。就行了。
 楼主| 发表于 2013-6-14 16:40:03 | 显示全部楼层
站长苏飞 发表于 2013-6-14 16:18
在子窗体中肯定没有办法修改主窗体的事件,只能重写,这个你网上找一下方法很多,
或者你在主窗体中不要 ...

我使用的就是重写啊,父窗体事件定义成virtual,子窗体使用override重写事件
发表于 2013-6-14 16:51:07 | 显示全部楼层
你贴一下代码看看
 楼主| 发表于 2013-6-14 16:55:26 | 显示全部楼层

父窗体:
           using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
namespace WinForms
{
    public partial class UpdateTruck : Form
    {
        public UpdateTruck()
        {
            InitializeComponent();
        }
        public List<TbTruck> list { get; set; }
        /// <summary>
        /// 定义属性保存选中的卡车信息
        /// </summary>
        public TbTruck tbTruck { get; set; }
        TbTruck t = new TbTruck();
        /// <summary>
        /// 更新卡车信息
        /// </summary>
        public void Update_TruckInfo()
        {
            t.Id = tbTruck.Id;
            t.Chpahaaa = this.txt_chpahaaa.Text.Trim();
            t.Chlisuyo = this.txt_chlisuyo.Text.Trim();
            t.Jifeduwe = this.txt_jifeauwe.Text.Trim();
            t.Chcuaaaa = this.txt_chcuaaaa.Text.Trim();
            t.Duweaaaa = this.txt_duweaaaa.Text.Trim();
            t.CrtUser = this.txt_crt_user.Text.Trim();
            //t.CrtDate = Convert.ToDateTime(this.txt_crt_date.Text.Trim());
            t.CrtDate = dtp_truck.Value;
            int count = TruckManage.Update(t);

            if (count > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private  void btnSubmit_Click(object sender, EventArgs e)
        {
            Update_TruckInfo();
        }

        private  void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private  void StruckDetail_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                try
                {
                    TbTruck t = TruckManage.SelectOnTruckId(Convert.ToInt32(tbTruck.Id));
                    this.txt_chpahaaa.Text = t.Chpahaaa;
                    this.txt_chlisuyo.Text = t.Chlisuyo;
                    this.txt_jifeauwe.Text = t.Jifeduwe;
                    this.txt_chcuaaaa.Text = t.Chcuaaaa;
                    this.txt_duweaaaa.Text = t.Duweaaaa;
                    this.txt_crt_user.Text = t.CrtUser;
                    //this.txt_crt_date.Text = t.CrtDate.ToString();
                    dtp_truck.Value = t.CrtDate;
                    //UpdateTruck.
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}



子窗体:
           using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
namespace WinForms
{
    public partial class AddTruck : UpdateTruck
    {
        public AddTruck()
        {
            InitializeComponent();
        }
        TbTruck t = new TbTruck();
        /// <summary>
        /// 添加卡车信息
        /// </summary>
        private void Add_TruckInfo()
        {
            t.Chpahaaa = this.txt_chpahaaa.Text.Trim();
            t.Chlisuyo = this.txt_chlisuyo.Text.Trim();
            t.Jifeduwe = this.txt_jifeauwe.Text.Trim();
            t.Chcuaaaa = this.txt_chcuaaaa.Text.Trim();
            t.Duweaaaa = this.txt_duweaaaa.Text.Trim();
            t.CrtUser = this.txt_crt_user.Text.Trim();
            //t.CrtDate = Convert.ToDateTime(this.txt_crt_date.Text.Trim());
            t.CrtDate = Convert.ToDateTime(dtp_truck.Value);
            int count = TruckManage.AddTruck(t);

            if (count > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //protected override void btnSubmit_Click(object sender, EventArgs e)
        //{
        //    Add_TruckInfo();
        //}
        protected  void btnAddSubmit_Click(object sender, EventArgs e)
        {
            Add_TruckInfo();
        }

        protected  void btnAddCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //protected override void btnCancel_Click(object sender, EventArgs e)
        //{
        //    this.Close();
        //}

        private void AddTruck_Load(object sender, EventArgs e)
        {

        }

    }
}
做了一点改动
发表于 2013-6-14 16:59:43 | 显示全部楼层
johnson.sia 发表于 2013-6-14 16:55
父窗体:
           using System;
using System.Collections.Generic;

没见你在主窗体中设置virtual啊,你看我的代码
父窗体
[code=csharp]  public virtual void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("fd");
        }[/code]
子窗体
[code=csharp] public override void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("123");
        }[/code]
注意这个方法不是双击按钮出来的,而是直接写的,不能双击生成
这样单击按钮1时直接输出的是123而不是输出fd再输出123,你再看看你的代码吧。
 楼主| 发表于 2013-6-14 17:10:39 | 显示全部楼层
站长苏飞 发表于 2013-6-14 16:59
没见你在主窗体中设置virtual啊,你看我的代码
父窗体

刚才的是我改过了的:
父窗体:
          using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
namespace WinForms
{
    public partial class UpdateTruck : Form
    {
        public UpdateTruck()
        {
            InitializeComponent();
        }
        public List<TbTruck> list { get; set; }
        /// <summary>
        /// 定义属性保存选中的卡车信息
        /// </summary>
        public TbTruck tbTruck { get; set; }
        TbTruck t = new TbTruck();
        /// <summary>
        /// 更新卡车信息
        /// </summary>
        public void Update_TruckInfo()
        {
            t.Id = tbTruck.Id;
            t.Chpahaaa = this.txt_chpahaaa.Text.Trim();
            t.Chlisuyo = this.txt_chlisuyo.Text.Trim();
            t.Jifeduwe = this.txt_jifeauwe.Text.Trim();
            t.Chcuaaaa = this.txt_chcuaaaa.Text.Trim();
            t.Duweaaaa = this.txt_duweaaaa.Text.Trim();
            t.CrtUser = this.txt_crt_user.Text.Trim();
            //t.CrtDate = Convert.ToDateTime(this.txt_crt_date.Text.Trim());
            t.CrtDate = dtp_truck.Value;
            int count = TruckManage.Update(t);

            if (count > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        public virtual  void btnSubmit_Click(object sender, EventArgs e)
        {
            Update_TruckInfo();
        }

        public virtual  void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private  void StruckDetail_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                try
                {
                    TbTruck t = TruckManage.SelectOnTruckId(Convert.ToInt32(tbTruck.Id));
                    this.txt_chpahaaa.Text = t.Chpahaaa;
                    this.txt_chlisuyo.Text = t.Chlisuyo;
                    this.txt_jifeauwe.Text = t.Jifeduwe;
                    this.txt_chcuaaaa.Text = t.Chcuaaaa;
                    this.txt_duweaaaa.Text = t.Duweaaaa;
                    this.txt_crt_user.Text = t.CrtUser;
                    //this.txt_crt_date.Text = t.CrtDate.ToString();
                    dtp_truck.Value = t.CrtDate;
                    //UpdateTruck.
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}


子窗体:
           using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
namespace WinForms
{
    public partial class AddTruck : UpdateTruck
    {
        public AddTruck()
        {
            InitializeComponent();
        }
        TbTruck t = new TbTruck();
        /// <summary>
        /// 添加卡车信息
        /// </summary>
        private void Add_TruckInfo()
        {
            t.Chpahaaa = this.txt_chpahaaa.Text.Trim();
            t.Chlisuyo = this.txt_chlisuyo.Text.Trim();
            t.Jifeduwe = this.txt_jifeauwe.Text.Trim();
            t.Chcuaaaa = this.txt_chcuaaaa.Text.Trim();
            t.Duweaaaa = this.txt_duweaaaa.Text.Trim();
            t.CrtUser = this.txt_crt_user.Text.Trim();
            //t.CrtDate = Convert.ToDateTime(this.txt_crt_date.Text.Trim());
            t.CrtDate = Convert.ToDateTime(dtp_truck.Value);
            int count = TruckManage.AddTruck(t);

            if (count > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public override void btnSubmit_Click(object sender, EventArgs e)
        {
            Add_TruckInfo();
        }

        public override void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddTruck_Load(object sender, EventArgs e)
        {

        }

    }
}





发表于 2013-6-14 17:34:16 | 显示全部楼层
johnson.sia 发表于 2013-6-14 17:10
刚才的是我改过了的:
父窗体:
          using System;

你看下我的代码吧,我的测试是正常的,就没几行,你的具体是什么情况不了解,或者你再开个项目能个小例子试试我的代码,说不定不是事件本身的问题,也许是你的方法本身实现的事。
你单独写个试试,或者把你的方法修改成我的直接一个提示信息那样的。别太复杂,先看看是那里的问题
 楼主| 发表于 2013-6-17 14:16:59 | 显示全部楼层
站长苏飞 发表于 2013-6-14 17:34
你看下我的代码吧,我的测试是正常的,就没几行,你的具体是什么情况不了解,或者你再开个项目能个小例子 ...

你的确实没有问题,但我的单击新建弹出窗体有一个错误是“未将对象的引用指向对象的实例”,觉得很纳闷,调试发现子窗体会执行父窗体的 “TbTruck t = new TbTruck();” 实力化代码,子类中也有这段,怎么回事啊
发表于 2013-6-17 14:20:46 | 显示全部楼层
johnson.sia 发表于 2013-6-17 14:16
你的确实没有问题,但我的单击新建弹出窗体有一个错误是“未将对象的引用指向对象的实例”,觉得很纳闷, ...

父类中所有的方法在子类中都会被继承的。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-17 16:05

© 2014-2021

快速回复 返回顶部 返回列表