将数据从动态控制传递到其他控件 [英] passing data from dynamic control to other control
问题描述
我是一名学生,而我的朋友在传递来自一个控件的数据时遇到困难,这是一个动态生成的控件,动态生成的控件是由此代码
int c = 0 ;
int p = 0 ;
private void button1_Click( object sender,EventArgs e)
{
panel1.VerticalScroll.Value = VerticalScroll.Minimum;
ComboBox txtRun3 = new ComboBox();
txtRun3.Name = txtDynamic + c ++;
txtRun3.Location = new 点( 30 , 18 +( 30 * c))
panel1.Controls.Add(txtRun3);
}
这是将组合框的文本转移到下一个表格的代码4
< pre lang =xml> private void button2_Click(object sender,EventArgs e)
{
Form4 f4 = new Form4();
按钮bs =发送者为按钮;
f4.Combo = panel1.Controls.OfType < ComboBox > 跨度>()第一()的文本。;
f4.Show();
}
他在公共区写下了以下propoerty of form4
public string 组合
{
get
{
return 此跨度> .comboBox1.Text;
}
设置
{
此 .comboBox1 .Text = value ;
}
}
和问题如果两个或多个组合框存在,组合框的文本将转移到form4'的组合框,如
txtRun3.text,txtRun2.text,txtRun3.text
表示逗号将在那里分开,如何做到这一点
和上面的代码只传递一个值到下一个表单,如何用逗号将所有组合框的文本传输到一个cpmbobox分隔
您可以声明一个List来存储ComboBox的值。示例:
public partial class Form1:Form
{
List< combobox> lst = new List< combobox>();
public Form1()
{
InitializeComponent();
}
}
当您动态创建ComboBox时,将其添加到列表中:
void DynamicAddComboBox()
{
for (< span class =code-keyword> int i = 0 ; i < < span class =code-digit> 3 ; i ++)
{
ComboBox c = new ComboBox();
panel1.Controls.Add(c);
lst.Add(c);
}
}
当你想获得组合框的值时,只需遍历列表即可。示例:
void GetComboBoxValues()
{
StringBuilder sb = new StringBuilder();
foreach (ComboBox c in lst)
{
sb。追加(c.Text + \\\\ n);
}
MessageBox.Show(sb.ToString());
}
更新:
您甚至可以在没有列表帮助的情况下直接在面板内循环组合框。你不必将它添加到List中。例如:
<预LANG = C#> <跨度类= 代码关键字>空隙跨度> GetComboBoxValuesFromPanel()
{
StringBuilder的SB = <跨度class =code-keyword> new StringBuilder();
foreach (控制c panel1.Controls)
{
if (c ComboBox)
{
ComboBox cb =(ComboBox)c ;
sb.Append(cb.Text);
}
}
MessageBox.Show(sb.ToString());
}
<击> 传递数据从动态生成cotrol到其它形式的的控制 [ ^ ]
到目前为止这个问题已经更新,这个解决方案不起作用..
-Krunal
hi,i am a student and my friend is facing difficulty in passing the data from one control which is dynamic generated the control which is dynamic generated is by this code
int c = 0;
int p = 0;
private void button1_Click(object sender, EventArgs e)
{
panel1.VerticalScroll.Value = VerticalScroll.Minimum;
ComboBox txtRun3 = new ComboBox();
txtRun3.Name = "txtDynamic" + c++ ;
txtRun3.Location = new Point(30, 18 + (30 * c))
panel1.Controls.Add(txtRun3);
}
and this is the code for transfering the text of the combobox with to the next form4
private void button2_Click(object sender, EventArgs e)
{
Form4 f4 = new Form4();
Button bs = sender as Button;
f4.Combo = panel1.Controls.OfType<ComboBox>().First().Text;
f4.Show();
}
he have written the below propoerty in the public section of the form4
public string Combo
{
get
{
return this.comboBox1.Text;
}
set
{
this.comboBox1.Text = value;
}
}
Quote:and the problem is if two or more combobox wil be present the text of the combobox will be transfer to the form4''s combobox like
txtRun3.text,txtRun2.text,txtRun3.text
means commas will be there for seperation, how to do that
and the upper code transfer only one value to the next form, how to transfer all the combobox''s text to one cpmbobox with comma seperation
You can declare a List to store the value of ComboBox. Example:
public partial class Form1 : Form { List<combobox> lst = new List<combobox>(); public Form1() { InitializeComponent(); } }
By the time you create ComboBox dynamically, you add it into the List:
void DynamicAddComboBox() { for (int i = 0; i < 3; i++) { ComboBox c = new ComboBox(); panel1.Controls.Add(c); lst.Add(c); } }
When you want to get the values of the combobox, just loop through the list. Example:
void GetComboBoxValues() { StringBuilder sb = new StringBuilder(); foreach (ComboBox c in lst) { sb.Append(c.Text + "\r\n"); } MessageBox.Show(sb.ToString()); }
Update:
You can even loop through the combobox direct within the panel without the help of list. You don''t have to add it into List. Example:
void GetComboBoxValuesFromPanel() { StringBuilder sb = new StringBuilder(); foreach (Control c in panel1.Controls) { if (c is ComboBox) { ComboBox cb = (ComboBox)c; sb.Append(cb.Text); } } MessageBox.Show(sb.ToString()); }
passing data from dynamically generated cotrol to the other form''s control[^]
so far this question been updated, this solution won''t work..
-Krunal
这篇关于将数据从动态控制传递到其他控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!