Reputation: 29
I have a windows form1 application. This form1 has a click button. When the button is clicked some events handler are created in the click-button method. In the SendText event handler method I a create and show a second Form2 where i want to print a string value met in form1.
The problem is the fact that in my ListBox I see just one string and not all of them (I am consecutively sending strings). Why? Plus there are no additional Form2 forms created and shown when a new event handler arrives.
In Form1 this is the way i am calling form2:
public void Send(string body, string name)
{
Form2 form2 = new Form2(body);
form2.Text = name;
form2.ShowDialog ();
}
public void OnMessage(first val, second vall)
{
send(string val1, string va2);
}
Form2 contains:
public Form2(string s)
{
InitializeComponent();
listBox1.Items.Add(s);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
2 Questions:
How can i create multiple Form2 forms each time the handler is activated (using threads, no?)?
If first val is the same when a new handler arrives how can I activate the form2 that is already on the screen and add a new item in the ListBox1?
Please provide examples, if possible.
Thank you.
Upvotes: 0
Views: 349
Reputation: 10257
if i got this right, you want:
what about this?
Dictionary<string,From2> myForm2s = new Dictionary<string,Form2>();
public void Send(string body, string name)
{
Form2 frm = null;
if(!myForm2s.tryGetValue(name,out frm))
{
frm = new Form2(body);
myForm2s[name] = frm;
frm.Text = name;
frm.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
frm.Show();
}
else
{
frm.listBox1.Items.Add(body); // assuming listBox1 is public
frm.Show();
frm.BringToFront();
}
}
void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
((Form2)sender).Hide();
}
(code not tested...)
you will possibly want to handle the FormClosing event of Form2 ... cancel the event using the eventarg and call this.Hide() ... so a Form2 will still disappear if the user klicks the x but the form is still there, ready to be reshown when the next event adds something to it ...
Upvotes: 0
Reputation: 1710
Instead of using form2.ShowDialog();
, you can use form2.Show();
This works because Show() is modeless, ShowDialog() is modal.
Here is an explanation of the differences.
For the second question, as others have said, you can add a form and the name with which it was initiated to a dictionary. Then, whenever you add another body value, you check the dictionary for a duplicate name value. If one exists, add the body value to the listbox of that particular form.
Upvotes: 1
Reputation: 622
Use a dictionary to store the opened Form2 ordered by name:
private Dictionary<string, Form2> forms = new Dictionary<string, Form2>();
public void Send(string body, string name)
{
if(forms.ContainsKey(name))
{
forms[name].AddNewItem(body);
}
else{
Form2 form2 = new Form2(body);
form2.Text = name;
forms.Add(name, form2);
form2.ShowDialog();
}
}
And Form2 will contain:
public Form2(string s)
{
InitializeComponent();
listBox1.Items.Add(s);
}
public void AddNewItem(string s)
{
listBox1.Items.Add(s);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Hope this helps :)
Upvotes: 0
Reputation: 4129
try to make your form static
static Form2 form2;
public void Send(string body, string name)
{
form2 = new Form2(body);
form2.Text = name;
form2.ShowDialog();
}
Upvotes: 0