c# - Hide button text when another button is clicked - c#

I'm trying to create a matching game that consists of 12 buttons. The program assigns a random string from an array that has 12 strings in it. When the button is pressed, the tag is passed to the button.text.
What I'm trying to accomplish now is, for example. If I press "button 1", the text of it changes to "Chevy Camaro". If I press "button 4" next, I want the button1.text to revert back to saying "button 1", rather than it's tag value of "Chevy Camaro". And in like fashion, since "button 4" was pressed, I would like it to show the tag.....
Each button has similar code to it, besides the button #, which of course changes based on the button that's being used.
I'm unsure how to state that, if button is the current active item, then show it's tag property, otherwise, revert back.
private void button4_Click(object sender, EventArgs e)
{
button4.Text = button4.Tag.ToString();
buttoncount++;
label2.Text = buttoncount.ToString();
}
Thanks in advance for all of your help. Slowly learning this stuff.... =p

You could keep track of the last button that was clicked:
public partial class Form1 : Form
{
Button lastButton = null;
int buttoncount;
public Form1()
{
InitializeComponent();
button1.Tag = "Ford Mustang";
button2.Tag = "Ford Focus";
button3.Tag = "Chevy Malibu";
button4.Tag = "Chevy Camaro";
button1.Click += button_Click;
button2.Click += button_Click;
button3.Click += button_Click;
button4.Click += button_Click;
//etc...
}
void button_Click(object sender, EventArgs e)
{
if (lastButton != null)
{
SwitchTagWithText();
}
lastButton = sender as Button;
SwitchTagWithText();
buttoncount++;
label2.Text = buttoncount.ToString();
}
void SwitchTagWithText()
{
string text = lastButton.Text;
lastButton.Text = lastButton.Tag.ToString();
lastButton.Tag = text;
}
}

Could you use a RadioButton control with its appearance set to button? Replace all buttons with these, put them in a GroupBox and the 'reverting' of appearance when clicked off can be automatically handled. To update the text, a simple event handler like below will do;
private void MakeButton()
{
RadioButton rb = new RadioButton
{
Appearance = Appearance.Button,
Tag = "Chevy Camero"
};
rb.CheckedChanged += rb_CheckedChanged;
}
private void rb_CheckedChanged(object sender, EventArgs e)
{
RadioButton clickedButton = sender as RadioButton;
string currentText = clickedButton.Text;
clickedButton.Text = clickedButton.Tag.ToString();
clickedButton.Tag = currentText;
}

Related

How can I assign same actions to multiple buttons at once?

I am working on a WinForms application where I want the user to click a button and then change the button's text (add +1 to the counter). I have multiple buttons and having three methods Click MouseDown and MouseUp for each of those would be pretty inefficient.
So I've set up a class, but don't know how to assign it to all the buttons.
public class AddButton : Button
{
public int Count { get; set; }
private void Button_Click(object sender, EventArgs e)
{
AddButton ab = (AddButton)sender;
Count++;
ab.Text = Count + " Added";
}
}
Maybe I could also just link all the button's EventHandlers to one, but I don't know how to start there and where to put the code. Here is the Visual Studio project download link if you need it.
You could try this code:
public class AddButton:Button
{
public int Count { get; set; } = 0;
public AddButton()
{
// After it's painted, set its text to current Counter
this.Paint += (s, e) => this.Text = Count.ToString();
this.Click += (s, e) => this.Text = (++Count).ToString();
}
}
In your project it's enough to change buttons initialization from:
Button button1 = new Button();
to
Button button1 = new AddButton();
// or
AddButton button1 = new AddButton();

Populate TextBox's value to a specific Textbox

I have three TextBoxe1,TextBoxe2 and TextBoxe3 and one main TextBox4 and Button1 when its clicked it will insert TextBox4's value into the clicked (the chosen/selected/clicked one) TextBox. This code Populates all the TextBoxes with the same value.
private void button1_Click(object sender, EventArgs e)
{
TextBox[] array = new TextBox[3] { textBox1, textBox2, textBox3 };
for (int i = 0; i < 3; i++)
{
if (array[i].Focus())
{
array[i].Text = textBox4.Text;
}
}
}
But I want it to take the TextBox4's value and insert into the TextBox2 that I have Clicked on. Like this illu.:
It's better to change the way that you set the value for those TextBox controls and think about another UI, but anyway, if you like to keep it as is, I'll share an idea to satisfy the requirement which you described in the question.
Define a field in form, TextBox selectedTextBox;, then handle Enter event of those 3 TextBox controls and in the handler set selectedTextBox = (TextBox)sender. Then in Click event handler of the button, check if selectedTextBox is not null, then set selectedTextBox.Text = textBox4.Text;:
TextBox selectedTextBox;
public Form1()
{
InitializeComponent();
textBox1.Enter += TextBox_Enter;
textBox2.Click += TextBox_Enter;
textBox3.Click += TextBox_Enter;
button1.Click += button1_Click;
}
void TextBox_Enter(object sender, EventArgs e)
{
selectedTextBox = (TextBox)sender;
}
void button1_Click(object sender, EventArgs e)
{
if(selectedTextBox!=null)
selectedTextBox.Text = textBox4.Text;
}
Make sure you don't attach event handler twice, so to attach event handler, use code editor or designer, not both of them.
Register the Click event of the 3 target TextBoxes to the same handler:
public Form1()
{
InitializeComponent();
textBox1.Click += TransportValueEvent_Click;
textBox2.Click += TransportValueEvent_Click;
textBox3.Click += TransportValueEvent_Click;
}
Inside the handler get the sender (which will be the TextBox that you have clicked) as TextBox and write the value:
private void TransportValueEvent_Click(object sender, EventArgs e)
{
(sender as TextBox).Text = textBox4.Text;
}
Now you don't need the button anymore. The value will be written to the correct TextBox as soon as you click it.
May be you want to avoid deletion if the textBox4 is empty, then you can update the value only if:
private void TransportValueEvent_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(textBox4.Text))
{
(sender as TextBox).Text = textBox4.Text;
}
}

How do I add click events to all buttons that have names starting with a certain string in C#?

I am writing a simple calculator script for my C# programming class. It will of course have buttons 0-9 that will update the output textbox to add the number of whatever button is clicked. My problem right now that is I would rather not have to have 10 different click events in my script. I would rather have a loop that cycles through the buttons that will add the same click event to each one and then decide what number to add to the output based on the button.
So right now, I have a click event for the "1" button which is this...
private void btnNum1_Click(object sender, EventArgs e)
{
txtOutput.Text = Convert.ToString(txtOutput.Text + "1");
}
This works fine, but, again, I would rather not have to do this 10 times. How can I create a loop that prevents this?
The button names are btnNum1, btnNum2, btnNum3, etc.
Assuming the button text is just "1", "2" etc you could do this:
private void btnNum_Click(object sender, EventArgs e)
{
var button = sender as Button
txtOutput.Text += button.Content.ToString();
}
Then just apply this event to all the buttons.
Also note you don't need Convert.ToString() as what you are trying to convert is already a string. Using += also cleans up your code a bit.
You could do this to wire-up all of the events in one go:
for (var n = 0; n <= 9; n++)
{
var btn =
this
.Controls
.Find("btnNum" + n.ToString(), false)
.Cast<Button>()
.First();
var digit = n;
btn.Click += (s, e) =>
{
txtOutput.Text = digit.ToString();
};
}
You could enumerate the children controls of your Form/Control, look the type of controls which are type of Button and the name StartWith 'btnNum', with each of these buttons, add a Click event address to btnNum_Click().
Say if all your buttons are contained in a Panel named 'pnlButtons', you could loop all the children like this.
foreach (var control in pnlButtons.Controls)
{
if(control.GetType() == typeof(Button))
{
var button = control as Button;
if (button .Name.StartWith('btnNum'))
{
button.Click += btnNum_Click;
}
}
}
You can use the "Tag" property of the Button control and make an array of Buttons to subscribe to the same event. See sample below:
void InitializeButtons()
{
Button btnNum1 = new Button();
btnNum1.Text = "1";
btnNum1.Tag = 1;
//Button 2..8 goes here
Button btnNum9 = new Button();
btnNum9.Text = "9";
btnNum9.Tag = 9;
Button[] buttons = new Button[]{
btnNum1, btnNum2, btnNum3, btnNum4, btnNum5, btnNum6, btnNum7, btnNum8, btnNum9
};
for (int i = 0; i < buttons.Length; i++)
{
buttons[i].Click += Button_Click;
}
}
void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int value = (int)button.Tag;
//Do something with value
}
Assuming WinForms, you can recursively search for buttons that start with "btnNum" and wire them up to a common handler like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
void Form1_Load(object sender, EventArgs e)
{
FindButtons(this);
}
private void FindButtons(Control ctl)
{
foreach(Control ctrl in ctl.Controls)
{
if (ctrl.Name.StartsWith("btnNum") && (ctrl is Button))
{
Button btn = (Button)ctrl;
btn.Click += btn_Click;
}
else if(ctrl.HasChildren)
{
FindButtons(ctrl);
}
}
}
private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
txtOutput.Text = Convert.ToString(txtOutput.Text + btn.Text);
}
}

Adding events to code-created objects

I am creating a 2d array of buttons using code and I want to add a button_click() method.
Besides the 2 usual arguments (object sender, EventArgs e) I want to get as an input 2 more variables, To identify which button was clicked, and do something else as a result.
I am currently doing this
arr[i,j].Click+= new EventHandler(button_click);
public void button_click(object sender, EventArgs e)
Is there another way of adding events that will allow me to do what I want?
And on a seperate note. Is there an easy way of creating cubes with a certain color without using buttons?
To identify which button was clicked, and do something else as a result.
You could use sender parameter to identify the button which was clicked.
If you do not want to introduce a custom button type which would have properties for i and j, you could use Tag property to store the indices.
Create a class which inherits from button class. And add 2 properties to it. Then access those properties in your form . Following example is one way to solve your problem.
public class ButtonCtrl : Button
{
public ButtonCtrl(int _arg1, int _arg2)
{
Arg1 = _arg1;
Arg2 = _arg2;
}
public int Arg1 { get; set; }
public int Arg2 { get; set; }
}
//create buttons in form c'tor
public Form1()
{
InitializeComponent();
ButtonCtrl button1 = new ButtonCtrl(1,2);
button1.Text = "dynamic 1";
button1.Click += new EventHandler(button_click);
button1.Top = 10;
this.Controls.Add(button1);
ButtonCtrl button2 = new ButtonCtrl(3, 4);
button2.Text = "dynamic 2";
button2.Click += new EventHandler(button_click);
button2.Top = 30;
this.Controls.Add(button2);
}
And the event handler
public void button_click(object sender,EventArgs e)
{
if(sender is ButtonCtrl)
{
ButtonCtrl btnCtrl= sender as ButtonCtrl;
label1.Text = btnCtrl.Arg1.ToString() + " " + btnCtrl.Arg2.ToString();
}
}

textbox focus check

I have a win app form with 3 text boxes and buttons as dial pad (it's a touchscreen app)...
When a dial pad button is pressed I want to check which one of these 3 text boxes has focus, and append text to it.
Something like:
if (tbx1.Focused == true)
{
tbx1.Text += "0";
}
else if (tbx2.Focused == true)
{
tbx2.Text += "0";
}
else
{
tbx3.Text += "0";
}
But this doesn't work... It appends text to tbx3 all the time.
Any suggestions?
Thanks :)
The problem arises when you click the button, the button will gain focus and not any of your textboxes.
What you can do is subscribe to the LostFocus event and remember what textbox had the focus last.
Something like:
private TextBox lastFocused;
private void load(object sender, EventArgs e){
foreach (TextBox box in new TextBox[] { txtBox1, txtBox2, txtBox3 }){
box.LostFocus += textBoxFocusLost;
}
}
private void textBoxFocusLost(object sender, EventArgs e){
lastFocused = (TextBox)sender;
}

Categories

Resources