Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking a Text Box to a variable?

Tags:

c#

textbox

I would like to have direct access to the text inside a textbox on another form, so I added a public variable _txt to a form and added an event like so:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    _txt = richTextBox1.Text;
}

But the form is loaded like this:

public FrmTextChild(string text)
{
    InitializeComponent();
    _txt = text;
    richTextBox1.Text = _txt;
    Text = "Untitled.txt";
}

Is there a better way to directly link the two?

like image 911
cam Avatar asked Jan 22 '23 08:01

cam


1 Answers

You could use a property instead to read directly from your TextBox. That way you don't need an extra variable at all.

public string Text
{
  get
  {
    return richTextBox1.Text;
  }
}

Add a setter if you also want to be able to change the text.

like image 125
Michael Madsen Avatar answered Feb 01 '23 03:02

Michael Madsen