Access Form Component from Another Class

Access form component from another class

Use a delegate for setting your label

public class MyClass {
Action<String> labelSetter;

public MyClass(Action<String> labelSetter) {
this.labelSetter = labelSetter;
}

public void MyMethod() {
labelSetter("outside");
}
}

.

public void buttonOut_Click(object sender, EventArgs e) {
var outside = new MyClass(UpdateLabel);
outside.MyMethod();
}

Accessing Form's Controls from another class

EDIT: Lot of edit.

public partial class Form1 : Form
{
// Static form. Null if no form created yet.
private static Form1 form = null;

private delegate void EnableDelegate(bool enable);

public Form1()
{
InitializeComponent();
form = this;
}

// Static method, call the non-static version if the form exist.
public static void EnableStaticTextBox(bool enable)
{
if (form != null)
form.EnableTextBox(enable);
}

private void EnableTextBox(bool enable)
{
// If this returns true, it means it was called from an external thread.
if (InvokeRequired)
{
// Create a delegate of this method and let the form run it.
this.Invoke(new EnableDelegate(EnableTextBox), new object[] { enable });
return; // Important
}

// Set textBox
textBox1.Enabled = enable;
}
}

how to access winform components from another class?

So it looks like you're passing the whole form into your second class anyway, So I'd do what LightStriker suggested. Make a public accessor for all of your items and then set it in your other class.

public partial class Form1 : Form
{
private myCounter _counterClass;
public Form1()
{
InitializeComponent();
}

public Label MyLabel1
{
get {return mylabel1;}
}

public Label MyLabel2
{
get {return mylabel2;}
}

private void Form1_Load(object sender, EventArgs e)
{
_counterClass = new myCounter(this);
}

protected void ButtonClick(object sender, EventArgs e)
{
_counterClass.changeColor();
}
}

Then in your second class you have access to your Label.

class myCounter
{
private readonly Form1 Board;
public myCounter(Form1 Board)
{
this.Board = Board;
}

public int turn = 0;

public void changeColor()
{

if (turn == 0)
{
turn = 1;
Board.MyLabel1.BackColor = Color.Red;
Board.MyLabel2.BackColor = Color.White;
}
else
{
turn = 0;
Board.MyLabel2.BackColor = Color.Yellow;
Board.MyLabel1.BackColor = Color.White;
}
}
}

Keep in mind this is code I have written in a wiki markup editor and is untested. This SHOULD work for you though.

Accessing MainForm from another class file

What is happening is that the form you are showing is not the same as the one inside the class Test.

To make things work you should pass the form to the class Test in this way:

public class Test
{
private MainForm form;
public Test(MainForm f)
{
this.form=f;
}
public void changer(){
form.Change = "qqqqq!";
}
}}

and in your main form you do this:

public partial class MainForm : Form
{
public MainForm()
{InitializeComponent();}

public string Change
{
get{ return label.Text;}
set{ label.Text = value;}
}

void ButtonClick(object sender, EventArgs e)
{
Test a = new Test(this);
a.changer();

}
}}

How to access to Form components from another class

The set_text function needs to use a specific Form object, like this

    void set_text()
{
mainForm->MessageLog->Text = "some text";
}

To make this work, mainForm needs to be a member variable of COpenGL of type System::Windows::Forms::Form^, and this member should be initialized in the constructor (resulting in a constructor call like this):

    OpenGL = gcnew COpenGL(this, splitContainer1->Panel2, splitContainer1->Panel2->Width, splitContainer1->Panel2->Height);

Alternatively, instead of passing the whole Form object to the COpenGL object, just pass a reference to the message log and store it in a member variable. Something along the lines of

public ref class COpenGL : public System::Windows::Forms::NativeWindow
{

System::Windows::Forms::TextBox^ messageLog;

public:

COpenGL( System::Windows::Forms::TextBox^ messageLog,
System::Windows::Forms::SplitterPanel^ parentForm,
GLsizei iWidth, GLsizei iHeight)
{
this->messageLog=messageLog;
// ...
}

void set_text()
{
messageLog->Text = "some text";
}
}

C# Access form objects from another class

Fortunately I had a moment of thinking outside the box and tried passing each object into its child object, conversely to my original screw up of building the hierarchy upside down from intended with subclasses. Off the wall or intuitive, you be the judge:

class Program
{
public static void Main()
{
Console.WriteLine("Main");
Console.ReadLine();
Form oForm = new Form();
}
}

public class Form
{
public Parent oParent;
public Form()
{
Console.WriteLine("Form");
Console.ReadLine();
Parent oParent = new Parent(this);
}

public void TargetFunction()
{
Console.WriteLine("Target Hit");
Console.ReadLine();
}
}

public class Parent
{
Form oForm;
Child oChild;
public int hi = 1;
public Parent(Form Form)
{
oForm = Form;
Console.WriteLine("Parent");
Console.ReadLine();
oChild = new Child(oForm, this);
}
}

public class Child
{
Form oForm;
Parent oParent;
public Child(Form Form, Parent Parent)
{
Console.WriteLine("Child");
Console.ReadLine();
oForm = Form;
oParent = Parent;

oForm.TargetFunction();
}
}

Access GUI components from another class

First respect encapsulation rules. Make your fields private. Next you want to have getters for the fields you need to access.

public class GUI {
private JTextField field = new JTextField();

public GUI() {
// pass this instance of GUI to other class
SomeListener listener = new SomeListener(GUI.this);
}

public JTextField getTextField() {
return field;
}
}

Then you'll want to pass your GUI to whatever class needs to access the text field. Say an ActionListener class. Use constructor injection (or "pass reference") for the passing of the GUI class. When you do this, the GUI being referenced in the SomeListener is the same one, and you don't ever create a new one (which will not reference the same instance you need).

public class SomeListener implements ActionListener {
private GUI gui;
private JTextField field;

public SomeListener(GUI gui) {
this.gui = gui;
this.field = gui.getTextField();
}
}

Though the above may work, it may be unnecesary. First think about what exactly it is you want to do with the text field. If some some action that can be performed in the GUI class, but you just need to access something in the class to perform it, you could just implement an interface with a method that needs to perform something. Something like this

public interface Performable {
public void someMethod();
}

public class GUI implements Performable {
private JTextField field = ..

public GUI() {
SomeListener listener = new SomeListener(GUI.this);
}

@Override
public void someMethod() {
field.setText("Hello");
}
}

public class SomeListener implements ActionListener {
private Performable perf;

public SomeListener(Performable perf) {
this.perf = perf;
}

@Override
public void actionPerformed(ActionEvent e) {
perf.someMethod();
}
}


Related Topics



Leave a reply



Submit