0

I am trying to make a value from a .xml file equal to a drop down box inside another form. In vb.net I can just call the form automatically, but inside C# I had to use the code 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

I am reading from a .xml file where the value of BaudRatebx is stored. I am trying to read from it and make the value from the .xml file equal to the 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. There isn't any code for the BaudRatebx being declared as I did that in the designer.

4

2 回答 2

1

在 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()方法 put 之后的其他表单的构造函数中,

BaudRatebx.SelectedIndex = Form1.BaudRatebx;
于 2013-07-29T19:35:12.183 回答
1

根据您的评论,我认为您希望在 ApplicationProperties 中有一个如下所示的吸气剂:

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

在您的 Form1 中,您希望:

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

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

于 2013-07-29T19:40:52.730 回答