Reputation: 1228
I am new in Window application. I have two forms form1 and form2. From1 has a textbox and form2 has a combobox. Now I want to display combox selected item of form2 into textbox of form1 when i close form2.So I have defied a static class (marketclass) and assigned selected value to statik variable. Beow is my code of form2.
private void cboMarkets_SelectedIndexChanged(Object sender, System.EventArgs e)
{
marketclass.nAlgoproperty = Convert.ToInt32(cmbMarket.SelectedValue);
this.Close();
}
Now Below id is my code of form1.
public Form1()
{
InitializeComponent();
if (marketclass.nAlgoproperty > 0)
{
textbox1.text = marketclass.nAlgoproperty;
}
}
But is not working. So how can I achieve this thing.
Thanks.
Upvotes: 0
Views: 3324
Reputation: 40
I found the short code, and put the value of display!
int widht = Screen.PrimaryScreen.Bounds.Width;//l
int height = Screen.PrimaryScreen.Bounds.Height;//h
textbox1.text ="Widht -> " +widht + " : Height ->" +height
You welcome!!
Upvotes: 0
Reputation: 8938
You are performing the assignment textbox1.Text = marketclass.nAlgoproperty
in Form1
's constructor (i.e. before Form2
assigns a value to marketclass.nAlgoroperty
).
Setting aside the "best" way to do this, you can just perform the assignment after the call in Form1
that shows Form2
(i.e. once Form2
closes).
form2.Show();
textbox1.Text = marketclass.nAlgoproperty;
That should do it. Once you get that working, consider a better way to do it - e.g. the MVC and Observer design patterns.
Upvotes: 0
Reputation: 39122
Show Form2 via ShowDialog(), then retrieve the value:
// ... in Form1 ...
Form2 f2 = new Form2();
f2.ShowDialog(); // code stops here until "f2" is closed
textbox1.text = marketclass.nAlgoproperty;
Upvotes: 3
Reputation: 150108
In general
This type of problem is well-solved using the Model-View-Controller (MVC) pattern.
This looks like WinForms, which does not exactly have excellent support for MVC. However, in spite of that, you can certainly implement the MVC pattern.
For a good getting started article I suggest you have a look at
http://www.codeproject.com/Articles/383153/The-Model-View-Controller-MVC-Pattern-with-Csharp
Specific to this Question
You are attempting to achieve a similar result using static variables. The problem is quite possibly the timing of when you change the backing static variable's value, and when you look for changes.
textbox1.text = marketclass.nAlgoproperty;
is called when Form1 is loaded. It could be that Form1 was indeed loaded before Form2's checkbox handler updated the value in marketclass
.
Even if that is not the case today, that exemplifies the type of issue that you will run into with the pattern you are now using.
The MVC pattern solves this type of timing issue by binding one or more controls to a backing variable such that, if a UI element changes the value, the value is updated and all other bound UI elements are also updated. Conversely, if the value changes (say because it was loaded from the database) all bound UI elements are automatically updated.
Upvotes: 1
Reputation: 198
Have you setted ComboBox's ValueMember property? if it is not, well, you should or if your using the combobox displayed text as value, you have to use SelectedText property instead:
private void cboMarkets_SelectedIndexChanged(Object sender, System.EventArgs e)
{
marketclass.nAlgoproperty = Convert.ToInt32(cmbMarket.SelectedText);
this.Close();
}
Upvotes: 0