从窗体访问文本框值到另一个类 [英] Accessing Text Box Values from Form to another class

查看:157
本文介绍了从窗体访问文本框值到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,它包含一个名为RateView.xaml.cs和MainWindow.xaml.cs的类。



MainWindow.xaml.cs包含三个文本框,其中我想要传递给RateView.xaml.cs的值。这些文本框的内容可以由最终用户进行更改,但无论如何,我总是希望文本框的任何值都可以进入rateview.xaml.cs。



如何做到这一点?



我是编码的新手,因此不确定,有人提到Get和Set语句,如果是的话,我该怎么做?



目前我在MainWindow中访问我的文本框:

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

然后将GetSomeNumber()方法传递给另一个单独的类来做一些计算



在intital加载时,它对我的​​方法中的值起作用,但是一旦有人更改了值rateview.xaml.cs不能识别此更改并始终使用首先加载的值。



谢谢

解决方案

例如(这是winforms)

这是主窗口,其中的文本框是:

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


$ / code $ / pre

这是一个你想与之交互的类文本框:

  public class Test 
{
public Test(Form1 form)
{
//在form1中设置文本框的文本
form.TextBox1Text =Hello World;
}
}


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

解决方案

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

这篇关于从窗体访问文本框值到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆