How to perform Click-Event on a hidden button - c#

In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.

Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}

If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }

Related

Clicked 2 Button At Once

Hello :) I Actually want to ask something
So my aim here is "If I click the button 1, the button 2 will be clicked too "
"Is it possible ? Click a one button so the other button will be clicked ?"
Here is my Code:
Button btn1 = sender as Button;
if (btn1 == button1){
button2.PerformClick();
}
It actually does not work it seems there is something wrong
I suggest extracting methods.
Before:
private void button1_Click(object sender, EventArgs e)
{
Routine 1 code ...
Routine 2 code ... // <- do not copy yourself; copy + paste is evil!
}
private void button2_Click(object sender, EventArgs e)
{
Routine 2 code ...
}
After:
//TODO: think over the right name
private void Routine1()
{
Routine 1 code ...
}
//TODO: think over the right name
private void Routine2()
{
Routine 2 code ...
}
...
private void button1_Click(object sender, EventArgs e)
{
Routine1();
Routine2();
}
private void button2_Click(object sender, EventArgs e)
{
Routine2();
}
It is very simple.
private void button1_Click(object sender, EventArgs e)
{
if ((sender as Button) == button1)
{
button2_Click(sender, e);
}
}
private void button2_Click(object sender, EventArgs e)
{
}
Unless you have a weird reason for doing this, don't!
You should prefer something like this :
void button1_Click(object sender, EventArgs e)
{
DoWork1();
DoWork2();
}
void button2_Click(object sender, EventArgs e)
{
DoWork2();
}

C# Calling a method

I have a form with multiple instances of a user control on it.
I've assigned the following:
Switch.armySwitchCloseButton.Click += armySwitchClose;
So when one of those instances is pressed, I call the following method:
void armySwitchClose(object sender, EventArgs e)
The above method has a bunch of additional code in it which isn't required for here.
Now what I need to do is from another button, call this above function from every instance.
How can I do this?
Many Thanks
In constructor of each user control you can pass the same instance of some object who knows how to run this method
void armySwitchClose(object sender, EventArgs e)
Then, you call this method inside each event method, for ex:
Public Class UserControl(){
private MakeEvent makeEvent;
Public MyClass(MakeEvent makeEvent)
{
this.makeEvent = makeEvent;
Switch.armySwitchCloseButton.Click += armySwitchClose;
}
void armySwitchClose(object sender, EventArgs e)
{
makeEvent.armySwitchClose(sender,e);
}
}
Public Class MakeEvent() {
void armySwitchClose(object sender, EventArgs e)
{
//the real implementation
}
}
Hope this solve your problem.
Oh, I think I get your problem wrong... When you click in one button all the other events in others UC must be triggered, right? This problem can be solved with the Observer Pattern
Public Interface IObserver
{
void armySwitchClose(object sender, EventArgs e);
}
Public UserControl1: Observer
{
void armySwitchClose(object sender, EventArgs e)
{
//implementation UC1
}
}
Public UserControl2: Observer
{
void armySwitchClose(object sender, EventArgs e)
{
//implementation UC2
}
}
In button UC:
Public UserControlButton
{
private List<IObserver> observers;
public void addObserver(IObserver observer)
{
observers.Add(observer);
}
public void button_clickedEvent(object sender, EventArgs e)
{
foreach(IObserver observer in observers)
{
observer.armySwitchClose(object sender, EventArgs e);
}
}
}
In form with all buttons you call addObserver adding each user control.

How to go from one "private void" to other "private void"?

how can i go from private void turnon to private void turnoff ? I want only know how to go from one void to other. I know i can just make from these two a one private void but i don't want it
private void turnon(object sender, EventArgs e)
{
button2.Visible = true
}
private void turnoff(object sender, EventArgs e)
{
button3.Visible = false
}
If you're wanting it to be visible in one event and visible in another, why not use one method like so:
private void SwitchState(object sender, EventArgs e)
{
button3.Visible = !button3.Visible;
}
On reading your comments I guess you want to add this line in at the end of your turnon event:
turnoff(sender, e);
Do you want a toggle? If so, you could use the following code.
private void toggle(object sender, EventArgs e)
{
button2.Visible = !button2.Visible;
}
Hi I want to make toggle button, So that it shows/hides the content by clicking that searchButton
Here is my code,
private boolean visible;
protected Button SearchButton;
private void Toggle(){
if(visible=false){
DishButton.setVisibility(View.INVISIBLE);
SpoonButton.setVisibility(View.INVISIBLE);
cupButton.setVisibility(View.INVISIBLE);
FridgeButton.setVisibility(View.INVISIBLE);
}
else {
DishButton.setVisibility(View.VISIBLE);
SpoonButton.setVisibility(View.VISIBLE);
cupButton.setVisibility(View.VISIBLE);
FridgeButton.setVisibility(View.VISIBLE);
visible=true;
}
}

How to propagate the name change of Form's UI controls's throughtout all the .cs in project

In C# , Winform, I have created a form and bunch of UI controls on it. I have changed the name of the controls through Properties windows but the following automated generated code did not update automatically. However, the InitializeComponent code is automatically updated though. My problem is now that I don't remember which box or whihc label I renamed to certain name.. Two questions : How could I have done this more efficiently to begin with? Question 2) Is there anything I could do now to make it automatically change the corresponding names? I have heard of refactoring but I don't know if I could have used it here and how? I appreciate any help.
public partial class frmMyInterface : Form
{
public frmMyInterface()
{
InitializeComponent();
}
private void frmMyInterface_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Rename each these event handlers and then on the property window, reassign the events selecting from the dropdrown. Or delete these event handlers and double click on each event in the property window and this time it will update it for you

calling a event handler within another event handler?

Here is the short sample code:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(object sender, EventArgs e); //can I call button1 event handler?
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
I wonder if it would be okay to code in the above way?
You can do that - although the code you provide can't be compiled. It should look like this:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(sender, e);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
But for best practice and code readability, you're probably better off doing this, especially as you are not making use of sender and e:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowMessageBox();
}
private void button1_Click(object sender, EventArgs e)
{
ShowMessageBox();
}
private void ShowMessageBox()
{
MessageBox.Show(txtbox1.Text);
}
Yes you can do that; an event handler is just another method.
However it might be worth creating a new method that shows the message box, and having both Click event handlers call that:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void button1_Click(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void ShowTextboxMessage()
{
MessageBox.Show(txtbox1.Text);
}
An event handler is nothing more than a method, so you can call it like any other.

Categories

Resources