Hi
How do i populate a listbox in window1 from window2.
Have a lisbox named"listbox1" in window1.
Cant just write "listbox1.Items.Add()" to populate the
listbox from window2
Regards Fredric
|
| Surfsune Thursday, August 03, 2006 9:24 AM |
What your suffering from is a lack of a dataclass. Where you have:
Form1
| dataclass (Containing all multiform data)
Form 2
Your application is a prime example of when to use this kind of model.
This way what ever the form is in the application as long as you pass the instance of the dataclass your forms will have access to the same data. This is normally done using a view class but I feel this may be getting ahead of your self.
For now create a new class called switchData, in switch data define the following methods:
/// Inside your name space
class switchData
{
string[] strList = new string[10];
int intPointer = 0;
public void addToStack(string strNew)
{
strList[intPointer] = strNew;
intPointer++;
}
public string[] GetStack()
{
return strList;
}
}
Then when you want to access your stack you can simply,
private void Form1_Load(object sender, EventArgs e)
{
Mydata.addToStack( "Test");
Mydata.addToStack( "Test2");
Mydata.addToStack( "Test3");
}
// On form 2 you will need...
private void Form2_Load(object sender, EventArgs e)
foreach (string strItem in Mydata.GetStack())
{
if (strItem != null)
{
comboBox1.Items.Add(strItem);
}
}
To pass this to another form you will need,
public void setMyData(switchData tmpData)
{
Mydata = tmpData;
}
Thus when you call the 2nd form you will need to:
form2 Myfrm = new form2;
Myfrm.SetMyData(Mydata);
Myfrm.Show();
What I just wrote will seem really confusing but it a VERY simple solution to what your trying to do, you would be much better with stacks to store the data. To read up on what this model is about then have a look at : http://www.c-sharpcorner.com/Code/2003/Feb/MVCDesign.asp
Hope this helps
|
| devstuff Thursday, August 03, 2006 12:08 PM |
Here is an example, how I do resovle this problem:
//first form class class Form1 : System.Windows.Form { ... ... ...
public Form1() { }
public void ClearListBox() { this.listBox1.Items.Clear(); } }
//second form class class Form2 : System.Windows.Form
{ ... ... ... private Form1 fForm1 = null; public Form2( Form1 InstanceOfForm1 ) { fForm1 = InstanceOfForm1; }
//event handler for button private void btnClear_Click(object sender, System.EventArgs e) { if( fForm1 != null ) fFrom1.ClearListBox(); } }
And this example have to work.
|
| Vojko Voga Thursday, August 03, 2006 4:34 PM |
Hi., you should implement one public method in Form1 which add item to listbox1.Items. In my opinion, you should not give public access to your controls in form1. And passing form1 as parameter to form2 constructor is only pass the reference of form1; not a new instance. If you don't have the reference of form1, how can you access it.?
public class Form1 : System.Windows.Form { ... ... ...
public void AddItemToListBox(object item) { this.listbox1.Items.Add(item); } }
public class Form2 : System.Windows.Form { private Form _form1;
public Form2(Form form1) { _form1 = form1; } ... ... ...
private void button1_Click(object sender, System.EventArgs e) { _form1.AddItemToListBox(something); } }
|
| Soe Moe Thursday, August 03, 2006 4:56 PM |
Hi,
Is this a web application or windows application.
If it is windows application. you can remove the listbox1 from window2 and add it to window1 like
window1.controls.Add( listbox1);
before that you can set the listbox1 properties like position, size and etc...
-Thanks
Mahesh |
| Mahesh Kannan Thursday, August 03, 2006 9:32 AM |
Ok
It is a window application.
I think i described it wrong before.
In window2 i calculate som stuff, and a whant to put that stuff into the lisbox in window1.
Regards Fredric
|
| Surfsune Thursday, August 03, 2006 10:15 AM |
You have two options: 1. in window1, change Modifiers of ListBox from Private to Public. So you can access listbox like: instance_of_window1.listbox1.acction_you_want
2. ListBox modifiers are Private and in window1 declare some public method - public void ClearListBox.
public void ClearListBox() { this.listBox1.Items.Clear(); }
You can access this method like: instance_of_window1.ClearListBox();
|
| Vojko Voga Thursday, August 03, 2006 10:29 AM |
I am really stupid because i cant get this to work.
I have changed the Modifiers of the listbox1 to public in the properties.
The class name of window1 i Form1.public partial class Form1 : Form
I tried this i window 2.
private void button2_Click(object sender, EventArgs e)
{
Form1 test = new Form1();
test.listBox1.Items.Add( "Hello");
}
|
| Surfsune Thursday, August 03, 2006 11:27 AM |
What your suffering from is a lack of a dataclass. Where you have:
Form1
| dataclass (Containing all multiform data)
Form 2
Your application is a prime example of when to use this kind of model.
This way what ever the form is in the application as long as you pass the instance of the dataclass your forms will have access to the same data. This is normally done using a view class but I feel this may be getting ahead of your self.
For now create a new class called switchData, in switch data define the following methods:
/// Inside your name space
class switchData
{
string[] strList = new string[10];
int intPointer = 0;
public void addToStack(string strNew)
{
strList[intPointer] = strNew;
intPointer++;
}
public string[] GetStack()
{
return strList;
}
}
Then when you want to access your stack you can simply,
private void Form1_Load(object sender, EventArgs e)
{
Mydata.addToStack( "Test");
Mydata.addToStack( "Test2");
Mydata.addToStack( "Test3");
}
// On form 2 you will need...
private void Form2_Load(object sender, EventArgs e)
foreach (string strItem in Mydata.GetStack())
{
if (strItem != null)
{
comboBox1.Items.Add(strItem);
}
}
To pass this to another form you will need,
public void setMyData(switchData tmpData)
{
Mydata = tmpData;
}
Thus when you call the 2nd form you will need to:
form2 Myfrm = new form2;
Myfrm.SetMyData(Mydata);
Myfrm.Show();
What I just wrote will seem really confusing but it a VERY simple solution to what your trying to do, you would be much better with stacks to store the data. To read up on what this model is about then have a look at : http://www.c-sharpcorner.com/Code/2003/Feb/MVCDesign.asp
Hope this helps
|
| devstuff Thursday, August 03, 2006 12:08 PM |
You are newing an instance of Form1, it is not the same as the one you already have! Declare this in Form1:
public static Form1 GetForm()
{
return this;
}
and then in Form2, call:
Form1.GetForm()._Any_Call_in_Form1;
--
cheers
farshad |
| farshad.A Thursday, August 03, 2006 12:21 PM |
Passing a form instance is a solution but not really a good practice. |
| devstuff Thursday, August 03, 2006 1:23 PM |
Here is an example, how I do resovle this problem:
//first form class class Form1 : System.Windows.Form { ... ... ...
public Form1() { }
public void ClearListBox() { this.listBox1.Items.Clear(); } }
//second form class class Form2 : System.Windows.Form
{ ... ... ... private Form1 fForm1 = null; public Form2( Form1 InstanceOfForm1 ) { fForm1 = InstanceOfForm1; }
//event handler for button private void btnClear_Click(object sender, System.EventArgs e) { if( fForm1 != null ) fFrom1.ClearListBox(); } }
And this example have to work.
|
| Vojko Voga Thursday, August 03, 2006 4:34 PM |
Hi., you should implement one public method in Form1 which add item to listbox1.Items. In my opinion, you should not give public access to your controls in form1. And passing form1 as parameter to form2 constructor is only pass the reference of form1; not a new instance. If you don't have the reference of form1, how can you access it.?
public class Form1 : System.Windows.Form { ... ... ...
public void AddItemToListBox(object item) { this.listbox1.Items.Add(item); } }
public class Form2 : System.Windows.Form { private Form _form1;
public Form2(Form form1) { _form1 = form1; } ... ... ...
private void button1_Click(object sender, System.EventArgs e) { _form1.AddItemToListBox(something); } }
|
| Soe Moe Thursday, August 03, 2006 4:56 PM |
Thanks all !!!!!
Finally i got the program to work.
Best regards Fredric |
| Surfsune Sunday, August 06, 2006 8:00 PM |
So,
Please mark your answer. 
Thanks.
soemoe |
| Soe Moe Sunday, August 06, 2006 11:25 PM |