Abbasi
Abbasi

Reputation: 93

how to set default values in user control windows form

i have a user control in which username and password are 2 text boxes. i want to set default values to them but not getting the right way to do so. i need simple and easy code if any one can provide??

private void UserControl1_Load(object sender, EventArgs e)
    {
        txtboxUserName.Text = "Khawar";
        txtboxPassword.Text = "123456";
    }

but the problem is that when i run this program name and password already showing in text boxes instead of asking user to enter name and password.

Upvotes: 4

Views: 3292

Answers (3)

Mohsin
Mohsin

Reputation: 902

Select the Field by click on it then click open properties tab and find the property named as Text then give it the Value u want to see here enter image description here

Upvotes: 1

Muhammad Umar
Muhammad Umar

Reputation: 3781

No need to show default values in username and password text box. Change your control load event as given below.

private void UserControl1_Load(object sender, EventArgs e)
{
    txtboxUserName.Text = "";
    txtboxPassword.Text = "";
}

Now i assume you want textbox and password value on some other event like button click event. In button click event check if username and password values are not empty then use those other wise default values as shown below.

private void ButtonClick(object sender, EventArgs e)
{
     string userName = String.IsNullOrEmpty(txtboxUserName.Text) ? "Khawar" : txtboxUserName.Text;
     string password = String.IsNullOrEmpty(txtboxPassword.Text) ? "123456" : txtboxPassword.Text;
}

Upvotes: 2

Whitesmell
Whitesmell

Reputation: 204

Do you mean this?

var userName = String.IsNullOrEmpty(txtboxUserName.Text) ? "Khawar" : txtboxUserName.Text

Upvotes: 0

Related Questions