In those cases when you need to return some information from the second (dialog) form, the easiest way is to start by creating an auxiliary class (or structure). It should fully describe the data you want to use in the first form.
Specifically, in your case, the class may be something like this:
public class ConnectionData { public string Server { get; set; } public int Port { get; set; } public string Database { get; set; } public int ID { get; set; } public string Password { get; set; } public override string ToString() { return string.Format ("Server = {0};Port={1};Database={2};User ID={3};Password={4}", Server, Port, Database, ID, Password); } }
About the second part of the question. You can store all of your strings to connect to in the list (read more about BindingList in MSDN ):
BindingList<ConnectionData> list = new BindingList<ConnectionData>();
later in the constructor, set it as a data source for your ComboBox :
//тут заполнение списка предварительными данными comboBox1.DataSource = list;
and each when adding new data they will automatically be displayed in your drop-down list. The specific display of data in the ComboBox by default depends on how you override the ToString method in your class.
Now directly on the transfer of data from the second form.
In the dialog form, add the property:
public ConnectionData Connection_data { get; set; }
and when processing a click on the Save button, fill it with information. In the first form, you will only have to compare Connection_data with null :
//... Form2 form2 = new Form2(); form2.ShowDialog(); if (form2.Connection_data != null) list.Add(form2.Connection_data);
In the new version of C # (6.0), you can use the null conditional operator and string interpolation. More information about what has been added can be read, for example, in Habré
Server,Port,Database,ID,Password. In your dialogue form, declare an object of this class and, when you clickSaveenter information there. From the first form, simply retrieve this property and check fornull. - Ev_Hyper