How to Access Winform Textbox Control from Another Class

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;
}
}

Access textbox from another class

Try this -

  public partial class Form1 : Form
{
Log log;
public Form1()
{
InitializeComponent();
log = new Log(richTextBox1);
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
log.AddLog(DateTime.Now.ToString());
}

private void button2_Click(object sender, EventArgs e)
{
log.AddLog();
}
}

public class Log
{
RichTextBox rtb;
public Log(RichTextBox rtb)
{
this.rtb = rtb;
}

public void AddLog(string msg)
{
rtb.Text += msg + Environment.NewLine;
}

public void AddLog()
{
rtb.Text += DateTime.Now.ToString() + Environment.NewLine;
}
}

This should do the trick. You need to pass the reference of the object which you want to modify.

C# Get textBox value from another class

You need to find the opened Form1 instead of creating another Form1, create the following class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
class Class1
{
public void someMethod()
{
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
Debug.WriteLine(t.Text + "what?");
}
}
}

Then in your button click method

private void button1_Click(object sender, EventArgs e)
{
Class1 c = new Class1();
c.someMethod();
}

c# Update WinForm GUI textBox from another class/event

Instead of:

Program.MainForm.AppendTextBox("Device removed\r\n");

Use:

Application.OpenForms.OfType<MainForm>().FirstOrDefault()?.AppendTextBox("Device removed\r\n");

Application.OpenForms is a static property giving you access to all the forms that are open within the application.

The above linq line checks the OpenForms list for any of the type MainForm then pulls the first one, if it exists, and calls AppendTextBox on it.

The ?. operator does the null check for you. It's equivalent of this code:

MainForm form = Application.OpenForms.OfType<MainForm>().FirstOrDefault();
if (form != null)
form.AppendTextBox("Device removed\r\n");

C# using winform controls in another class

In the property of the label (or any control) set the "Modifiers" option to "Public"

Now you can access the label from the object of the Form

 Form1 f = new Form1()
f.lblMyLabel.Text = "My Text"

How to interact with textbox control on winform from class?

Your InputDevice class is referencing the base Form class, not your Form1 class that's extending it. So your code compiles, and you can access anything available on the base Form class from InputDevice, but you can't access anything specific to Form1.

Modify your InputDevice class to reference Form1 instead of Form.

Form1 mainForm;
public InputDevice( IntPtr hwnd, Form1 frmMain )
{
... stuff ...
mainForm = frmMain;
}

There may not even be a need to pass the instance of Form1 to InputDevice. If "ScanCode" were a public property on InputDevice (with a public "getter" at least), you could just use this code in Form1 instead:

id.KeyPressed += delegate { txtScanCode.Text = id.ScanCode; };

Or perhaps the scan code is passed back via the event arguments of that delegate?

id.KeyPressed += (s, e) => txtScanCode.Text = e.ScanCode;


Related Topics



Leave a reply



Submit