zoukankan      html  css  js  c++  java
  • C# form 传参数的几个方法

    方法一:传值
    最先想到的,Form2构造函数中接收一个string类型参数,即Form1中选中行的文本,将Form2的TextBox控件的Text设置为该string,即完成了Form1向Form2的传值。当Form2的AcceptChange按钮按下,需要修改Form1中ListBox中相应列的值,因此可以考虑同时将Form1中的ListBox控件当参数也传入Form2,所有修改工作都在Form2中完成,根据这个思路,Form2代码如下:

    1. <pre name="code" class="csharp">public partial class Form2 : Form
    2. {
    3. private string text;
    4. private ListBox lb;
    5. private int index;
    6. //构造函数接收三个参数:选中行文本,ListBox控件,选中行索引
    7. public Form2(string text,ListBox lb,int index)
    8. {
    9. this.text = text;
    10. this.lb = lb;
    11. this.index = index;
    12. InitializeComponent();
    13. this.textBox1.Text = text;
    14. }
    15. private void btnChange_Click(object sender, EventArgs e)
    16. {
    17. string text = this.textBox1.Text;
    18. this.lb.Items.RemoveAt(index);
    19. this.lb.Items.Insert(index, text);
    20. this.Close();
    21. }
    22. }
    
    
    Form1中new窗体2时这么写:
    1. public partial class Form1 : Form
    2. {
    3. int index = 0;
    4. string text = null;
    5. public Form1()
    6. {
    7. InitializeComponent();
    8. }
    9. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    10. {
    11. if (this.listBox1.SelectedItem != null)
    12. {
    13. text = this.listBox1.SelectedItem.ToString();
    14. index = this.listBox1.SelectedIndex;
    15. //构造Form2同时传递参数
    16. Form2 form2 = new Form2(text, listBox1, index);
    17. form2.ShowDialog();
    18. }
    19. }
    20. }

     OK,方法一的解决方法就是这样,好处是直观,需要什么就传什么,缺点也是显而易见的,如果窗体1中需要修改的是一百个控件,难道构造的时候还传100个参数进去?况且如果其他窗体仍然需要弹Form2,那Form2就废了,只能供窗体1使用,除非写重载的构造函数,不利于代码的复用,继续看下一个方法。

    方法二:继承
     这个方法我试了很多次,继承的确可以做,但是麻烦不说,还不方便,因此个人认为如果为了互相操作数据而使用继承,是不合适的,但既然是个方法,就扔出来看看,实际作用≈0。
    Form2:

    1. //声明Form2继承于Form1
    2. public partial class Form2 : Form1
    3. {
    4. public int index;
    5. public ListBox lb;
    6. public Form2(string text)
    7. {
    8. //将继承过来的listBox设置为不可见
    9. this.listBox1.Visible=false;
    10. InitializeComponent();
    11. this.textBox1.Text = text;
    12. }
    13. private void btnChange_Click(object sender, EventArgs e)
    14. {
    15. string text = this.textBox1.Text;
    16. this.lb.Items.RemoveAt(index);
    17. this.lb.Items.Insert(index, text);
    18. this.Close();
    19. }
    20. }

    Form1:
    1. public partial class Form1 : Form
    2. {
    3. public int index = 0;
    4. public string text = null;
    5. public Form1()
    6. {
    7. InitializeComponent();
    8. }
    9. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    10. {
    11. if (this.listBox1.SelectedItem != null)
    12. {
    13. text = this.listBox1.SelectedItem.ToString();
    14. index = this.listBox1.SelectedIndex;
    15. Form2 form2 = new Form2(text);
    16. //构造完Form2后,为Form2中各参数赋值
    17. form2.lb = this.listBox1;
    18. form2.index = index;
    19. form2.Show();
    20. }
    21. }
    22. }

     这里有几点问题需要注意,Form2中各属性需要哪种赋值方法?从Java过渡来的都知道,Java继承中在子类中使用关键字super可以访问基类中公有的方法及参数,而C#中super换成了base,那是不是意味着我们可以在Form2中这么为参数赋值呢?
    this.lb=base.listBox1;
    this.index=base.index;
     OK,第二种写法没问题,可以保存index值,但是对ListBox控件,这么赋值就会出问题,通过测试我发现,base.listBox1指向的是子类继承过来的listBox1对象,并不是基类自己的listBox1对象。因此我们猜测,那base.index值是不是也是指向子类的index呢?测试一下发现的确是这样,因此this.index=base.index等于没写,去掉照样可以用,因为index一样被Form2继承过来了,因此我们可以了解到,C#中的窗体继承通过base.控件是无法操作基类控件的。

    方法三:事件回调
     既然C#有事件这个东西,为啥不用呢,而且事件在窗体通信方面,有着更为方便的作用,我们知道事件实际上就是状态的捕获,在最后我会举一个捕获状态的例子,先看数据互相操作的例子。
    Form2:

    1. //定义一个需要string类型参数的委托
    2. public delegate void MyDelegate(string text);
    3. public partial class Form2 : Form1
    4. {
    5. //定义该委托的事件
    6. public event MyDelegate MyEvent;
    7. public Form2(string text)
    8. {
    9. InitializeComponent();
    10. this.textBox1.Text = text;
    11. }
    12. private void btnChange_Click(object sender, EventArgs e)
    13. {
    14. //触发事件,并将修改后的文本回传
    15. MyEvent(this.textBox1.Text);
    16. this.Close();
    17. }
    18. }

    Form1:
    1. public partial class Form1 : Form
    2. {
    3. public int index = 0;
    4. public string text = null;
    5. public Form1()
    6. {
    7. InitializeComponent();
    8. }
    9. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    10. {
    11. if (this.listBox1.SelectedItem != null)
    12. {
    13. text = this.listBox1.SelectedItem.ToString();
    14. index = this.listBox1.SelectedIndex;
    15. Form2 form2 = new Form2(text);
    16. //注册form2_MyEvent方法的MyEvent事件
    17. form2.MyEvent += new MyDelegate(form2_MyEvent);
    18. form2.Show();
    19. }
    20. }
    21. //处理
    22. void form2_MyEvent(string text)
    23. {
    24. this.listBox1.Items.RemoveAt(index);
    25. this.listBox1.Items.Insert(index, text);
    26. }
    27. }

     可以看出,使用事件做是很方便的,并且不需要传递那么多参数,不需要有继承关系,且提高了代码重用,因此在一般的需求下,建议这么使用。最后说一下事件的状态捕获:
    当点Form1中的ShowForm2按钮时,弹出Form2,且在TextBox里写入"Show Form2",当点Form2中Form2Close按钮时,关闭Form2,且将Form1的TextBox文本改为"Close Form2",这就是一个状态的捕获过程,常用在应用程序的状态栏。
    Form1:
    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. private void button1_Click(object sender, EventArgs e)
    8. {
    9. Form2 form2 = new Form2();
    10. this.textBox1.Text = "Show Form2";
    11. form2.MyEvent += new EventHandler(form2_MyEvent);
    12. form2.Show();
    13. }
    14. void form2_MyEvent(object sender, EventArgs e)
    15. {
    16. this.textBox1.Text = "Close Form2";
    17. }
    18. }

    Form2:
    1. public partial class Form2 : Form
    2. {
    3. public event EventHandler MyEvent;
    4. public Form2()
    5. {
    6. InitializeComponent();
    7. }
    8. private void button1_Click(object sender, EventArgs e)
    9. {
    10. MyEvent(sender,e);
    11. this.Close();
    12. }
    13. }

    From : http://www.cnblogs.com/Asa-Zhu/archive/2012/12/06/2805074.html

    转自https://blog.csdn.net/u011981242/article/details/48785639

  • 相关阅读:
    【修正】gooogleman嵌入式联盟部分图标,并增加gooogleman名片(20101205)
    【程序基础】==和=号的区别
    gooogleman嵌入式开发板联盟准备配发嵌入式视频教程
    【终极版】gooogleman嵌入式开发板联盟图标发布以及使用说明
    关于wince驱动和应用学习的N个问题
    【原创】深刻体会wince 驱动中Sleep函数的作用
    网站策划的经典语录
    repeater中用单选按钮
    一个专业制作网站者的自白
    (原创)文件压缩代码
  • 原文地址:https://www.cnblogs.com/owenzh/p/11016937.html
Copyright © 2011-2022 走看看