Button's OnClick event is called:
private void vkb7_Click_1(object sender, EventArgs e)
{
//method
button_start(object sender);
// do stuff 2
}
Some method is triggered:
private void button_start(object sender)
{
bool some = true;
if (some)
{
return; // I'd also like to break "button click"
}
}
I'd like to break "button click" event from method - how do I do it?
Simple, let button_start() return a boolean:
private bool button_start(object sender)
{
bool some = ...;
return some;
}
private void vkb7_Click_1(object sender, EventArgs e)
{
//method
if (button_start(object sender))
return;
// do stuff 2
}
If you are determining in button_start whether actions should occur in vkb7_Click_1, consider making button_start return bool and then use the return value in vkb7_Click_1.
On the other hand, if you are encountering an error condition in button_start, throw an exception.
You can't really "block" the event as it has already been fired. You can however handle the click manually depending on the result.
private void vkb7_Click_1(object sender, EventArgs e)
{
bool result = button_start(sender);
if (result)
DoSomething();
else
DoSomethingElse();
}
private bool button_start(object sender)
{
bool some = true;
if (some)
return false;
return true;
}
First, Set the return type of button_start to bool
Second, If you want to pass sender of vkb7 button or properties of vkb7 button,
Then you should pass it like this:
button_start(sender)
So the code would be like this:
private void vkb7_Click_1(object sender, EventArgs e)
{
//method
if(button_start(sender))
// do stuff 2
}
Then in the method
private bool button_start(object sender)
{
bool some = true;
if (some)
{
return true; // I'd also like to break "button click"
}
else
return false;
}
Related
I have an entry_TextChanged event handler and a button.
How can I say if this button clicked go to that entry_TextChanged handler again?
private bool save = false;
private void entry_TextChanged(object sender, TextChangedEventArgs e)
{
if (save == true)
{
// Do Something
}
}
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
// GO and do the entry texchanged??
}
you can just call it the same as any other function entry_TextChanged(null, null) or any other values that you want to send to the function
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
entry_TextChanged(null, null)
}
Alright, so I'm trying to practice my windows forms skills and I've been trying to figure out a simple thing such as making a button that says "Show message", when you click it, it makes an invisible label Visible that says "Hey man" and the text of the button should change to "Hide message".
When you click it the label should go invisible again. I tried to do it with if statements for a while but didn't really figure it out, so I just used 2 buttons which made it a million times more simple. I'm just wondering, how would I go about doing it with only 1 button?
My code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnShow_Click(object sender, EventArgs e)
{
lblShow.Visible = true;
btnShow.Visible = false;
btnHide.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnHide_Click(object sender, EventArgs e)
{
lblShow.Visible = false;
btnShow.Visible = true;
btnHide.Visible = false;
}
}
}
You could add a bool to your class, and change the bool when you click the button
bool showLabel = true;
private void btnToggle_Click(object sender, EventArgs e)
{
if (showLabel)
{
lblShow.Visible = true;
}
else
{
lblShow.Visible = false;
}
showLabel = !showLabel;
}
This can be done quite simply with only two lines of code:
private void btnToggle_Click(object sender, EventArgs e)
{
lblShow.Visible = !lblShow.Visible;
btnToggle.Text = lblShow.Visible ? "Hide Message" : "Show Message";
}
It can technically be done in one line, but I think it's less readable:
btnToggle.Text = (lblShow.Visible = !lblShow.Visible) ? "Hide Message" : "Show Message";
I suggest keeping a state variable and updating the form based on the value of the state variable. This is a flexible pattern where you can accommodate more than one state, which means you can have combinations of buttons that produce different results.
Try this:
public partial class Form1 : Form
{
public enum FormState
{
MessageHidden,
MessageVisible,
Default = MessageHidden
}
FormState state = FormState.Default;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
UpdateUI();
}
protected void UpdateUI()
{
switch (state)
{
case FormState.MessageHidden:
label1.Visible = false;
button1.Text = "Show Message";
break;
case FormState.MessageVisible:
label1.Visible = true;
button1.Text = "Hide Message";
break;
default:
throw new NotImplementedException();
}
}
private void button1_Click(object sender, EventArgs e)
{
switch (state)
{
case FormState.MessageHidden:
state = FormState.MessageVisible;
break;
case FormState.MessageVisible:
state = FormState.MessageHidden;
break;
default:
throw new NotImplementedException();
}
UpdateUI();
}
}
Example screenshots
FormState.MessageHidden
FormState.MessageVisible
You can refactor using one method to set the label visibility like that:
private void btnToggle_Click(object sender, EventArgs e)
{
lblShow.Visible = !lblShow.Visible;
btnToggle.Text = lblShow.Visible ? "Hide Message" : "Show Message";
}
With the VS Form Desiner you can set the label Visible property to true and the button Text to nothing.
And in the FormLoad event you call the method to set the things :
btnToggle_Click(this, null);
Can a button return the value of a variable?is it possible? I'm new in winform so...understand me. The final goal is to have a simple code with two buttons that if clicked return a msgbox with the value of a var.
private string button3_Click(object sender, EventArgs e)
{
string res = "PASS";
MessageBox.Show(res);
return res;
}
private string button4_Click(object sender, EventArgs e)
{
string res = "FAIL";
MessageBox.Show(res);
return res;
}
Well, button3_Click and alike are so-called callback functions which are called by system and that's why do not return any value (the system doesn't need it). Let's extract a method:
private string MyButtonClick(object sender) {
string result = "";
if (sender == button3)
result = "PASS"; //TODO: better read it from resources, not hadcoded
else if (sender == button4)
result = "FAIL"; //TODO: better read it from resources, not hadcoded
if (!string.IsNullOrEmpty(result))
MessageBox.Show(result);
return result;
}
And then you can put:
// System callback function
// void: System callback function signature
private void button3_Click(object sender, EventArgs e) {
MyButtonClick(sender);
}
// void: System callback function signature
private void button4_Click(object sender, EventArgs e) {
MyButtonClick(sender);
}
Or
// custom code
string myClickMessage = MyButtonClick(button4);
Click event handlers have strongly typed signature, i.e. the return type must be void, so you cannot return anything from them.
But, if you are working with windows Forms, you have class for your form, where you placed those event hadnlers. So what you can do is: define private variable:
pirvate string _res;
and then assign it value in your click event hadlers, eg.:
private string button3_Click(object sender, EventArgs e)
{
_res = "PASS";
MessageBox.Show(_res);
}
Then in the code you could use this value as you want.
If you want to use it outside your class, define property:
public string Res
{
get { return _res; }
}
I have a button that on click event goes and get data from server and displays that on a grid.
The code is like below:
private void btnSearch_Click(object sender, EventArgs e)
{
// Here I should do something in order to know if the async ProcessSearch method is busy..
// if not busy then I will execute it if not then I will return.
// shows loading animation
ShowPleaseWait(Translate("Searching data. Please wait..."));
ProcessSearch();
}
private async void ProcessSearch()
{
Data.SeekWCF seekWcf = new Data.SeekWCF();
_ds = await seekWcf.SearchInvoiceAdminAsync(new Guid(cboEmployer.Value.ToString()), new Guid(cboGroup.Value.ToString()), txtSearchInvoiceNumber.Text, chkSearchLike.Checked, txtSearchFolio.Text, Convert.ToInt32(txtYear.Value));
seekWcf.Dispose();
if (_ds != null)
{
SetupInvoiceGrid();
}
// hides the loading animation
HidePleaseWait();
}
How can I know if the async method ProcessSearch is busy or running so I can prevent the user to execute the method again when clicking the button again.
You could just set a boolean:
private bool isSearching = false;
private void btnSearch_Click(object sender, EventArgs e)
{
if (isSearching)
return;
// shows loading animation
ShowPleaseWait(Translate("Searching data. Please wait..."));
ProcessSearch();
}
private async void ProcessSearch()
{
isSearching = true;
// do other stuff
isSearching = false;
}
If you're concerned about concurrency, you could add a lock:
private bool isSearching = false;
private object lockObj = new object();
private void btnSearch_Click(object sender, EventArgs e)
{
lock (lockObj)
{
if (isSearching)
return;
else
isSearching = true;
}
// shows loading animation
ShowPleaseWait(Translate("Searching data. Please wait..."));
ProcessSearch();
}
private async void ProcessSearch()
{
// do other stuff
isSearching = false;
}
I have two buttons in my program and i want that when i press first button the second button is clicked automatically ( in the event handler of first button , i want to press the second button through coding).
private void button1_Click(object sender, EventArgs e)
{
passWord = pwd.Text;
user = uName.Text;
loginbackend obj = new loginbackend();
bool isValid = obj.IsValidateCredentials(user, passWord, domain);
if (isValid)
{
loginbackend login = new loginbackend();
passWord = pwd.Text;
login.SaveUserPass(passWord);
HtmlDocument webDoc = this.webBrowser1.Document;
HtmlElement username = webDoc.GetElementById("__login_name");
HtmlElement password = webDoc.GetElementById("__login_password");
username.SetAttribute("value", user);
password.SetAttribute("value", passWord);
HtmlElementCollection inputTags = webDoc.GetElementsByTagName("input");
foreach (HtmlElement hElement in inputTags)
{
string typeTag = hElement.GetAttribute("type");
string typeAttri = hElement.GetAttribute("value");
if (typeTag.Equals("submit") && typeAttri.Equals("Login"))
{
hElement.InvokeMember("click");
break;
}
}
button3_Click(sender, e);
label1.Visible = false ;
label3.Visible = false;
uName.Visible = false;
pwd.Visible = false;
button1.Visible = false;
button2.Visible = true;
}
else
{
MessageBox.Show("Invalid Username or Password");
}
}
private void button3_Click(object sender, EventArgs e)
{
HtmlDocument webDoc1 = this.webBrowser1.Document;
HtmlElementCollection aTags = webDoc1.GetElementsByTagName("a");
foreach (HtmlElement link in aTags)
{
if (link.InnerText.Equals("Show Assigned"))
{
link.InvokeMember("click");
break;
}
}
}
I think what you're describing is that you want to call a method when button B is clicked, but then also call that method when button A is clicked.
protected void ButtonA_Click(...)
{
DoWork();
}
protected void ButtonB_Click(...)
{
// do some extra work here
DoWork();
}
private void DoWork()
{
// do the common work here
}
Depending on your implementation in the event handlers, you can also just call the event handler of the second button from that of the first, but the above way is the 'right' way to do it.
You could just call the method.
private void btnA_Click(object sender, EventArgs e)
{
doA();
}
private void doA()
{
//A stuff
}
private void btnB_Click(object sender, EventArgs e)
{
doA();
doB();
}
private void doB()
{
//B stuff
}
Or call the _Click method directly;
private void btnB_Click(object sender, EventArgs e)
{
btnA_Click(sender, e);
doB();
}
I guess, you don't really care whether the button is clicked or not, you just care that the code of the second button is executed. So... just call it:
void button1_Click(...)
{
button2_Click(...);
}