有两个窗口Form1和Form2 ,Form1中有个组件SerialPort,我的 问题是怎样从Form2中引用
Form1的SerialPort?
Form1的SerialPort?
解决方案 »
- 关于StreamReader 的问题
- 摄像头传来的图像挡住菜单了,怎么办?
- (100分求)修改算法
- 100分跪求用“C#设置代理服务器的代码”!!!
- 最高分求VISUAL STUDIO 2005 BETA1下载
- 在线游戏的通信原理是什么
- WinForm交流!
- .NET v1.1中的利用remoting实现“推”模式,引发 安全方面异常
- const是编译期静态解析的常量(因此其表达式必须在编译时就可以求值);readonly则是运行期动态解析的常量;
- 高分求Console程序输入参数解析算法。要求见内。
- System.IO问题(不同的流的作用?)
- 为什么同样的数据库连结语句在windows 窗体可运行,在ASP.NET下去显示用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败。连接字符串属性无效 '
Form2 Dlg = new Form2(this);
Dlg.ShowDialog();
Form2:public Form2(Form1 f1)
{
this.form1=f1;
}
如果你只想改变SerialPort组件的某个属性那么不要将整个组件传给Form2要用一个委托将此属性封装后传给Form2.
这是在Form1中引用自己的组件,我想通过这种方法来在Form2中引用serialPort1:
System.IO.Ports.SerialPort seriport = Application.OpenForms["Form1"] .components.Components[2] as System.IO.Ports.SerialPort;
可是却发现不能引用components,这是怎么回事?
然后form1.SerialPort1用就可以了。
public Form2(Form1 f1)
{
this.form1=f1;
}
AxMSFlexGridLib.AxMSFlexGrid flex = (AxMSFlexGridLib.AxMSFlexGrid)Application.OpenForms["Form1"].Controls[0].Controls["tabPage3"].Controls["axMSFlexGrid1"];
可换成了组件,怎么就不能实现呢?怪哉!
请大家献策,
Form1 from1 = null;
如果不行,可以考虑用委托调用相关方法。
public Form2(Form1 FormMain)
{
this.form1=FormMain;
}//操作
form1.SerialPort .....
public Form2(ref SerialPort InputSerialPort) //注意 ref
{
mySerialPort = InputSerialPort;
}
而且在.NET2.0中Application.OpenForms["Form1"]就可以实现大家所说的。
大家可以试试Application.OpenForms["Form1"].Controls这种引用控件的方法,引用组件
Form1窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public Button Form1Button1 //主要这里提供一个属性,供外部调用,和别的控件一样
{
get
{
return this.button1;
}
} private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
} }
}Form2窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
this.button1.Text=frm.Form1Button1.Text;
}
}
}一个访问另外个窗体的Button.Text
然后在Form1中添加
public Container con = new Container();
con.Add(this.components.Components[2] );
就可以在Form2中引用了
System.IO.Ports.SerialPort seriport = form1.con.Components[0] as System.IO.Ports.SerialPort;
谢谢大家的参与。
另有一个奇怪的问题就是 为什么不能直接引用Container呢?
this.Container.Add(this.components.Components[2]);总是出错。
答应大家的一定作到,明天再加100分,大散分拉。
其实对于控件下面就可以直接访问:
this.button1.Text=((Button)Application.OpenForms["Form1"].Controls["button1"] ).Text;
Form1 form1 = (Form1)this.Owner;
System.IO.Ports.SerialPort seriport;
seriport = form1.con.Components[0] as System.IO.Ports.SerialPort;在Form1中添加
public Container con = new Container();
con.Add(this.components.Components[2] );