update Window Form's list box.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 2 classes
form.cs and cls1.cs

in form.cs
using cls1_ns;
namespace form_ns
{
.....public class formCls.....
public System.Windows.Forms.ListBox LstBox;
....}

in cls1.cs

using form_ns;
namespace cls1_ns
{
class ...etc...
static formCls cForm = new formCls();
.....
public void funcA()
{
....
cForm.LstBox.Add (....)
}
}
}

this doesn't work, would you please tell how to update the value in window
form from another class?
thanks a lot!!!
 
The code that you show should work just fine.
You'll need to provide more info/code

/claes
 
Label won't be updated, thanks!
here is my example :

Form1.cs

using Cls_ns;

namespace sampleFrm
{
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
static Class1 cls = new Class1 ();
...etc....
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
cls.update ();
}
}
}

Class1.cs

------------------------------------------------------------------------------
using System;
using sampleFrm;

namespace Cls_ns
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
static Form1 frm = new Form1 ();
public Class1()
{
//
// TODO: Add constructor logic here
//

}

public void update()
{
frm.label1.Text = "yeah";
}
}
}
 
is that when I instantiate -->
static formCls cForm = new formCls();

there will be a new "Form Class", so whatever I update, i just update on the
copy, but not the real form?
how can i solve the problem?

thanks!!!
 
Well, you failed to mention that you already had an existing form.
Pass a reference of your exisiting form to the constructor of Class1

public class Form1 : System.Windows.Forms.Form
{
private Class1 cls = new Class1(this);
}

public class Class1
{
private Form1 m_frm;
public Class1(Form1 frm)
{
m_frm = frm;
}
public void update()
{
m_frm.label1.Text = "yeah";
}
}

/claes
 
thanks!
but i get the following error

private Class1 cls= new Class1(this);

" Keyword this is not available in the current context"
 
thanks a lot!
i have solved it

Claes Bergefall said:
Well, you failed to mention that you already had an existing form.
Pass a reference of your exisiting form to the constructor of Class1

public class Form1 : System.Windows.Forms.Form
{
private Class1 cls = new Class1(this);
}

public class Class1
{
private Form1 m_frm;
public Class1(Form1 frm)
{
m_frm = frm;
}
public void update()
{
m_frm.label1.Text = "yeah";
}
}

/claes
 
Back
Top