How use return between methods - c#

In my Program I have a button click, from that onclick event I am calling one method for some textbox validation. The code is:
protected void btnupdate_Click(object sender, EventArgs e)
{
CheckValidation();
//Some other Code
}
public void CheckValidation()
{
if (txtphysi.Text.Trim() == "")
{
lblerrmsg.Visible = true;
lblerrmsg.Text = "Please Enter Physician Name";
return;
}
//Some other Code
}
Here if txtphysi.text is null then it comes into loop and use return then it came from the CheckValidation() method only and it continues in btnupdate_Click event, but here I want to stop the execution process in btnupdate_Click also. How can I do it?

It is very simple programming logic in my understanding that you need to apply here..
That is return Boolean from CheckValidation() method instead of return void so that parent function knows the state from the function's execution.
protected void btnupdate_Click(object sender, EventArgs e)
{
var flag = CheckValidation();
if(!flag)
return;
//Some other Code
}
public bool CheckValidation()
{
var flag = false; // by default flag is false
if (string.IsNullOrWhiteSpace(txtphysi.Text)) // Use string.IsNullOrWhiteSpace() method instead of Trim() == "" to comply with framework rules
{
lblerrmsg.Visible = true;
lblerrmsg.Text = "Please Enter Physician Name";
return flag;
}
//Some other Code
flag = true; // change the flag to true to continue execution
return flag;
}

To achieve this:
1) Change the return type of the CheckValidation() method to bool. In your btnupdate_Click() method do something along the lines of
if(!CheckValidation())
{
return;
}
Hope that helps.

If this is ASP.NET, which you have tagged it as, then you should perhapts use a RequiredFieldValidator (for webforms), and you could use DataAnnotations if you are using MVC: http://forums.asp.net/t/1983198.aspx?RequiredFieldValidator+in+MVC

Though answer has been already accepted, but I believe this is not good practice to put code in events. Better to use delegates for such purposes.
public class Sample
{
public Sample()
{
UpdateAction = OnUpdate;
}
Action UpdateAction = null;
private void OnUpdate()
{
//some update related stuff
}
protected void btnupdate_Click(object sender, EventArgs e)
{
CheckValidation(UpdateAction);
}
public void CheckValidation(Action action)
{
if (txtphysi.Text.Trim() == "")
{
lblerrmsg.Visible = true;
lblerrmsg.Text = "Please Enter Physician Name";
return;
}
action();
}
}

Here try this:
protected void btnupdate_Click(object sender, EventArgs e)
{
if(!CheckValidation())
{
//Some other Code
}
}
public bool CheckValidation()
{
if (string.isnullorEmpty(txtphysi.Text.Trim()) )
{
lblerrmsg.Visible = true;
lblerrmsg.Text = "Please Enter Physician Name";
return false;
}
else
{
//Some other Code
return true;
}
}

Related

Can we pass object like textbox to a function?

I wanna ask about winforms.
Is it possible to pass object like TextBox to a function that we can call it "Checker Validation" ?
because I'm too lazy check 1 by 1 on every textbox so I'm curious about how to check it with a function
public void checker(object)
{
if(Object.Text == ""){ Object.BackColor = Color.Red;}
else{Object.BackColor = Color.White;}
}
public void textbox_TextChanged(object sender,EventArgs e)
{
checker(object);
}
i have no idea to input the object textbox on a function #.#
You can do
public void checker(TextBox txtBox)
{
if(textbox == null)
{
return;
}
if(txtBox.Text == "")
{
txtBox.BackColor = Color.Red;
}
else
{
txtBox.BackColor = Color.White;
}
}
public void textbox_TextChanged(object sender,EventArgs e)
{
checker(sender as TextBox);
}
Yes.. basically just make the param to your function a textbox...
private void Checktext(textbox mytext)
{
if (mytext.Text=="whatever")
{ }
else
{ }
}
Thats psudo code off top of my head, but it will get you there.

Clear All TextBoxes when Input is changed in a certain box in WinForm program

I have an application where the user enters their name and weight and it will convert the weight. I was wondering if there was a way that if the user's name input was less than 2 characters the 'calculate' button would be disabled and would re-enable when more than 2 characters. I am required to create an isValid method and when I run it my application does not disable the button.
public bool isValid (String text)
{
if (string.IsNullOrWhiteSpace(Name.Text) || Name.Text.Length < 2)
{
return false;
}
else
{
return true;
}
}
private void ValidateName()
{
while (isValid(Name.Text) == false)
{
this.ConvertBtn.Enabled = false;
}
}
Use TextChanged Event
private TextBox_TextChanged(object sender, EventArgs e)
{
TextBox myTextBox = (TextBox)sender;
this.ValidateName(myTextBox.text);
}
In this case IsValid method will be executed when any changes happened in the TextBox.
I think in your case Leave Event will be better since it will execute only when user leave a control
private void TextBox_Leave(object sender, EventArgs e)
{
TextBox myTextBox = (TextBox)sender;
this.ValidateName(myTextBox.text);
}
And change your IsValid method to use parameter passed there
public bool isValid (String text)
{
cosnt int MIN_LENGTH = 2;
if (string.IsNullOrWhiteSpace(text) == true) return false;
if (text.Length < MIN_LENGTH) return false;
return true;
}
Then you don't need a loop in the Validate method
private void ValidateName(string name)
{
this.ConvertBtn.Enabled = this.IsValid(name);
}
Use textChanged event of textbox in which name is input. From that event, you can call isValid method
Added trim() to Name.Text as scenario might be Name should not include pre and post spaces. If its no required you can remove the trim().
const int LENGTH_MIN_NAME = 2;
private void Name_TextChanged(object sender, EventArgs e)
{
this.ConvertBtn.Enabled = isValid(Name.Text.Trim());
}
public bool isValid(String text)
{
return (!(string.IsNullOrWhiteSpace(text) || text.Length <= LENGTH_MIN_NAME));
}

c# create object for .show() and .hide()

Is there a way to create a method that goes like this in C#:
public void showHide(string shOne, string shTwo, string shThree) {
button1.shOne();
button2.shTwo();
button3.shThree();
}
private void discountButton_Click(object sender, EventArgs e) {
showHide("Show", "Hide", "Hide")
// I was thinking that this should be the same as
// button1.Show();
// button2.Hide();
// button3.Hide();
}
Is this possible ? I'm designing a c# application for thesis and I need to show and hide buttons (lots of buttons and labels and stuff).
I'm using this code as of now but I keep getting error:
Panel' does not contain a definition for 'shOne'.
public void showHide(bool shOne, bool shTwo, bool shThree)
{
button1.Visible = shOne;
button2.Visible = shTwo;
button3.Visible = shThree;
}
You can do this by multiple ways here is my code
private void button1_Click(object sender, EventArgs e)
{
if (button2.Visible == false)
{
button2.Show();
}
else
{
button2.Hide();
}
}

improve a basic login system using radio buttons

here is my code:
public bool radioButtons()
{
if (!userRadioButton.Checked && !adminRadioButton.Checked)
{
MessageBox.Show("You must select an account type");
return false;
}
else
{
return true;
}
}
now, when the username is empty, it brings up the messagebox twice?
I think what you want to implement is something like that
public bool radioButtons()
{
if (!userRadioButton.Checked && !adminRadioButton.Checked)
{
MessageBox.Show("You must select an account type");
return false;
}
else
{
return true;
}
}
and in button click event.
public void button1_Click(object sender, EventArgs e)
{
bool a = radioButtons();
if (a)
{
// a is true do what you want.
}
}
you need to change 2 things:
the radioButtons method doesn't need to accept a boolean
you should declare a first
your code should look like:
public bool radioButtons()
{
if (!userRadioButton.Checked && !adminRadioButton.Checked)
{
MessageBox.Show("You must select an account type");
return false;
}
else
{
return true;
}
}
public void button1_Click(object sender, EventArgs e)
{
if (radioButtons())
{
//Your code here
}
}

Validate one method C#

If I have one validating method Method1 which returns e.Cancel true or false, and which looks like this:
private void textBox1_Validating_1(object sender, CancelEventArgs e)
{
ErrorProvider errorProvider = new ErrorProvider();
bool isEmpty = String.IsNullOrEmpty(textBox1.Text);
if (isEmpty)
{
e.Cancel = true;
errorProvider.SetError(textBox1, "txt");
}
else
{
e.Cancel = false;
errorProvider.SetError(textBox1, "");
}
}
And I want to get the result of validation, in my other method, here:
private void button4_Click(object sender, EventArgs e)
{
//bool passed = this.Validate(textBox1_Validating_1);
if (passed == false) return;
I would want something like this:
bool passed = this.Validate(textBox1_Validating_1);
Only to validate this one method. How do I do it?
I am able to do it like this:
bool passed = this.ValidateChildren();
if (passed == false) return;
But if I do that, then I validate all my methods, but I one want to validate just this one Method1
How can I accomplish this?
I will suggest to create a separate method for validation and call it on submit. Try this :
private void SubmitButton_Click(object sender, EventArgs e)
{
if (ValidateControls()==0)
{
//Form is validated
}
}
int ValidateControls()
{
int flag = 0;
errorProvider1.Clear();
if (txtAge.Text.Trim() == String.Empty)
{
errorProvider1.SetError(txtAge, "Age is required");
flag = 1;
}
............................................
............................................
// validate all controls
............................................
............................................
if (txtSalary.Text.Trim() == String.Empty)
{
errorProvider1.SetError(txtSalary, "Salary is required");
flag = 1;
}
return flag;
}
public bool IsValidated()
{
return !String.IsNullOrEmpty(textBox1.Text);
}
private void button4_Click(object sender, EventArgs e)
{
bool passed = IsValidated();
}
Something like this?
var cnclEvent = new CancelEventArgs();
textBox1_Validating_1(null, cnclEvent);
if (cnclEvent.Cancel) return;

Categories

Resources