简体   繁体   English

在另一个表单上调用事件

[英]Calling event on another form

I am trying to make a value from a .xml file equal to a drop down box inside another form. 我试图从.xml文件中创建一个等于另一个表单内的下拉框的值。 In vb.net I can just call the form automatically, but inside C# I had to use the code ApplicationProperties ApplicationPropertiesWindow = new ApplicationProperties(); 在vb.net中我可以自动调用表单,但在C#中我必须使用代码ApplicationProperties ApplicationPropertiesWindow = new ApplicationProperties(); to open the other form. 打开另一个表格。

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

Why can I not call the form using: ApplicationPropertiesWindow.BaudRatebx.SelectedIndex = Reader.Value 为什么我不能使用以下方法调用表单: ApplicationPropertiesWindow.BaudRatebx.SelectedIndex = Reader.Value

I am reading from a .xml file where the value of BaudRatebx is stored. 我正在读取存储BaudRatebx值的.xml文件。 I am trying to read from it and make the value from the .xml file equal to the BaudRatebx. 我试图从它读取并使.xml文件中的值等于BaudRatebx。 The only problem is that BaudRatebx is on another form and I cannot call it because I do not know how, when I try to call the dropdown box it says BaudRatebx is inaccessible due to its protection level . 唯一的问题是BaudRatebx处于另一种形式,我无法调用它,因为我不知道如何,当我尝试调用下拉框时,它说BaudRatebx由于其保护级别而无法访问 There isn't any code for the BaudRatebx being declared as I did that in the designer. BaudRatebx没有任何代码被声明,就像我在设计器中那样。

In Form1, add a public static field for the value and set it in your reader. 在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);
                        }
                 }
             }
 }

Then in your other form's constructor after the InitalizeProperties() method put, 然后在InitalizeProperties()方法放入之后的另一个窗体的构造函数中,

BaudRatebx.SelectedIndex = Form1.BaudRatebx;

From you comments I think you want to in your ApplicationProperties have a getter like the one that follows: 从你的评论我认为你想在你的ApplicationProperties中有一个像下面那样的getter:

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

And in your Form1 you would want to: 在您的Form1中,您可能希望:

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

I hope this get you going in the right direction. 我希望这能让你朝着正确的方向前进。

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

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