I want to user to enter textbox value before continue the run next method. After message box shows and click ok. application goes to MyNextMethod. Therefore I cannot enter my valid value to text box.
If (textbox1 =="")
{
MessageBox.Show("Please enter valid entery.")
}
MyNextMethod();
If (textbox1.Text == string.empty)
{
MessageBox.Show("Please enter valid entery.")
}
else
{
MyNextMethod();
}
Use
return;
after
MessageBox.Show();
function
This is your requirement or goal: You want user to enter textbox value before continue the run next method. It is OK with your old code.
If (textbox1 =="")
{
MessageBox.Show("Please enter valid entery.")
}
But you call MyNextMethod() method, it means whether user enter the value or not you allow to go MyNextMethod().
Conclusion, you can do as you like:
1. you can append return; after MessageBox.Show(); as first Answer
2. you can also call MyNextMethod() with else. as 2nd Answer
You can use textbox onChange()
event. If text is changed in textbox
, check if entered value is a number and calculate total value according to the other value. try this code
If (textbox1.Text =="" || textbox1.Text.Length==0)
{
MessageBox.Show("Please enter valid entery.");
Console.ReadLine();
}
else
{
MyNextMethod();
}
You can also use textbox1_LostFocus()
so when Focus
lost you can call your NextMethod
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
MyNextMethod();
}
You can also do this if textbox is empty
if(textbox1.text=="")
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Please enter text')</script>");
}
else
{
// your next method
}
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.