• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

How To Access A Textbox On One Form From Another Form?

char[] rager

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
I have read many forums and answers on the Internet and since I will not be taking C#.NET at my university until later, I still want to know how to modify a textbox on my main form from another form.

I am making an automatic code generator for HTML, and on my main form I have a textbox showing the HTML code currently in a .html file. On my main form, I also have a button to access a code editor, which I put into a different windows form.

Now, whenever I make code changes in my code editor, save it, and exit that windows form, what do I write in that formClosing or formClosed event handler to update the textbox on my MAIN form? By the way, I have already set that textbox to public status.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You have to either put in a property or make the textbox object public. Then you can access it via InstanceOfOwningForm.NameOfTextbox.

E.g.
Code:
public class MainForm : Form
{
   private TextBox _DisplayBox = new TextBox();

  public MainForm()
  {
    InitializeComponent();

    ChildForm frm = new ChildForm();
    frm.ShowDialog();
    _DisplayBox.Text = frm.EditableText;
  }
}

public class ChildForm
{
  private TextBox _MyTextBox = new TextBox();
  public string EditableText
  {
    get { return _MyTextBox.Text; }
    set { _MyTextBox.Text = value; }
  }
}

The other alternative is to make a static class and a variable static to hold the value. Static means it is owned by the class instead of the object.

Code:
public static class Static
{
  public static string EditableText;
}

public class MainForm : Form
{
   private TextBox _DisplayBox = new TextBox();

  public MainForm()
  {
    InitializeComponent();

    ChildForm frm = new ChildForm();
    frm.ShowDialog();
    _DisplayBox.Text = Static.EditableText;  // Reads the value and displays it.
  }
}

public class ChildForm
{
  private TextBox _MyTextBox = new TextBox();
  public Childform()
  {
    Initialize Component();
    
    this.FormClosing += new FormClosingEventHandler(this.MainForm_FormClosing);
  }
  
  private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  {
    Static.EditableText = _MyTextBox.Text; // Sets the text.
  }
}
 
Last edited:

char[] rager

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
Thank you FordGT90Concept. I got it to work :toast:
 
Top