简体   繁体   English

如何在 Windows 窗体 C# 上的文本框中显示文本

[英]How to display text in a TextBox on a Windows Form C#

There are several answers online about this question but none of them have answered my question.网上有很多关于这个问题的答案,但没有一个回答我的问题。 I have a Windows Form which has a list of details of a user, some of the data can be edited while some cannot.我有一个 Windows 窗体,其中包含用户的详细信息列表,有些数据可以编辑,有些则不能。 I want to 'getUserName()' (Which returns a username) and display it in a Textbox我想“getUserName()”(返回用户名)并将其显示在文本框中

    // Display Student Details Form
    public void displayStudent()
    {
        StudentDetails sd = new StudentDetails();
        sd.ShowDialog();
    }

The above creates the new form which contains several labels and textboxes, when it is created (and displayed), the textboxes on that form should populate to contain student information (such as username, name, address etc)以上创建了包含多个标签和文本框的新表单,创建(并显示)时,该表单上的文本框应填充以包含学生信息(如用户名、姓名、地址等)

    private void displayUserName_TextChanged(object sender, EventArgs e)
    {
        displayUserName.Text = displayUserName.Text = s.getUserName();
    }

s is a Student Object s 是学生对象

The above code, I wish to take the username from the student object and display it in a textbox for another user to see and possibly edit.上面的代码,我希望从学生对象中获取用户名并将其显示在文本框中以供其他用户查看和可能编辑。

Just try displayUserName.Text = s.getUserName();试试displayUserName.Text = s.getUserName();

Put this in the Form Load event, on a Button press or comboBox Selection event... Making some assumptions (Because you did not provide many details), for instance in your StudentDetails Form code behind:将其放在 Form Load 事件中,在 Button press 或 comboBox Selection 事件中......做出一些假设(因为你没有提供很多细节),例如在你的 StudentDetails 表单代码后面:

public partial class StudentDetails : Form
{
    private int _studentId;

    private DbContext _context;

    public StudentDetails(int studentId)
    {
        _context = new DbContext();
        _studentId = studentId;
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        Student s = _context.Students.Find(_studentId);

        displayUserName.Text = s.getUserName();
        // Using a function here is overkill, perhaps.
        // This should also work here:
        // displayUserName.Text = s.FullName;


        base.OnLoad(e);
    }
} 

Now in your main form:现在以您的主要形式:

StudentDetails form = new StudentDetails(studentId);
form.ShowDialog() ;

As an aside, most people will not bother answering your question if you do not at least try and then show what you have tried.顺便说一句,如果您至少不尝试然后展示您尝试过的内容,那么大多数人都不会费心回答您的问题。

Try this code where note that I do specify that the Text for the displayUserName box is given after I've created the object and the x is merely whatever you have for ensuring that the instance has the data set to use rather than being blank that would continue the bug.试试这个代码,注意我在创建对象后指定了displayUserName框的文本,而x只是确保实例具有要使用的数据集而不是空白的任何东西继续bug。

// Display Student Details Form
public void displayStudent()
{
    StudentDetails sd = new StudentDetails();
    sd.getData(x);
    displayUserName.Text = sd.getUserName();
    sd.ShowDialog();
}

The TextChanged call would be what you'd use after the user has changed the value of the text box and is something to do as part of a processing the form rather than initializing it, in my experience.根据我的经验,在用户更改文本框的值之后,您将使用TextChanged调用,并且作为处理表单的一部分而不是初始化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM