繁体   English   中英

在另一个表单上调用事件

[英]Calling event on another form

我试图从.xml文件中创建一个等于另一个表单内的下拉框的值。 在vb.net中我可以自动调用表单,但在C#中我必须使用代码ApplicationProperties ApplicationPropertiesWindow = 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

我正在读取存储BaudRatebx值的.xml文件。 我试图从它读取并使.xml文件中的值等于BaudRatebx。 唯一的问题是BaudRatebx处于另一种形式,我无法调用它,因为我不知道如何,当我尝试调用下拉框时,它说BaudRatebx由于其保护级别而无法访问 BaudRatebx没有任何代码被声明,就像我在设计器中那样。

在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);
                        }
                 }
             }
 }

然后在InitalizeProperties()方法放入之后的另一个窗体的构造函数中,

BaudRatebx.SelectedIndex = Form1.BaudRatebx;

从你的评论我认为你想在你的ApplicationProperties中有一个像下面那样的getter:

public ComboBox GetComboBox
{
      get { return this.ComboBox; }
}

在您的Form1中,您可能希望:

ApplicationProperties ApplicationPropertiesWindow = new ApplicationProperties();
ApplicationPropertiesWindow .ShowDialog();
ComboBox comboBox = ApplicationPropertiesWindow.GetComboBox;

我希望这能让你朝着正确的方向前进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM