C# – Accessing Text Box Values from Form to another class

cgetsettextboxwpf

I have a WPF Application which contains a class called RateView.xaml.cs and MainWindow.xaml.cs

The MainWindow.xaml.cs contains three textboxes of which values I want to pass into the RateView.xaml.cs. The content of these textboxes can be changed by the end user but regardless of that I always want whatever the value is of the textbox to be going into rateview.xaml.cs.

How can this be done?

I am a newbie to coding hence not sure, someone mentioned Get and Set statements, if so how can I do these?

Currently I access my textboxes like this in the MainWindow:

private float GetSomeNumber()
{
    bool Number1 = false;
    float parsedNumber1Value = 0.00F;
    Number1 = float.TryParse(Number1_TextBox.Text, out parsedNumber1Value);
    return parsedNumber1Value;
}

The GetSomeNumber() method is then passed to another seperate class to do some calculation with.

On intital load it works of the value from my method, but once someone changes the value rateview.xaml.cs doesn't recognise this change and always uses the values that were first loaded.

Thanks

Best Answer

Just a small example (This is winforms)

This is the mainwindow, where your textbox is:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
1
public string TextBox1Text
{ 
  get { return textBox1.Text; }
  set { textBox1.Text = value;
}
}

and this is a class where you want to interact with the textboxes:

public class Test
{
public Test(Form1 form)
{
//Set the text of the textbox in the form1
form.TextBox1Text = "Hello World";
}
}
Related Topic