将数据从动态生成的cotrol传递到另一个窗体的控件 [英] passing data from dynamically generated cotrol to the other form's control

查看:57
本文介绍了将数据从动态生成的cotrol传递到另一个窗体的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI
i am makinng一个Windows窗体项目,并且难以将dnamically生成的控件值传递给另一个窗体的正常控制值。

我的代码就像

HI i am makinng a windows form project and facing difficulty in passing the dnamically generated control value to the other form''s normal control value.
my code is like

int c = 0;
       int p = 0;
       private void button1_Click(object sender, EventArgs e)
       {
           panel1.VerticalScroll.Value = VerticalScroll.Minimum;
          // panel1.HorizontalScroll.Value = HorizontalScroll.Minimum;

           ComboBox txtRun3 = new ComboBox();
           txtRun3.Name = "txtDynamic" + c++ ;
           txtRun3.Location = new Point(30, 18 + (30 * c))
           panel1.Controls.Add(txtRun3);
          txtRun3.Focus();
       }

       private void button2_Click(object sender, EventArgs e)
       {
           Form4 f4 = new Form4();
           ComboBox cb1 = sender as ComboBox;
           Button bs = cb1.Tag as Button;
           f4.comboBox1.Text = bs.Text;
           f4.Show();
       }



i收到错误

对象引用未设置为对象的实例。


i am getting error as
"Object reference not set to an instance of an object."

推荐答案

在.NET 1.x中使用C#和VB.NET示例传递表单之间的值 [ ^ ]

http://stackoverflow.com/questions / 1559770 / send-values-from-one-form-to-another-form-in-c-sharp-winforms-application [ ^ ]



可以通过传递参数化来完成构造函数在第一种形式中定义一个方法并在第二个中引用该表单并调用方法 ...
Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^]
http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application[^]

It can be done by either passing parameterized constructor or define one method in 1st form and take reference of that form in 2nd and call the method...


你还记得我的意思吗?今天早上告诉你有关控制器的信息吗?

如果一个控件是一个ComboBox,那么它也不能成为一个标签!所以当你执行

Do you remember what I told you this morning, about casting controls?
"If a Control is a ComboBox, then it can''t also be a Label! So when you execute
ComboBox cb = sender as ComboBox;
Label lb = sender as Label;

您可以保证两个变量中的一个为空 - 所以当您尝试同时使用这两个变量时,您肯定会收到错误。



这也适用于Buttons和ComboBoxes:

You are guaranteed that one of the two variables will be null - so when you try to use both, you are certain to get the error."

That applies to Buttons and ComboBoxes as well:

Button btnh = sender as Button;
ComboBox cb1 = sender as ComboBox;





顺便说一下 - 不要传输这样的数据:



Any by the way - don''t transfer data like this:

Form4 f4 = new Form4();
f4.comboBox1.Text = bs.Text;

再次将Form4中的ComboBox设为私有,并使用财产代替:

在Form4中:

Make the ComboBox in Form4 private again, and use a property instead:
In Form4:

public string WhateverTheComboBoxIsMeantToDoUseYourOwnNameHere
   {
   get { return comboBox1.Text; }
   set { comboBox1.Text = value; }
   }

这样,您可以稍后更改Form4的设计,而不会影响它之外的表单。 (记住OOPs基金会!)



标签改为ComboBoxes - OriginalGriff [/ edit]

That way, you can change the design of Form4 later without it affecting forms outside it. (Remember the OOPs fundimentals!)

[edit]"Labels" changed to "ComboBoxes" - OriginalGriff[/edit]


这篇关于将数据从动态生成的cotrol传递到另一个窗体的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆