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;
}
}
Related
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() { }
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();
}
Need help in making a simple calculator. i can't put more than one number in my calculator's textbox. Everytime i put a second number it replaces the first one need help!
I can't exceed more than one input number in my Calculator's Textbox instead it replaces the first number with a second number input
namespace Calculator_Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void InputOutputArea_TextChanged(object sender, EventArgs e)
{
}
private void One_Click(object sender, EventArgs e)
{
int Input = 1;
InputOutputArea.Text = Input.ToString();
}
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text = Input.ToString();
}
private void Three_Click(object sender, EventArgs e)
{
}
private void Four_Click(object sender, EventArgs e)
{
}
private void Five_Click(object sender, EventArgs e)
{
}
private void Six_Click(object sender, EventArgs e)
{
}
private void Seven_Click(object sender, EventArgs e)
{
}
private void Eight_Click(object sender, EventArgs e)
{
}
private void Nine_Click(object sender, EventArgs e)
{
}
private void Eql_Click(object sender, EventArgs e)
{
}
private void AddB_Click(object sender, EventArgs e)
{
}
private void Minus_Click(object sender, EventArgs e)
{
}
private void MultiplyB_Click(object sender, EventArgs e)
{
}
private void DivideB_Click(object sender, EventArgs e)
{
}
private void Zero_Click(object sender, EventArgs e)
{
}
private void ResetB_Click(object sender, EventArgs e)
{
InputOutputArea.Clear();
}
}
}
You should use
InputOutputArea.Text += Input.ToString();
(note the '+') in order to append to a text box.
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text += Input.ToString();
}
You must use += to add other text to next of first text
Here is your problem:
InputOutputArea.Text = Input.ToString();
This replaces the content of the textbox instead of adding to it.
InputOutputArea.Text += Input.ToString();
the above code should do as you ask.
Good to remember is that concatenating strings with + is rather inefficient, so don't do this in performance critical code unless absolutely necessary. In those cases a String-builder is almost always better.
Every answers talking about the Concatenation of the previous text with the current, But I would like to suggest something more than that;
You need not to create separate event handlers for all your buttons that are doing same tasks, Hope that the Text of each button will be the number that you need to display in the textBox(say btnOne will holds 1 and btnTwoholds 2 and so on). By make use of this Text we can reuse the handlers like the following, Let btnNumber_Click be the handler and which is defined like the following:
private void btnNumber_Click(object sender, EventArgs e)
{
Button currentButton = sender as Button;
InputOutputArea.Text += currentButton.Text;
}
The methods below are just inverses of one another. I suspect that I can combine the logic into one method. I prefer to avoid Reflection. Is it possible to combine them and maintain readability?
private void btnAdd_Click(object sender, EventArgs e)
{
LabEntity selectedItem = bindingSource1.Current as LabEntity;
selectedLabsData.Add(selectedItem);
availableLabsData.Remove(selectedItem);
}
private void btnRemove_Click(object sender, EventArgs e)
{
LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
availableLabsData.Add(selectedItem);//called Add instead of remove
selectedLabsData.Remove(selectedItem);//called Remove instead of Add
}
You could factor out the logic into a helper method:
private void ListFixup(object entity, List<Item> addList, List<Item> removeList)
{
LabEntity selectedItem = entity as LabEntity;
// don't forget your error checking here
addList.Add(selectedItem);
removeList.Remove(selectedItem);
}
private void btnAdd_Click(object sender, EventArgs e)
{
ListFixup(bindingSource1.Current, selectedLabsData, availableLabsData);
}
private void btnRemove_Click(object sender, EventArgs e)
{
ListFixup(bindingSource2.Current, availableLabsData, selectedLabsData);
}
I'm not sure this helps readability, but it does reduce code duplication.
private void btnAdd_Click(object sender, EventArgs e)
{
SwapThem( bindingSource1, selectedLabsData, availableLabsData );
}
private void btnRemove_Click(object sender, EventArgs e)
{
SwapThem( bindingSource2, availableLabsData, selectedLabsData );
}
// I just don't know the proper type-cast of the "toAddTo" and "toRemoveFrom" parameters.
private void SwapThem( BindingSource bs, List<yourType> toAddTo, List<yourType> toRemoveFrom )
{
LabEntity selectedItem = bs.Current as LabEntity;
toAddTo.Add(selectedItem);
toRemoveFrom.Remove(selectedItem);
}
There isn't any way to refactor the two methods that wouldn't result in significantly reducing the readability of the code, you can see this in some of the other answers posted. This is a case where the level of code duplication is acceptable for the sake of readability.
Add a tag to the sender?
private void btnClick(object sender, EventArgs e)
{
Button *myButton = (Button)sender;
if (myButton.tag == 1){
LabEntity selectedItem = bindingSource1.Current as LabEntity;
selectedLabsData.Add(selectedItem);
availableLabsData.Remove(selectedItem);
}
else {
LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
availableLabsData.Add(selectedItem);//called Add instead of remove
selectedLabsData.Remove(selectedItem);//called Remove instead of Add
}
}
I havn't checked this for compiler errors, It is just an example.
Have both buttons link to a single event handler. The handler could then look something like this: (excuse the multiple checks on the buttons as I don't know what type to declare holders for 'availableLabsData' and 'selectedLabsData' as):
private void btnClick(object sender, EventArgs e)
{
var bindingSource = (sender == btnRemove) ? bindingSource2 : bindingSource1;
var selectedItem = source.Current as LabEntity;
if(sender == btnRemove)
{
availableLabsData.Add(selectedItem);
selectedLabsData.Remove(selectedItem);
}
else if(sender == btnAdd)
{
availableLabsData.Remove(selectedItem);
selectedLabsData.Add(selectedItem);
}
}
Everyone's right about not going overboard at the expense of making the code less readable
The only thought I have is ...
From just the code posted but it looks like selectedLabsData and availableLabsData are tightly intertwined so I wouldn't put logic related to them in your event handlers. Put the logic in different methods (preferably a different class) so that you don't ever accidentally update one without updating the other. This has the benefit of making the methods in question simpler and more "readable".
private void LabsDataAdded(LabEntity value)
{
selectedLabsData.Add(value);
availableLabsData.Remove(value);
}
private void LabsDataRemoved(LabEntity value)
{
availableLabsData.Add(value);
selectedLabsData.Remove(value);
}
then the methods would just be:
private void btnAdd_Click(object sender, EventArgs e)
{
LabsDataAdded(bindingSource1.Current as LabEntity);
}
private void btnRemove_Click(object sender, EventArgs e)
{
LabsDataRemoved(bindingSource2.Current as LabEntity);
}
Even better, you could use lambda expressions to make the code even more succinct:
btnAdd.Clicked += (sender, e) => LabsDataAdded(bindingSource1.Current as LabEntity);
btnAdd.Clicked += (sender, e) => LabsDataRemoved(bindingSource1.Current as LabEntity);
Something like this:
private void btnAdd_Click(object sender, EventArgs e)
{
LabEntity selectedItem = bindingSource1.Current as LabEntity;
RemoveItemFromList(selectedItem);
}
private void btnRemove_Click(object sender, EventArgs e)
{
LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
RemoveItemFromList(selectedItem);
}
private void RemoveItemFromList(LabEntity ent)
{
selectedLabsData.Add(ent);
availableLabsData.Remove(ent);
}
I've got these Methods:
private void button_Click(object sender, EventArgs e)
{
//Changes the Text in the RichBox, EXAMPLE:
richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged";
}
And,
private void richTextBox_TextChanged(object sender, EventArgs e)
{
//Wants something like that
if(called from button_click)
{
//DO SOMETHING
}
else
{
//DO SOMETHING
}
}
How can I handle this, to know if it was called from the Button_click?
Do I have to use the object sender to get informations? But how?
Hope u guys can help me
Just use a flag:
private bool _isInButtonClick;
private void button_Click(object sender, EventArgs e)
{
try
{
_isInButtonClick = true;
//Changes the Text in the RichBox, EXAMPLE:
richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged";
}
finally
{
_isInButtonClick = false;
}
}
private void richTextBox_TextChanged(object sender, EventArgs e)
{
if(_isInButtonClick)
{
//DO SOMETHING
}
else
{
//DO SOMETHING
}
}
private void richTextBox_TextChanged(object sender, EventArgs e)
Here sender is the richTextBox, not the button that changed the text.
You could go into the stack trace to discover if the button click is on the call stack, but that's overkill (like using a nuke to crack a walnut).
Add a flag (bool) to your form, set it to true in the button click, and check it in the TextChanged event, then at the end of the button click, set it to false again.
If you do this I would advise wrapping this signal logic in a class that implements IDispose and use it in using statements.
That said, are you sure you need this functionality?