C# 在另一个窗体上调用事件
我试图使.xml文件中的值等于另一个表单中的下拉框。在vb.net中,我可以自动调用表单,但在C#中,我必须使用代码C# 在另一个窗体上调用事件,c#,xml,winforms,C#,Xml,Winforms,我试图使.xml文件中的值等于另一个表单中的下拉框。在vb.net中,我可以自动调用表单,但在C#中,我必须使用代码ApplicationProperties ApplicationProperties Window=new ApplicationProperties()打开另一个窗体 private void Form1_Load(object sender, EventArgs e) { //Declaring the XmlReader. X
ApplicationProperties ApplicationProperties Window=new ApplicationProperties()
打开另一个窗体
private void Form1_Load(object sender, EventArgs e)
{
//Declaring the XmlReader.
XmlTextReader Reader = new XmlTextReader(@"C:\ForteSenderv2.0\Properties.xml");
while (Reader.Read())
{
switch (Reader.NodeType)
{
//Seeing if the node is and element.
case XmlNodeType.Text:
case XmlNodeType.Element:
if (Reader.Name == "BaudRate")
{
//Reading the node.
Reader.Read();
//Making the Baud Rate equal to the .xml file.
Form.ApplicationProperties.BaudRatebx.SelectedIndex = Reader.Value;
}
}
}
}
为什么我不能使用以下方式调用表单:
ApplicationPropertiesWindow.BaudRatebx.SelectedIndex=Reader.Value
我正在读取一个.xml文件,其中存储了BaudRatebx的值。我试图从中读取并使.xml文件中的值等于波特率bx。唯一的问题是BaudRatebx是另一种形式,我无法调用它,因为我不知道如何调用,当我尝试调用下拉框时,它会说,由于其保护级别,无法访问BaudRatebx。在设计器中声明的波特率bx没有任何代码。在Form1中,为该值添加一个公共静态字段,并在读取器中进行设置
public static int BaudRatebx;
private void Form1_Load(object sender, EventArgs e)
{
//Declaring the XmlReader.
XmlTextReader Reader = new XmlTextReader(@"C:\ForteSenderv2.0\Properties.xml");
while (Reader.Read())
{
switch (Reader.NodeType)
{
//Seeing if the node is and element.
case XmlNodeType.Text:
case XmlNodeType.Element:
if (Reader.Name == "BaudRate")
{
//Reading the node.
Reader.Read();
//Making the Baud Rate equal to the .xml file.
BaudRatebx = int.Parse(Reader.Value);
}
}
}
}
然后在另一个窗体的构造函数中,在initializeProperties()
方法put之后
BaudRatebx.SelectedIndex = Form1.BaudRatebx;
从您的评论中,我认为您希望在ApplicationProperties中有一个如下所示的getter:
public ComboBox GetComboBox
{
get { return this.ComboBox; }
}
在表格1中,您希望:
ApplicationProperties ApplicationPropertiesWindow = new ApplicationProperties();
ApplicationPropertiesWindow .ShowDialog();
ComboBox comboBox = ApplicationPropertiesWindow.GetComboBox;
我希望这能让您朝着正确的方向前进。您遇到了什么错误?我可以理解为什么您会遇到麻烦,但是如果没有所有的代码和更好的解释,我就有点不知所措了。可能是交叉线程问题?我也不确定我是否理解这个问题。
BaudRatebx
是如何声明的?表单必须处于活动状态才能在其中设置任何内容,因为必须首先将属性或表单初始化。它在vb.net中工作的原因是它是vb6的遗留,在vb6中用于访问默认表单对象。那么如何激活表单?我在mainform加载处理程序中有.xml文件。我想启动程序并自动设置波特率Bx的值。