0

I'm very new to windows forms so please don't mind the noob question.

I searched, read and watched videos on how to do this but I am stuck. What I'm trying to do is, access the objects (textbox, label, combobox,etc) between forms. I'd really appriciate if someone can put comments and explain it as simple as possible on how it works so I can get a basic understanding on how things work. So here I have

MainForm (includes a label combobox and buttons that calls different child forms)

private void AddCity_Click(object sender, EventArgs e)
    {
        AddAddCity _frmAddCity = new AddCity();
        _frmAddCity.ShowDialog();
        _frmAddCity.Dispose();
    }

Addcity (includes multiline textbox, add button, remove button, textbox to enter city name)

public static List<string> citylist
public static List<string> cityNames 
private void btnAddCity_Click(object sender, EventArgs e)
    {
        citylist.Add(txtCityName.Text);   
        txtCityName.AppendText(txtCityName.Text + "\r\n");
    }

private void btnOK_Click(object sender, EventArgs e)
    {
        mainform.CityCombobox.Items.AddRange(computerlist);//this gives an error
    }
private void AddCity_FormClosing(object sender, FormClosingEventArgs e)
    {
        cityNames.AddRange(citylist);
    }
private void AddComputers_Load(object sender, EventArgs e)
    {
        foreach(string City in cityNames)
        {
            txtCityName.AppendText(City + "\r\n");
        }
    }

I created two lists and add the first one to second one so when I close the form I can keep those city names. When re-call the addcity form, I want all the city names in the textbox so I can remove the ones I don't want. I get errors trying to build it since it doesn't work.

Now going back to the mainform, if I select a City from the dropdown box, I want that to be used as a string on all the other child forms. Like frmState and so forth.

Is there a way to create a separate class to clean things up and maybe use that to automate more stuff and use that class in any form I want?

Thank you everyone in advance for their time.

Besiktas
  • 331
  • 1
  • 6
  • 23
  • You will find some useful options in the [linked post](http://stackoverflow.com/a/38769212/3110834). The most simple option is making those controls public. But as a better option you can create some public properties or methods exposing the operation which you want to do against controls and use those public members. – Reza Aghaei Feb 27 '17 at 21:38
  • Thanks for the link. I was more looking for an explanation on how it works. – Besiktas Feb 27 '17 at 21:46
  • The linked post contains explanations and examples for different case. The main point is 'forms are classes too'. They can communicate like other classes. Also consider separating data from your controls. Your controls should just show data. For example, instead of adding an item directly to the `ComboBox`, add the item to data-source and use the `ComboBox` to show data. – Reza Aghaei Feb 27 '17 at 21:51

0 Answers0