Change Textbox from Outside Class

K

Kevin Bowser

I am having trouble updating the text in a C# form textbox from outside the
class. Calling function ChangeDisplay from inside the class ControlPanel
works fine, but when I call it from the main program, it doesn't update. I
have declared the variable in another class called global vars, which
several other classes are using.



namespace ControlPanel

{

public class Form1 : System.Windows.Forms.Form

private System.Windows.Forms.TextBox cp_screen;

private System.ComponentModel.Container components = null;

public string keypadinput = "";


public Form1()

{

InitializeComponent();

}

public void ChangeDisplay(string x) // Set display to the passed string

{

cp_screen.Text= x;

}



------------Class Declaration------------------------------

namespace globalvars

{

public class variables

{

public static Form1 Controlpannel = new Form1(); // Start a new instance of
the control pannel class

public static Controller MainController = new Controller(); // Initialize
the main system controller

}

}



Thanks,



Kevin Bowser
 
M

Morten Wennevik

Hi Kevin,

How do you launch your program? (What does Main() look like?)
How do you launch ControlPanel/MainForm and communicate with them?

Happy Coding!
Morten Wennevik [C# MVP]
 
K

Kevin Bowser

I appologize, what I was trying to make the program do is not possible.

The apps are event driven, and you can not update the display without an
event. So I just have a timer that checks to see if the display needs to be
changes every 30 seconds. Quick workaround for the security system
simulator I am trying to put together.


Morten Wennevik said:
Hi Kevin,

How do you launch your program? (What does Main() look like?)
How do you launch ControlPanel/MainForm and communicate with them?

Happy Coding!
Morten Wennevik [C# MVP]


Kevin Bowser said:
I am having trouble updating the text in a C# form textbox from outside
the
class. Calling function ChangeDisplay from inside the class ControlPanel
works fine, but when I call it from the main program, it doesn't update.
I
have declared the variable in another class called global vars, which
several other classes are using.



namespace ControlPanel

{

public class Form1 : System.Windows.Forms.Form

private System.Windows.Forms.TextBox cp_screen;

private System.ComponentModel.Container components = null;

public string keypadinput = "";


public Form1()

{

InitializeComponent();

}

public void ChangeDisplay(string x) // Set display to the passed string

{

cp_screen.Text= x;

}



------------Class Declaration------------------------------

namespace globalvars

{

public class variables

{

public static Form1 Controlpannel = new Form1(); // Start a new instance
of
the control pannel class

public static Controller MainController = new Controller(); // Initialize
the main system controller

}

}



Thanks,



Kevin Bowser
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top