简体   繁体   中英

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. I want to 'getUserName()' (Which returns a username) and display it in a Textbox

    // 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

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();

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:

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.

// 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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