Setting Properties of Controls on Another Form

Title 2: How can I set the Text property of a TextBox in another form?

Page 1 of 1

3 Replies - 12399 Views - Last Post: 03 February 2008 - 10:55 PM Rate Topic: -----

#1 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Setting Properties of Controls on Another Form

Posted 03 February 2008 - 02:03 PM

I have been googling for a while but haven't been able to find the answer to my problem. I want to be able to set the text property in a TextBox in another form, without having to show another form, close another form, etc.

I know in good ol' VB.NET you can do something like this and it does exactly what I'd want it to do in C# (this not being the real names, hyperthetically):
Form1.TextBox1.Text = "Eyyyyy"


But in C#, afaik the only way I can set the Text property of a TextBox in another form is like so (the TextBox being declared public of course):

frmMain formMain = new frmMain();
formMain.rtxBBCode.Text = templateInfo[2];
formMain.Show();



Is there a way I can do this, without needing to create another instance of frmMain? Thanks in advance. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Setting Properties of Controls on Another Form

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Setting Properties of Controls on Another Form

Posted 03 February 2008 - 03:27 PM

How are you currently instantiating the second form, the one with the TextBox you wish to change?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Setting Properties of Controls on Another Form

Posted 03 February 2008 - 08:46 PM

TextBox1 simply doesn't exist until you have an instance of Form1, so I'm not real sure how it works in VB.NET

You seem to understand the problem. Yes, that's how it works. No one gets to formMain.rtxBBCode.Text until you have an instance of the class, so I'm not real sure why it would be an issue.

I you want to set some kind of default value, you could have a static value that loads in when then instance is created.

e.g.
public Form1 : Form {
   public static string defaultRtxBBCodeValue = "foo";
   public Form1 {
	  ...
	  this.rtxBBCode.Text = Form1.defaultRtxBBCodeValue;
   }
}



Again, not real sure why you'd want to.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: Setting Properties of Controls on Another Form

Posted 03 February 2008 - 10:55 PM

Well the second form was kind of a template picker, and I wanted to be able to set the text of the textbox on the main form according to the choice the user selected.

By reading both of your posts, I came up with the idea of declaring a public variable in the Dialog form, because I knew I kept the original instance of the Dialog form. When the DialogResult was OK, I set the textbox's value from the main form.

Example (for those that were having the same problem):

The Main Form:

In a subroutine:
// Create an instance of the dialog form.
frmPickTemplate pickTemplateForm = new frmPickTemplate();

// Show the form, and if it results in an OK by a button press
// set the text to whatever the value is in the public variable.
if (pickTemplateForm.ShowDialog() == DialogResult.OK)
	rtxBBCode.Text = pickTemplateForm.pickTemplateText;



The Dialog Form:

In the form's class:
public string pickTemplateText = "";



Setting the value (in a subroutine):
pickTemplateText = "Hello";



Thanks for all your help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1