C# - Disable button if string empty or has whitespace? - c#

I just started learning C#.
Here's my code:
private void button1_Click(object sender, EventArgs e)
{
object Nappi1 = ("Nice button");
MessageBox.Show(Nappi1.ToString());
}
I got a textbox, that should disable the button1 if empty or whitespace.
I already got it working in some level, but it checks the state of the textbox on button1_Click.
private void button1_Click(object sender, EventArgs e)
{
if (textBox1 = "")
{
button1.enabled = false;
}
else
{
button1.enabled = true;
object Nappi1 = ("Nice button");
MessageBox.Show(Nappi1.ToString());
}
}
Fictional example:
if (textBox1 = "" or textBox1 = whitespace[s])
How could I make it check the state of the textbox onLoad (as soon as the program starts)?
How could I make it check if (multiple) whitespace, and can I write it to the same if -statement?
Please keep it simple.

To answer exactly the question title, Shorter, clearer:
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);

Replace your if-else with this, if it is only a string:
if (string.IsNullOrWhiteSpace(textBox1)) {
button1.enabled = false;
}
else {
button1.enabled = true;
...
}
or use textBox1.Text if it is really a Textbox use this:
if (string.IsNullOrWhiteSpace(textBox1.Text)) {
button1.enabled = false;
}
else {
button1.enabled = true;
...
}

You want String.IsNullOrWhiteSpace:
if (String.IsNullOrWhiteSpace(textBox1.Text)) {
button1.enabled = false;
}
You originally had:
if (textBox1 = "") {
button1.enabled = false;
}
textbox is the control, you need to use the Text property which refers to the string literal inside the textbox control. Also in C# = is an assignment, you ideally would want == which is used to compare.
If you're not using .NET 4 or .NET 4.5 you can use:
String.IsNullOrEmpty

This can be done by using hooking an event handler to text-box text changed event.
Steps:
Attach an event handler for text-box (on text changed)
Inside the text changed event handler enable / disable the button.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text))
button1.Enabled = false;
else
button1.Enabled = true;
}
By default disable the button in InitializeComponent method of form
button1.Enabled = false;

In the textBox1 text changed event, right this code:
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
string.IsNullOrWhiteSpace("some text") will check if the text is none or just a wightspaces
if this is true you will set button1.Enabled to false.

Related

Disable button when the selected value in comboBox changed

I have a button and a comboBox. The comboBox has 2 value, 'yes and no' I want to disable the button if the selected value is no while i want to enabled if the value selected is yes what would I do, I dont know where will I put the code and also my code seems wrong.
if (ComboBoxCustType.SelectedIndex = 0)
{
Button1.Enabled = false;
}
else
Button1.Enabled = true;
Since you didn't specify WinForms or WPF, this is for WinForms.
You have to create an event on the ComboBox.SelectedIndexChanged and inside the event handler, you write your code to handle the selecteditem text.
public Form1()
{
comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
InitializeComponent();
CheckSelction();
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckSelction();
}
void CheckSelction()
{
if (comboBox1.SelectedItem != null)
{
var item = comboBox1.SelectedItem.ToString();
button1.Enabled = item == "yes";
}
else
button1.Enabled = false;
}

Simple login screen with user name and password

I'm trying to create a simple login screen, which has 2 textboxes and 1 button. When strings i've insterted in the first textbox (username) and the second textbox (password) matches the strings i've defined before, the buttons Enabled propertie should become True and when clicked, it should open another form, here's the code i've written so far:
public partial class LogInScreen : Form
{
public LogInScreen()
{
InitializeComponent();
string lietotajvards = "user";
string parole = "user";
if (textBox1.Text == lietotajvards && textBox2.Text == parole)
{
button1.Enabled = true;
}
else
{
button1.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
this.Hide();
f1.Show();
}
}
The thing is that with my code it doesn't work as expected and the button is enabled all the time. Where's the problem?
Your code will only execute once when the form is initialized, you have to make use of a textchanged event on textbox1 and textbox2, thereafter you can use the code you wrote to check if the button needs to be enabled. In the else you must disable the button.
Text changed event handlers:
void textBox1_TextChanged(object sender, EventArgs e)
{
handleLoginButton();
}
void textBox2_TextChanged(object sender, EventArgs e)
{
handleLoginButton();
}
Enable/disable button:
private void handleLoginButton(){
string lietotajvards = "user";
string parole = "user";
if (textBox1.Text == lietotajvards && textBox2.Text == parole)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}
The constructor only runs once for the form, you need to handle the text changed events for the input controls and then re-evaluate your condition again.
Also it should go without saying (although it is being said here) that this is a terrible way to handle logging in to an application.

Check if user type a single character or number in textbox c#

I already can make the login for user, so if user type their username or password wrongly in textbox, the message label that says "Invalid username or password" appear. But, i want to when user type a single character or number in textbox when the message label is appear, the message label will not visible to user (visible = false) as user already type a single character or number in textbox. But the message label didn't disappear when user type a single character or number.
This is the code:
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
And here is the image:
Below image is when user type wrongly (the username or password), the message label appear:
Below image is when user type a single character or number, but the message label still at there
My question is: How do i set the message label to not show when user type a single character or number in the textbox?
Any help?
Your answer will be great appreciated!
Thank you!
Problem : You have not wiredup the CheckTextBox() method for both TextBox1 and TextBox2 TextChanged Event.
Solution : in your Form_Load WireUp the CheckTextBox() method for the Textbox1 and TextBox2 TextChanged Event as below:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.TextChanged += new System.EventHandler(this.CheckTextBox);
textBox2.TextChanged += new System.EventHandler(this.CheckTextBox);
}
Suggestion : i think string.IsNullOrWhiteSpace() is more appropriate as it would also check for Whitespace in addition to null and Empty strings.
Try This:
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
This line is checking if either textbox has information in it.
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
Change the || to && and then the label will only be shown when BOTH textboxes do not have any data.
If I understand you correctly, try this:
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
If you change || to && then label5 will be visible only if both textboxes are empty.
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
Try this..use && instead of ||
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
Check this code, Should be call this OnTextChanged="CheckTextBox" on your textbox
protected void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="CheckTextBox"></asp:TextBox>

Making picturebox appear when combobox element is selected

I'm trying to make pictures appear, when the element in combobox is selected.
private void Form4_Load(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
pictureBox1.Visible = true;
pictureBox2.Visible = true;
pictureBox3.Visible = true;
pictureBox4.Visible = true;
}
}
I tried to do it this way, but that doesn't work. Where I’ve made the mistake?
The naming of your method makes me think you're doing that inside the form loading event.
Use the SelectedIndexChanged event of the combobox instead.

Why Doesn't Regex give a chance to input data?

I have a event which doesn't give a any opportunity to input data in TextBox. When I'm trying to input data in Textbox, then Textbox doesn't give to do it:
private void Login_textbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(textbox1.Text, #"^[a-zA-Z]+$"))
e.Handled = true;
}
I just want to input data in TextBox which isn't digit or any symbols.
Try using the following code
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString() , #"^[a-zA-Z]+$"))
e.Handled = true;
}
Thanks!
It seems you are using c#.
Then steps you need to follow :
1) Set causesValidation property of your textbox to true
2) Set event listeners for causes validation
myTextBox1.Validating +=
new System.ComponentModel.CancelEventHandler(myTextBox1_Validating);
myTextBox1.Validated +=
new System.EventHandler(myTextBox1_Validated);
3) Implement these event hadler functions
private void myTextBox1_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
if(!CheckIfTextBoxNumeric(myTextBox1))
{
myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
}
private void myTextBox1_Validated(object sender,System.EventArgs e)
{
myLabel.Text = "Validated first control";
}
If you instead want to use maskedTextBox refer http://msdn.microsoft.com/en-us/library/ms234064(v=vs.80).aspx

Categories

Resources