简体   繁体   中英

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. 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.

In Form1, add a public static field for the value and set it in your reader.

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,

BaudRatebx.SelectedIndex = Form1.BaudRatebx;

From you comments I think you want to in your ApplicationProperties have a getter like the one that follows:

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

And in your Form1 you would want to:

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

I hope this get you going in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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