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

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

问题描述

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

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

MainWindow.xaml.cs 包含三个文本框,我想将它们的值传递到 RateView.xaml.cs.这些文本框的内容可以由最终用户更改,但无论如何我总是希望文本框的值进入 rateview.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.

如何做到这一点?

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

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

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

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

然后将 GetSomeNumber() 方法传递给另一个单独的类以进行一些计算.

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

在初始加载时,它适用于我方法中的值,但一旦有人更改值 rateview.xaml.cs 就无法识别此更改,并且始终使用第一次加载的值.

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.

谢谢

推荐答案

只是一个小例子(这是winforms)

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天全站免登陆