Form1 http://i12.pixs.ru/storage/0/5/2/form1jpg_4473358_24731052.jpg

As of the 2nd form, the values ​​entered in TexBox are saved with the name Name (TextBox) in the ComboBox. And how when choosing from ComboBox these values ​​to transfer to connectionString?

class Connection { private string connectionString; protected NpgsqlCommand command; protected NpgsqlConnection connection; protected NpgsqlDataReader reader; public PostgreConnection() { connectionString = @"Server ="+ ServerBox.Text + ";Port="+ PortBox.Text + ";Database="+ DatabaseBox.Text + ";User ID=" + UserIDBox.Text + ";Password=" + PasswordBox.Text; connection = new NpgsqlConnection(connectionString); command = connection.CreateCommand(); } } 
  • 3
    The easiest way to create a class (or structure) with the following properties: Server , Port , Database , ID , Password . In your dialogue form, declare an object of this class and, when you click Save enter information there. From the first form, simply retrieve this property and check for null . - Ev_Hyper
  • @Ev_Hyper Can you make your comment as an answer? - Vadim Ovchinnikov
  • @VadimOvchinnikov thanks for reminding. It is not always possible to write a full answer: ( - Ev_Hyper

1 answer 1

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é

  • Many thanks, helped! - YoFi