C# 从另一个类获取 textBox 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9626187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:15:21  来源:igfitidea点击:

C# Get textBox value from another class

c#textbox

提问by Kirev

Let's say I have a form named Form1 with textBox and button in it.

假设我有一个名为 Form1 的表单,其中包含文本框和按钮。

I want to get the textBox value from another class on button click. I'm trying to do it like this, but it doesn't work:

我想在单击按钮时从另一个类中获取 textBox 值。我正在尝试这样做,但它不起作用:

class Main
{
    public void someMethod()
    {
        Form1 f1 = new Form1();
        string desiredValue = f1.textBox.Text;
    }
}

Forgive me for the dumb question, but I'm pretty new in C# and can't get this thing to work.

原谅我这个愚蠢的问题,但我在 C# 中很新,无法让这个东西工作。

采纳答案by uowzd01

You need to find the opened Form1 instead of creating another Form1, create the following class

需要找到打开的Form1而不是创建另一个Form1,创建如下类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Class1
    {
        public void someMethod()
        {
            TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
            Debug.WriteLine(t.Text + "what?");
        }
    }
}

Then in your button click method

然后在您的按钮单击方法中

private void button1_Click(object sender, EventArgs e)
{
    Class1 c = new Class1();
    c.someMethod();
}

回答by mjroodt

I think its because you are creating a new instance of form1 so you are actually only getting the textbox text from f1.

我认为这是因为您正在创建 form1 的新实例,因此您实际上只从 f1 获取文本框文本。

回答by Default

your textBox is probably private, although that is as it should be. If you need the Text from the textbox you can expose it with a property

您的 textBox 可能是private,尽管它应该是。如果您需要文本框中的文本,您可以使用属性公开它

public string TextBoxText{ get { return textBox.Text; } }

回答by daniloquio

When you say

当你说

Form1 f1 = new Form1();

You are creating a whole new object with its own textbox.

您正在创建一个带有自己的文本框的全新对象。

If you want the value that is on that textbox of that form, you will need to refer to the same instance of Form1 where the user typed the value.

如果您想要该表单的文本框上的值,您将需要引用用户键入该值的 Form1 的同一实例。

回答by REW

Is this pseudo-code, or is this the code you're actually trying to use?

这是伪代码,还是您实际尝试使用的代码?

If you're trying to use this code, what you're doing is creating a brand new Form1. Unless the constructor for Form1 puts something into your textbox, it will be empty at this point.

如果您尝试使用此代码,您所做的就是创建一个全新的 Form1。除非 Form1 的构造函数将某些内容放入您的文本框中,否则此时它将为空。

回答by Al-Ameen Coe

Form 1

表格 1

public string pathret()
{
    return textBox.Text;
}

Form 2

表格 2

class Main
{
    public void someMethod()
    {
        Form1 f1 = new Form1();
        string desiredValue = f1.pathret();
    }
}