简体   繁体   中英

Set default value by textbox in C#

  • I'm developing an application. I have a button btnSendEmail to send a password email from server to user email. My application is able to send a email but It need to know the password of my PC. In the future, if I build this application to another server, I also need to know that password of that PC. Therefore, I have a form with two text boxs: Email server and Password server. How can I set up that one time, and set that value to default value.
  • For example, I wanna set server email is abc@gmail.com and password is 123456. It doesn't change until I change texts from two text boxs. Then I build to another PC, I can change to xyz@gmail.com and password is 234567. Is there any way to do that?

You could use Settings for this. In the projects properties, there is a page Settings . You can enter strings there and give them a name. Create a string and call it UserEmail (The value you enter now will be the default value for new installations). In the Form where the value should be shown create a method that is called when the Form is loaded (perhaps called from Form_Load):

private void Init()
{
    this.txtEmail.Text = Properties.Settings.Default.UserEmail;
}

Now you need to save the value. You could do this whenever the value changes or just, when the form closes. The following method could be called in both scenarios (perhaps Form_Closing):

private void Save()
{
    Properties.Settings.Default.UserEmail = this.txtEmail.Text;
    Properties.Settings.Default.Save();
}

This saves the value, that is currently entered in the TextBox to the setting, which will be loaded the next time the application starts.

In winforms, you could use below code on load,

Textbox.Text = "default text"

Example:

emailTextbox.Text = "abc@gmail.com"
passwordBox.Text = "123456"

First of all assign hardcode value for two textboxs, email and password in page load like below

protected void Page_Load(object sender, EventArgs e)
{
emailTextbox.Text = "youremail@gmail.com";
passwordTextbox.Text = "yourPassword";
}

After button btnSendEmail click Check follow

if(!string.IsNullOrEmpty(emailTextbox.Text)&&!string.IsNullOrEmpty(passwordTextbox.Text))
{
// write email sending code here
}
else
{
//warning message regarding email id and password is required
}

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