C#窗体间传值

来源:互联网 发布:淘宝保证金可以交30吗 编辑:程序博客网 时间:2024/05/17 12:01

public Form2(string i)
  {
   *******;
   this.a=i;
  }
  Form1中
  Form2 f=new Form2(b);
  这样form1的值b就放到form2中了


刚刚发现C#窗体间传值的第二种方法,第一种是通过够造传值,这里就不讲了。下面贴点代码看看:
在button1的单击事件中写入如下代码:
form2 f2 = new form2(this);
f2.ShowDialog();

在窗体form2的窗体类中声明变量,并改写初始化构造函数如下:
form1 form1;
public form2(form1 f1)
{
InitializeComponent();
form1 = f1;
}

在button1的点击事件中写入如下代码:
form1.textBox1.text = this.textBox2.text;
运行即可实现传值。

注:textbox1的Modifiers(控件的可见性级别,即修饰符)应为:public ,internal,protected中之一,不能为private(私有)。

其中showDialog方法有一个重载,参数是模式化显示窗口的拥有窗口,这样定义后,在显示出来的窗口中可以通过窗体的owner属性来访问其拥有者窗口及其相关信息,从而实现传值。

 

主窗体Form1是一个ListBox,单击选中某列时,弹出窗体Form2,Form2中两个控件,一个是TextBox,显示选中的该列的文本,另一个是按钮,点击时将修改后的值回传,且在Form1中修改相应的列的文本,同时Form2关闭。

 

方法一:传值

   最先想到的,Form2构造函数中接收一个string类型参数,即Form1中选中行的文本,将Form2的TextBox控件的Text设置为该string,即完成了Form1向Form2的传值。当Form2的AcceptChange按钮按下,需要修改Form1中ListBox中相应列的值,因此可以考虑同时将Form1中的ListBox控件当参数也传入Form2,所有修改工作都在Form2中完成,根据这个思路,Form2代码如下:
C#代码
publicpartial class Form2 : Form  
    {  
        private string text;  
        private ListBox lb;  
        private int index;  
 
       //构造函数接收三个参数:选中行文本,ListBox控件,选中行索引  
        public Form2(string text,ListBox lb,int index)  
        {  
            this.text = text;  
            this.lb = lb;  
            this.index = index;  
            InitializeComponent();  
            this.textBox1.Text = text;  
        }  
 
        private void btnChange_Click(object sender, EventArgs e)  
        {             
 
            string text = this.textBox1.Text;  
            this.lb.Items.RemoveAt(index);  
            this.lb.Items.Insert(index, text);  
            this.Close();  
        }  
    } 

publicpartial class Form2 : Form
    {
        private string text;
        private ListBox lb;
        private int index;

       //构造函数接收三个参数:选中行文本,ListBox控件,选中行索引
        public Form2(string text,ListBox lb,int index)
        {
            this.text = text;
            this.lb = lb;
            this.index = index;
            InitializeComponent();
            this.textBox1.Text = text;
        }

        private void btnChange_Click(object sender, EventArgs e)
        {          

            string text = this.textBox1.Text;
            this.lb.Items.RemoveAt(index);
            this.lb.Items.Insert(index, text);
            this.Close();
        }
    }
 Form1中new窗体2时这么写:

C#代码
public partial class Form1 :Form  
    {  
        int index = 0;  
        string text = null;  
        public Form1()  
        {  
            InitializeComponent();  
        }  
 
        private void listBox1_SelectedIndexChanged(object sender, EventArgse)  
        {  
            if (this.listBox1.SelectedItem != null)  
            {  
                text = this.listBox1.SelectedItem.ToString();  
                index = this.listBox1.SelectedIndex;  
 
               //构造Form2同时传递参数  
                Form2 form2 = new Form2(text, listBox1, index);  
                form2.ShowDialog();  
            }  
        } 

public partial class Form1 :Form
    {
        int index = 0;
        string text = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgse)
        {
            if (this.listBox1.SelectedItem != null)
            {
                text = this.listBox1.SelectedItem.ToString();
                index = this.listBox1.SelectedIndex;

               //构造Form2同时传递参数
                Form2 form2 = new Form2(text, listBox1, index);
                form2.ShowDialog();
            }
        }
 

 

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

   方法二:继承
   这个方法我试了很多次,继承的确可以做,但是麻烦不说,还不方便,因此个人认为如果为了互相操作数据而使用继承,是不合适的,但既然是个方法,就扔出来看看,实际作用≈0。
   Form2:
C#代码
//声明Form2继承于Form1  
 
public partial classForm2 : Form1  
    {  
         publicint index;  
 
        public ListBox lb;  
        public Form2(string text)  
        {    
 
           //将继承过来的listBox设置为不可见  
 
            this.listBox1.Visible=false;  
            InitializeComponent();  
            this.textBox1.Text = text;  
        }  
        private void btnChange_Click(object sender, EventArgs e)  
        {  
            string text = this.textBox1.Text;  
            this.lb.Items.RemoveAt(index);  
            this.lb.Items.Insert(index,text);           
            this.Close();  
 
        }  
    } 

//声明Form2继承于Form1

public partial classForm2 : Form1
    {
         publicint index;

        public ListBox lb;
        public Form2(string text)
        { 

           //将继承过来的listBox设置为不可见

            this.listBox1.Visible=false;
            InitializeComponent();
            this.textBox1.Text = text;
        }
        private void btnChange_Click(object sender, EventArgs e)
        {
            string text = this.textBox1.Text;
            this.lb.Items.RemoveAt(index);
            this.lb.Items.Insert(index,text);        
            this.Close();

        }
    }
 
Form1:
C#代码
public partial class Form1 :Form  
    {  
        public int index = 0;  
        public string text = null;  
        public Form1()  
        {  
            InitializeComponent();  
        }  
 
        private void listBox1_SelectedIndexChanged(object sender, EventArgse)  
        {  
            if (this.listBox1.SelectedItem != null)  
            {  
                text = this.listBox1.SelectedItem.ToString();  
                index = this.listBox1.SelectedIndex;  
                Form2 form2 = new Form2(text);  
 
               //构造完Form2后,为Form2中各参数赋值  
                form2.lb =this.listBox1;                 
                form2.index = index;  
                form2.Show();  
            }  
        }  
    } 

public partial class Form1 :Form
    {
        public int index = 0;
        public string text = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgse)
        {
            if (this.listBox1.SelectedItem != null)
            {
                text = this.listBox1.SelectedItem.ToString();
                index = this.listBox1.SelectedIndex;
                Form2 form2 = new Form2(text);

               //构造完Form2后,为Form2中各参数赋值
                form2.lb =this.listBox1;              
                form2.index = index;
                form2.Show();
            }
        }
    }
 这里有几点问题需要注意,Form2中各属性需要哪种赋值方法?从Java过度来的都知道,Java继承中在子类中使用关键字super可以访问基类中公有的方法及参数,而C#中super换成了base,那是不是意味着我们可以在Form2中这么为参数赋值呢?
C#代码
this.lb=base.listBox1;  
 
this.index=base.index;  
  

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:
C#代码
//定义一个需要string类型参数的委托  
 
publicdelegate void MyDelegate(string text);  
 
public partial class Form2 :Form1  
    {  
 
       //定义该委托的事件  
        public event MyDelegate MyEvent;  
        public Form2(string text)  
        {   
            InitializeComponent();  
            this.textBox1.Text = text;  
        }  
        private void btnChange_Click(object sender, EventArgs e)  
        {  
 
           //触发事件,并将修改后的文本回传  
            MyEvent(this.textBox1.Text);  
            this.Close();  
        }  
    } 

//定义一个需要string类型参数的委托

publicdelegate void MyDelegate(string text);

public partial class Form2 :Form1
    {

       //定义该委托的事件
        public event MyDelegate MyEvent;
        public Form2(string text)
        {
            InitializeComponent();
            this.textBox1.Text = text;
        }
        private void btnChange_Click(object sender, EventArgs e)
        {

           //触发事件,并将修改后的文本回传
            MyEvent(this.textBox1.Text);
            this.Close();
        }
    }
  Form1:
C#代码
public partial class Form1 :Form  
    {  
        public int index = 0;  
        public string text = null;  
        public Form1()  
        {  
            InitializeComponent();  
        }  
 
        private void listBox1_SelectedIndexChanged(object sender, EventArgse)  
        {  
            if (this.listBox1.SelectedItem != null)  
            {  
                text = this.listBox1.SelectedItem.ToString();  
                index = this.listBox1.SelectedIndex;  
                Form2 form2 = new Form2(text);  
 
               //注册form2_MyEvent方法的MyEvent事件  
                form2.MyEvent += new MyDelegate(form2_MyEvent);  
                form2.Show();  
            }  
        }  
 
       //处理  
 
        void form2_MyEvent(string text)  
        {  
            this.listBox1.Items.RemoveAt(index);  
            this.listBox1.Items.Insert(index, text);  
        }  
    } 

 

 

如果窗体A是B new出来的或则B是A new出来的 可以用以下方法:

在窗体A中:
public delegate void weituo(); //声明一个委托
public event weituo shuaxin;//声明一个事件

并在你那个按钮单击事件中:
shuaxin();
便可以了!

在B窗体中:
A formA = new A();
formA.shuaxin += new A.weituo(fangfa);
formA.Show();

这里面fangfa就是你用来修改label的方法(回调函数)

void fangfa()
{
    //你自己写的刷新代码!
}

注意:formA.shuaxin += new A.weituo(fangfa);这句在输入完+=之后按2下Tab便可以自动生成方法(也就是上面的fangfa())名字是系统自动命名可以改就是了!

 


Form1 下的Button下的的
            Form2 f2 = new Form2();
            f2.GetId(textBox1 .Text );
            this.Hide();
            f2.Show();
Form2 的代码:
       public Form2()
        {
            InitializeComponent();
        }
        public void GetId(string id)
        {
            textBox1.Text = id;
        }

 


2.Session
此方式不仅可以将值传递到下一个页面,还可以将值交叉传递到多个页面,直到把Session变量的值Remove,变量才会消失;缺点是Session变量是存储在服务器端的,消耗服务器端的内存使用,客户端通过ID传回到服务器短检索Session变量值,由于Session存在TimeOut的问题,所以对客户的操作会产生影响!如果服务器端的内存不足时,可能引起Session的崩溃!所以在大的系统里不建议使用这个!
Default1:Session["a"] = b;
  Default2:string C = Convert.ToString(Session["a"]);

原创粉丝点击