C# 表单加载时初始化文本框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17183531/
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-10 08:46:04  来源:igfitidea点击:

Initialize the value of a textbox when form loads

c#winformstextbox

提问by user2499687

I would like to put a string value in my frmCredentials.txtUsernametext box when the form first loads.

frmCredentials.txtUsername当表单首次加载时,我想在我的文本框中放置一个字符串值。

Here is the code where the form is first called:

这是第一次调用表单的代码:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmCredentials());

回答by Adriaan Stander

Why not put the code in the Form.Load event

为什么不把代码放在Form.Load 事件中

Occurs before a form is displayed for the first time.

You can use this event to perform tasks such as allocating resources used by the form.

在第一次显示表单之前发生。

您可以使用此事件来执行任务,例如分配表单使用的资源。

回答by vivat pisces

You could set it in the constructor of frmCredentials:

您可以在 frmCredentials 的构造函数中设置它:

public class frmCredentials : Form
{
    public frmCredentials()
    {
        InitializeComponent();

        txtUsername.Text = "whatever";
    }
}

回答by CodeCamper

Step 1) Double Click On Your Form: This will create and display the form load event.

Step 2) Type in the {} the following, txtUsername.Text="MyTextGoesHere";

步骤 1) 双击您的表单:这将创建并显示表单加载事件。

步骤 2) 在 {} 中输入以下内容, txtUsername.Text="MyTextGoesHere";

After you try this if this still does not resolve your homework please comment below and I will try to help further.

尝试此操作后,如果仍然无法解决您的作业,请在下面发表评论,我会尽力提供进一步帮助。

回答by Mohit

You can create an overloaded constructor that accepts one parameter, and another one will be your default constructor.

您可以创建一个接受一个参数的重载构造函数,另一个将作为您的默认构造函数。

public class frmCredentials : Form
{
    public frmCredentials()
    {
        InitializeComponent();
    }

    public frmCredentials(string myValue )
    {
        InitializeComponent();
        txtUsername.Text = myValue;
    }
}

from your code:

从你的代码:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmCredentials("Hello World"));

回答by Anjan

Yes Form load event is better to load default values when loading the Form

是的,Form load 事件最好在加载 Form 时加载默认值

        private void Form1_Load(object sender, EventArgs e)
        {
            txtUsername.Text = "My Username";
        }

回答by sab669

Mohit probably has the best answer, but if you ONLY want to be able to create a form with the textbox you don't need to retain the 'default' constructor as he/she did:

Mohit 可能有最好的答案,但是如果您只想能够使用文本框创建表单,则不需要像他/她那样保留“默认”构造函数:

 public class frmCredentials : Form
 {
     public frmCredentials(string myValue)
     {
         InitializeComponent();
         txtUsername.Text = myValue;
     }
 }

Now when you call a new frmCredentials, you'll haveto pass it a string, like so

现在,当你调用一个新的frmCredentials,你就必须给它传递一个字符串,像这样

 var myForm = new frmCredentials("A string is required.");