How do I use a while with when a button is clicked? - c#

I have a piece of code where I want it so that it won't execute that piece of code until the necessary button is clicked, in this case a play again button. Here is my piece of code:
private void ShowResultsOfThrow()
{
TwoUp.SetUpCoins();
TwoUp.ThrowCoins();
TwoUp.OutputWinner();
do
{
TwoUp.ThrowCoins();
TwoUp.OutputWinner();
TwoUp.HoldPoints();
} while //when the button is clicked to play again
TwoUp.ResetGlobals();
}

Just put that code in button_click event
private void button1_click(object sender, EventArgs e)
{
TwoUp.ThrowCoins();
TwoUp.OutputWinner();
TwoUp.HoldPoints();
}

Based on your comments:
Don't use the ShowResultsOfThrow() method.
Instead, if your button is named Button1, for instance, try this:
private void Initalize()
{
TwoUp.SetUpCoins();
}
private void Button1_click(object sender, EventArgs e)
{
TwoUp.ThrowCoins();
TwoUp.OutputWinner();
TwoUp.HoldPoints();
TwoUp.ResetGlobals(); // Optional - not sure what this does...
}
Initalize() should only be called ONCE (or whenever you need to setup the coins)

If you have access to the button.you can do this
let us assume the button name is btnClick
let there be a method that would get called when the button is clicked.
private void TwoUpMethod(object sender, EventArgs e)
{
TwoUp.ThrowCoins();
TwoUp.OutputWinner();
TwoUp.HoldPoints();
TwoUp.ResetGlobals(); // Optional - not sure what this does...
}
Now register this method with the Click event of the button.Put this below code in InitializeComponent() method of the form
btnClick.Click+=TwoUpMethod;

Related

Raise an Event Except when something happpen

Scenario: Only If the user follow the path Click on ListView > Click on Button the Button1 do something.
In other word I want to check in Button1_Click(object sender, EventArgs e) if the previous focus was on ListView.
So I tried this:
private void ListView_Test_Leave(object sender, EventArgs e)
{
_focusedControl = null;
}
I want raise previous event except when this event is raised:
private void Button1_Click(object sender, EventArgs e)
{
if(_focusedControl == listView_Test)
{
// ...
}
}
Edit: I have a variable that holds a reference to the currently focused control:
private Control _focusedControl;
and I update it in this way:
private void ListView_Test_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
If the user follow the path Click on ListView > Click on Button I want raise only the Button1_Click event, in all other case I want normal raise.
You could use a helper variable.
bool wasRaised=false;
private void Button1_Click(object sender, EventArgs e) { wasRaised=true;}
Then you can check that variable in your event, and only run if it is false.

Button click event clicking exe

I have simple Windows form With just one button which Calls one url
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
Process.Start(sInfo);
}
When i click exe then it shows button and then i have to click button to perform action. Is it possible to call button click event automatically on clicking exe.
You can do the same with Form.Load event. Just refactor you code with extract method to have no duplicated code and call your function in both event handlers:
private void button1_Click(object sender, EventArgs e)
{
OpenUrl();
}
private void form1_Load(object sender, EventArgs e)
{
OpenUrl();
}
private void OpenUrl()
{
ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
Process.Start(sInfo);
}
Simply call:
button1_Click(this, null);
from anywhere in your code.

Why it is not Possible to call textBox1_KeyPress() event from some Button Click event.?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
MessageBox.Show("called btn 0 click..");
KeyPressEventArgs e0 = new KeyPressEventArgs('0');
textBox1_KeyPress(sender, e0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("called txtbox_keypress event...");
}
}
Am sorry if this is a silly question,I have just started to learn windows forms, I still find material on the internet confusing.I want to implement calculator. So when number button is pressed it should be filled in textbox. So I thought calling textBox1_keypress() event from button click event would work??? but its not working,
I can manually write the logic in button click event to fill text in text box but if i do so, i have to do the same thing in button1_KeyPress event too. so it would be duplication of code right??...so i thought solution was to call textBox1_KeyPress() event from both button click event and button key press event...but its not working .So what should i do???..is there any other approach which should i follow.
so it would be duplication of code right??
Yes, it would be. So you can do
private void btn0_Click(object sender, EventArgs e)
{
CommonMethod(e);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
CommonMethod(e);
}
private void CommonMethod(EventArgs e)
{
//Your logic here.
}
The TextBox KeyPress event handler (textBox1_KeyPress) is called after the user presses a key. The KeyPressEventArgs parameter includes information such as what key was pressed. So calling it from your btn0_Click method isn't going to set the text for the TextBox.
Rather, you want to (probably) append whatever number the user pressed to the text already present in the TextBox. Something like
private void btn0_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
might be closer to what you're trying to accomplish.
You could put the logic in an extra function like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
NumberLogic(0),
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// I don't know right now if e contains the key pressed. If not replace by the correct argument
NumberLogic(Convert.ToInt32(e));
}
void NumberLogic(int numberPressed){
MessageBox.Show("Button " + numberPressed.ToString() + " pressed.");
}
}
You don't want to tie the events together like that.
A key-press is one thing, handled in one way.
A button click is something totally different and should be handled as such.
The basic reason is this,
The button doesn't know what number it is, you need to tell it that.
A key-press on the other hand, knows what number was pressed.
If you REALLY want to, for some reason, you could use SendKeys to trigger your key-press event in a round-about way, from the button.
SendKeys.SendWait("0");
I can suggest to you to use an Tag Property of the Buttons. Put in it the value of each button in Design mode or in Constructor, create one button event handler for all buttons and use Tag value:
Constructor:
button1.Tag = 1;
button2.Tag = 2;
button1.Click += buttons_Click;
button2.Click += buttons_Click;
Event hadler:
private void buttons_Click(object sender, EventArgs e)
{
textBox1.Text = ((Button)sender).Tag.ToString();
}

Why textbox click event not firing

I have a textbox and it's readonly. When I click on I want it to call my button click event:
private void tbFile_Click(object sender, EventArgs e)
{
//btnBrowse_Click(sender, e);
MessageBox.Show("test");
}
When click on the textbox, nothing happens. How do I fix it?
Update:
private void btnBrowse_Click(object sender, EventArgs e)
{
openFile();
}
private void tbFile_Click(object sender, EventArgs e)
{
//btnBrowse_Click(sender, e);
if (tbFile.Text != "")
{
openFile();
}
}
public void openFile()
{
var FD = new System.Windows.Forms.OpenFileDialog();
FD.Filter = "DBF Files|*.DBF";
FD.InitialDirectory = #"C:\";
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileToOpen = FD.FileName;
tbFile.Text = fileToOpen;
}
}
When I hit browse button and select a file, the browse file window comes up again. So it's appearing twice now and the textbox click is still not working.
There is no reason that can be inferred from the information you provided why you shouldn't trigger the openFile() method when you click on the tbFile control.
The fact that the textbox is set to readonly does not stop it from raising the click event in any way.
The only possibility is that the method is not assigned to the click event of the control.
Make sure in the event properties of the control that the click event is indeed assigned to the "tbFile_Click" method.
Just because there exsits a method that's called the same as a control but has "_Click" added does not make it get executed unless you specifically tell c# you want to associate that method with the click event of the control.
When you assign the method through the event window, C# generates a code file behind the scenes that adds the callback to that specific event.
You should use the btnBrowse.PerformClick() method to simulate a user click, instead of calling the handler.
The default I got from VS 2013 was a 'MouseClick' function so this works:
private void btnBrowse_Click(object sender, EventArgs e)
{
MyAwesomeFunction(sender);
}
private void tbFile_MouseClick(object sender, MouseEventArgs e)
{
MyAwesomeFunction(sender);
}
private void MyAwesomeFunction(object sender)
{
MessageBox.Show("test");
}

How to perform Label click in C#?

I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}

Categories