I want if a user type the character "a" in textbox, message box with message "Ok" displays, and then textbox should clear.But I am facing the problem that when I type "a" message "Ok" is displays and along with message "No" is also displays.But when I remove the statment of clear then all goes good. Please tell me how to overcome this problem ?
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "a")
{
textBox1.Text = "";
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("No");
}
}
It's because the Text="" triggered the TextChanged one more time. Use some flag like this:
bool suppressTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e) {
if(suppressTextChanged) return;
if (textBox1.Text == "a") {
suppressTextChanged = true;
textBox1.Text = "";
suppressTextChanged = false;
MessageBox.Show("Ok");
} else {
MessageBox.Show("No");
}
}
NOTE: The code above supposes you want to check against a string ("a" is just an example). If you want to check against a character. Use the KeyPress event instead.
You're using the TextChanged event of the textbox. When you change the text manually the TextChanged event runs again and this time the else expression runs.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text != "")
if (textBox1.Text == "a")
{
textBox1.Text = "";
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("No");
}
}
That is because when you set textBox1.Text = ""; the event textBox1_TextChanged is raised one more and there is not a letter 'a' in the textbox and therefore a messasge box with "No" is also displayed.
You need to check if the change in textBox1.Text was from the user, or from you. "No" is displayed because you change the text to something ("") that is not "a". You could keep a boolean flag that indicates whether you want to react on the change:
bool changedByCode = false;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(changedByCode) return;
if (textBox1.Text == "a")
{
changedByCode = true;
textBox1.Text = "";
changedByCode = false;
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("No");
}
}
You should handle KeyUp event
public Form1()
{
InitializeComponent();
textBox1.KeyUp+=new KeyEventHandler(textBox1_KeyUp);
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (textBox1.Text == "a")
{
textBox1.Text = "";
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("No");
}
}
That's because the textBox1.Text = "" calls textBox1_TextChanged again.
Related
What i want to do is:
When the user start typing anything in the textBox2 first make it empty and then start showing what the user is typing.
Then if the user deleting what he was typing then show the original text again.
Now what it does is that i need to delete on my own the text inside but then when i try to type anything i see the original text again since it's empty.
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text != "" || textBox2.Text == "Enter Email Account")
{
textBox2.Text = "Enter Email Account";
button2.Enabled = false;
}
else
{
button2.Enabled = true;
}
}
You should be using a Watermark. But in case you wish to do it as you started, you set the default value in properties.
You need first of all to create your event handlers during the Enter and Leave event;
private void Form1_Load()
{
textBox1.Text = "Enter Email Account";
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
textBox1.LostFocus += new EventHandler(textBox1_Leave);
}
Then on Enter, the watermark text should be deleted;
protected void textBox1_GotFocus(object sender, EventArgs e)
{
if (textBox1.Text == "Enter Email Account")
{
textBox1.Text = "";
}
}
Then you re-check on Leave event;
protected void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
textBox1.Text = "Enter Email Account";
}
}
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>
i am making a windows form application in which i used a datagridview.
i want that when i write something in textbox in datagridview,than a messagebox appears containing the string i wrote..
ican't get my text in textchanged event..
all thing must be fired in textchanged event..
here is my code:-
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
TextBox tb = (TextBox)e.Control;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
//listBox1.Visible = true;
//string firstChar = "";
//this.listBox1.Items.Clear();
//if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
string str = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString();
if (str != "")
{
MessageBox.Show(str);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
var enteredText = (sender as TextBox).Text
...
}
Showing MessageBox in TextChanged will be very annoying.
Instead you could try it in DataGridView.CellValidated event which is fired after validation of the cell is completed.
Sample code:
dataGridView1.CellValidated += new DataGridViewCellEventHandler(dataGridView1_CellValidated);
void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
I am trying to validate windows form with try catch and so far I succeeded. My goal is when someone forgot to fill the gap or put in incorrect entry, catch returns messagebox with a warning. Now I also have Validating event on every control I want to validate so when somebody leave it empty or in incorrect format it will show the error next to the control. That seems ok so far (for me, at least) but my issue is, that if user doesn't even click to one box it only shows message box, but it won't highlight wrong controls.
Below is my code:
private void createButton_Click(object sender, EventArgs e)
{
try
{
Book newBook = new Book(titleBox.Text, authBox.Text, Convert.ToInt32(yearBox.Text), Convert.ToInt32(editBox.Text), pubComboBox.Text, descBox.Text);
bookList.Add(newBook);
booklistListBox.DataSource = bookList;
}
catch (FormatException)
{
MessageBox.Show("You probably missed a gap or put in incorrect form");
}
}
and those validating events:
private void titleBox_Validating(object sender, CancelEventArgs e)
{
if (titleBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(titleBox, "Title is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(titleBox, "");
}
}
private void authBox_Validating(object sender, CancelEventArgs e)
{
if (authBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(authBox, "Author is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(authBox, "");
}
}
private void yearBox_Validating(object sender, CancelEventArgs e)
{
if (yearBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(yearBox, "Year is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(yearBox, "");
}
}
private void editBox_Validating(object sender, CancelEventArgs e)
{
if (editBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(editBox, "Edition is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(editBox, "");
}
}
private void pubComboBox_Validating(object sender, CancelEventArgs e)
{
if (pubComboBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(pubComboBox, "Publisher is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(pubComboBox, "");
}
}
private void descBox_Validating(object sender, CancelEventArgs e)
{
if (descBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(descBox, "Description is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(descBox, "");
}
}
So is there way to, I don't know, change focus or something like that, forced with pressing the create button?
Thank You
Try using ValidateChildren():
private void createButton_Click(object sender, EventArgs e)
{
bool gotIssues = this.ValidateChildren();
if (gotIssues)
{
// someone didn't validate well...
}
}
So, the issue here is that you want to have it highlight in either of two scenarios:
1) When you leave the field and its contents are invalid (empty in this case)
2) When you click the create button and the field in question has invalid contents
And so I would create a single textBox_checkIfEmpty(object sender, EventArgs e) method:
private void textBox_checkIfEmpty(object sender, EventArgs e)
{
var asTb = sender as TextBox;
if (asTb != null && asTb.Text.Trim() == String.Empty)
{
errorProvider.SetError(asTb, "I'll leave it to you to abstract the error message appropriately");
e.Cancel = true;
}
else
{
errorProvider.SetError(asTb, "");
}
}
Then, you can set this method as the handler for your Validate event on your desired required controls, and you can also call the same method from the create button's handler, looping through the required TextBox instances and executing the method on each.
UPDATE
J. Hudler's ValidateChildren solution would be a more (developer) efficient tail to mine, as opposed to looping through the desired controls. That said, if the form has many children, and you only need to validate several, it might be helpful to loop still. Just depends on your specific scenario. My only other question is whether or not ValidateChildren is infinitely recursive, or if it only goes one level down (immediate children rather than all descendants).
the event validating for control call when the mouse click on the control and then leave it from the control. In your case when the user does not click on the control it will not trigger the validating event. U can do this by making your own function and call them on creat event.
private void button1_Click(object sender, EventArgs e)
{
textBox1_Validating(sender);
}
public void textBox1_Validating(object sender)
{
MessageBox.Show("validating");
errorProvider1.SetError(textBox1, "provide");
}
I'm currently creating a calculator type form on C#. I have four radiobuttons (Addition, subtraction, multi, and div) and a label in between two textboxes. The label changes according to the selected radiobutton, (for example if I selected the Addition radiobutton the label would read "+"). The problem I'm experiencing with this code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
label3.Text = ("+");
}
else if (radioButton2.Checked == true)
{
label3.Text = ("-");
}
else if (radioButton3.Checked == true)
{
label3.Text = ("x");
}
else if (radioButton4.Checked == true)
{
label3.Text = ("/");
}
}
is when I select the division button the label does not change unless I go through all the buttons and THEN other radio buttons (such as subtraction), when selected, do not change the label until multiple tries. I tried changing the last line to an "else label3.text=("/");" but it doesn't really change anything other than the order of errors.
Any help would be appreciated! Thanks :)
I think you need to check if the radio button is checked in each individual radioButtonX_CheckedChanged method like so:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
label3.Text = ("+");
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
label3.Text = ("-");
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked)
{
label3.Text = ("x");
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
if (radioButton4.Checked)
{
label3.Text = ("/");
}
}
Let me know if that helps, and if you are still having the issue.
You may want to change how you check for the Checked button. MrB's solution works, but if you'd like to keep your selection code in a single block (as you have), make sure all your radio buttons have their CheckedChanged event subscribed to something similar to the following:
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
var radioButton = (RadioButton)sender;
if (radioButton.Checked)
{
switch (radioButton.Text)
{
case "Add":
label3.Text = "+";
break;
case "Subtract":
label3.Text = "-";
break;
case "Divison":
label3.Text = "/";
break;
}
}
}
You can also switch on another property, such as the RadioButton.Tag field, whatever may be meaningful to you.
As far as the actual reason your code is failing, it's hard to understand without ensuring which RadioButton's have their events set properly, and seeing the incorrect results.
protected void Page_Load(object sender, EventArgs e)
{
agm.Visible = RadioButtonList1.SelectedValue == "1" ? true : false;
}