说明:主窗体Form1上一个Button1,子窗体childForm上有一个ComboBox.
我想Click Button1的时候根据注册表获得本机上的串口个数(串口个数是不一定的),然后传递给子窗体的ComboBox.Items写了如下代码:
主窗体:
private string str;
public string Str
{
get { return str; }
set { this.str = value; }
} private void button1_Click(object sender, EventArgs e)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
string[] comArray = comKey.GetValueNames();
for (int i = 0; i < comArray.Length; i++)
{
this.str = comKey.GetValue(comArray[i]).ToString(); //这里不知道该怎么写.我这种写发只能传最后一个字符串给子窗体.
}
childForm cf = new childForm();
cf.ShowDialog(this);
}
子窗体: //子窗体的Form_Load事件
private void childForm_Load(object sender, EventArgs e)
{
Form1 fm = (Form1)this.Owner;
this.comboBox1.Items.Add(fm.Str);
}这里的运行结果只有一个Item:COM2,实际上有2个(COM1和COM2).
不知道该怎么办.
我想Click Button1的时候根据注册表获得本机上的串口个数(串口个数是不一定的),然后传递给子窗体的ComboBox.Items写了如下代码:
主窗体:
private string str;
public string Str
{
get { return str; }
set { this.str = value; }
} private void button1_Click(object sender, EventArgs e)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
string[] comArray = comKey.GetValueNames();
for (int i = 0; i < comArray.Length; i++)
{
this.str = comKey.GetValue(comArray[i]).ToString(); //这里不知道该怎么写.我这种写发只能传最后一个字符串给子窗体.
}
childForm cf = new childForm();
cf.ShowDialog(this);
}
子窗体: //子窗体的Form_Load事件
private void childForm_Load(object sender, EventArgs e)
{
Form1 fm = (Form1)this.Owner;
this.comboBox1.Items.Add(fm.Str);
}这里的运行结果只有一个Item:COM2,实际上有2个(COM1和COM2).
不知道该怎么办.
{
this.str = comKey.GetValue(comArray[i]).ToString(); //这里不知道该怎么写.我这种写发只能传最后一个字符串给子窗体.
} 关键在这里 你只能将comArray最后一个赋给str为何不弄一个 string[] 的属性呢 然后在子窗体foreach 添加到comboBox1
{
this.str = comKey.GetValue(comArray[i]).ToString(); //我的目的是把所有的字符串传递给子窗体.???
}
childForm cf = new childForm();
//在这里调用childForm 中ADDITEM(str)
cf.ShowDialog(this);
你在childform中写个公开的委托方法delegate void addstr(string str)public void ADDITEM(string str)
{
if(comboBox1.invokerequered)
{
....跨线程委托调用方法
}
else
{
comboBox1.Items.Add(fm.Str);
}
}
public string[] Str
{
get { return str; }
set { this.str = value; }
} private void button1_Click(object sender, EventArgs e)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
str = comKey.GetValueNames();
childForm cf = new childForm();
cf.ShowDialog(this);
}
子窗体: //子窗体的Form_Load事件
private void childForm_Load(object sender, EventArgs e)
{
Form1 fm = (Form1)this.Owner;
foreach(string s in fm.Str){
this.comboBox1.Items.Add(s);
}
}
public string[] Str
{
get { return str; }
set { this.str = value; }
} private void button1_Click(object sender, EventArgs e)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
str = comKey.GetValueNames();
childForm cf = new childForm();
cf.ShowDialog(this);
}
子窗体: //子窗体的Form_Load事件
private void childForm_Load(object sender, EventArgs e)
{
Form1 fm = (Form1)this.Owner;
foreach(string s in fm.Str){
this.comboBox1.Items.Add(s);
}
}
{
变量名=参数名;
}这样数据就传过来了,随便什么类型的数据都可以。
{
this.str = comKey.GetValue(comArray[i]).ToString(); //我的目的是把所有的字符串传递给子窗体.???
}
----->>
for(int i=0;i<comArray.Length;i++)
{
this.str+=comkey.GetValue(comArray[i]).ToString();
}
Form4 a=new Form4();
a.textBox1.Text="123";
就这个意思
这个方法必不可少,不然无法得到数据.10楼的可以得到数据,不过就是全部写在一起了,没分开
{
private Form1 _Form1 = null;//在Form2中添加Form1这个属性 把Form1当成Form2的属性用 控件也成
public Form2(Form1 _Form1)//重载构造 把Form1 或者 控件传过来
{
this._Form1 = _Form1;
InitializeComponent();
this.Show();
}
public Form2()
{
InitializeComponent();
this.Show();
}
这种传值也可以
//public Form1 F1
//{
// set { this._Form1 = value; }
//}
private void button1_Click(object sender, EventArgs e)
{
//用下面的格式就可以在Form2中使用Form1中的值 控件
this._Form1.Visible = true;
//this._Form1.Show();
}
}
{
this.str += comKey.GetValue(comArray[i]).ToString(); //这里不知道该怎么写.我这种写发只能传最后一个字符串给子窗体.
}
父窗体:
private ArrayList comList;
public ArrayList ComList
{
get { return comList; }
set { this.comList = value; }
} private void button1_Click(object sender, EventArgs e)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
string[] comArray = comKey.GetValueNames();
comList = new ArrayList(comArray.Length);
for (int i = 0; i < comArray.Length; i++)
{
this.comList.Add(comKey.GetValue(comArray[i]).ToString());
}
childForm cf = new childForm();
cf.ShowDialog(this);
}
子窗体:
private void childForm_Load(object sender, EventArgs e)
{
Form1 fm = (Form1)this.Owner;
for (int i = 0; i < fm.ComList.Count; i++)
{
this.comboBox1.Items.Add(fm.ComList[i]);
}
}