Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i have 2 forms in C#. i want to select a value by combo box in form no.1 and use this value in form no.2, without any changes! and i don't know what's the value. just know it selected by pressed on combobox
how can i do this?
Posted
Comments
BillWoodruff 27-Oct-13 12:47pm    
Is Form 2 created by Form 1 ?
Sergey Alexandrovich Kryukov 27-Oct-13 15:12pm    
What difference would it make? The reference to one form should be known by another, that's it. And the reference to the "full form" could be bad (excessive: the problem is solved but breaking of isolation compromises safety) — please see my answer.
—SA
BillWoodruff 27-Oct-13 22:33pm    
@Sergey, we'd certainly agree that allowing one Form to have a reference to another Form indicates bad design in almost every case. The programmer's goal should always be to expose the absolute minimum of any Object to another Object: that's a variant of the principle of "separation of concerns."

If Form1 creates Form2, then Form1 can, of course, access any Public Property defined in Form2, and that may be all the OP needs here. Form1 can also call any Public Method in Form2, or set any variable declared as Public in Form2.

I believe you have an intellectual passion for using Interfaces, which I respect, and that is one technique that is useful. But, often, you don't need to rent a ten-ton steamroller to mow the lawn :)
Sergey Alexandrovich Kryukov 28-Oct-13 2:03am    
This discussion is not related to interfaces at all. I mentioned it as the second step. And, in my answer, I also explained that this is just one possibility. Your idea of "fixation" is somewhat artificial.

If your forms collaborate anyhow, then, by definition, one instance gets a reference to something, related to another form. When I say "what difference does it make", I tried to explain that it is not related to what code creates what instance. For example, one method can create both instances and pass required reference to one or both, and so on...

—SA
BillWoodruff 28-Oct-13 4:57am    
I have edited my comment to remove the word "fixation," which, I agree, could be interpreted as having a top-note of "negativity" :)

bill

 
Share this answer
 
Comments
mohsenvasefi 27-Oct-13 12:14pm    
thanks, but it's too difficult to understand :(
BillWoodruff 27-Oct-13 22:35pm    
If you find those articles by OriginalGriff too difficult, then I think that you need to get a good book on .NET and review the fundamentals. Those articles are about as simple and clear an explanation as you are going to find of how to communicate between WinForm Objects in .NET.
Thomas Daniels 28-Oct-13 3:41am    
The articles by OriginalGriff are well explained, they're not difficult at all.
mohsenvasefi 28-Oct-13 2:10am    
listen to me. i want to open a serial port by combo box selection. in combo box, i have some of serial ports as "com1, com2, ..., com8". when i select one of these serial ports in form1, this "comx" opens in form2 by serialport1.open();. just this! but i can't use the selection comx in form2
Quote:
in form1, i wrote Form2 sd = new Form2(); sd.ShowDialog();

i want to open a serial port by combo box selection. in combo box, i have some of serial ports as "com1, com2, ..., com8". when i select one of these serial ports in form1, this "comx" opens in form2 by serialport1.open();

Now that we (finally) have an idea of what you want to do ... we can proceed.

The first task is to analyze whether we are in a scenario that requires notification or access: in this case we know that when Form2 'sd is shown, modally, that it needs to "know" which ComboBox Item was selected in Form1.

We also know that since Form2 is shown modally, the user cannot switch back to Form1 while Form2 is shown, and select another ComboBox Item: so, we don't need to worry about that.

So, knowing we are in an access rather than notification scenario means we are going to add either a Public property, or variable of some Type to Form2.

Technically, we are going to "inject a value" into Form2. You could also say, correctly, that we have "exposed" an integer variable in Form2 to "consumers of the class."

In this case, there's no need for the facilities a Property offers us, so we can, simply, in Form2 'sd, write something like:
C#
public Type variableName;

Let's assume we only need to pass an integer into 'sd when it's opened:
C#
// in Form sd
public int WhichSerialPort;

The choice I'd actually make here would be to use a nullable integer for this variable:
C#
public int? WhichSerialPort = null;

And, I'll explain that below.

Then, we can turn our attention to Form1. The action that triggers the modal showing of Form2 is, evidently, the user selecting a ComboBox Item in Form1.

So, in Form1's ComboBox SelectedIndexChanged EventHandler:
C#
private void cmboWhichSerialPort_SelectedIndexChanged(object sender, EventArgs e)
{
    sd.WhichSerialPort = cmboWhichSerialPort.SelectedIndex;
    sd.ShowModal();
}
You'll note that in the above code we can set the value of 'WhichSerialPort instance of Form2 'sd before it is shown.

Then, in the Form Load EventHandler of Form2 'sd:
C#
private void sd_Load(object sender, EventArgs e)
{
    // throw an error if WhichSerialPort == null
    if (! WhichSerialPort.HasValue) { throw new ArgumentNullException("Serial Port to open is unspecified"); }

    // now you have a valid integer
    switch (WhichSerialPort)
    {
        case 0:
            // code for opening Serial Port #0
            break;
        case 1:
            // code for opening Serial Port #1
            break;
    }
}
Using a nullable integer to hold the data written into Form2 'sd from Form1 is useful as a way to guard against errors. Because an integer is a value Type, it is automatically set to #0 when it is created. By using a nullable integer, and setting it to 'null by default, we have the means to test the variable in the Form_Load EventHandler and make sure it has an integer value, rather than 'null.
 
Share this answer
 
v2
Comments
Thomas Daniels 28-Oct-13 4:53am    
Great answer, +5!
I also edited your answer to improve the formatting.
BillWoodruff 28-Oct-13 5:05am    
Wow, I got the first notification icon on CP I've seen in many moons.

Can you do plastic surgery as well ? My face needs a lot of work :)
The problem is not correctly formulated, but, most likely, this is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
mohsenvasefi 28-Oct-13 2:10am    
listen to me. i want to open a serial port by combo box selection. in combo box, i have some of serial ports as "com1, com2, ..., com8". when i select one of these serial ports in form1, this "comx" opens in form2 by serialport1.open();. just this! but i can't use the selection comx in form2
Sergey Alexandrovich Kryukov 28-Oct-13 3:12am    
Say, one form implements the interface which accept a byte with COM index (1, 2...) and opens the port, or another form implements the interface and accepts the request on which com is selected and returns the index to the caller. What could be a problem?
—SA
we must define in form1
Form2 sd = new Form2(comboBox1.Text); 
sd.ShowDialog();

and in form2,
public Form2(string strport)
strport1 = strport;
serialPort1.PortName = strport1;
serialPort1.Open();

it was so easy!
 
Share this answer
 
Comments
BillWoodruff 28-Oct-13 4:10am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900