I have created a set of button and attach Click event and GotFocus event to them.
for (int i = 0; i < NumberOfQuestion; i++)
{
RadButton button = new RadButton();
// radButton1
//
button.Anchor = AnchorStyles.None;
button.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
button.Location = new Point(65 * i + 15, 10);
button.Name = "btn_cauhoi" + (i + 1);
button.Size = new Size(60, 35);
button.TabIndex = 1 + i;
button.Text = "Câu " + (i + 1);
button.Tag = (i + 1);
button.Click += Button_Click;
button.GotFocus += Button_Click; ;
//
panel_nut_cauhoi.Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
var button = (RadButton)sender;
var index = (int)button.Tag;
MessageBox.Show(index.ToString());
}
It triggers Click event correctly but with GotFocus event it trigger repeatly.
Somebody helps me, please.
Thanks in advances.
When you click ok on message box it loose focus and get focus again.
So if you delete MessageBox.Show() you will see its gonna trigger only one time, so you can test code like below, you will see the name of the button as btn_cauhoi1 or btn_cauhoi2 or btn_cauhoi3 up to which button you do click, it means its gonna trigger only one time.
var button = (RadButton)sender;
var index = (int)button.Tag;
//MessageBox.Show(index.ToString());
this.Text = button.Name;
Related
In my code the buttons are made automatically and I need to save the information from the button in the click event. I am coding a ShopSystem in WindowsForms and when I click a button (should also work like clicking 3 times) it should stand in a text box at the next form but I just need help at coding the clickevent.
while (id < artikelAnzahl)
{
Button ArtikelID = new Button
{
Location = new Point(posX, posY),
Size = new Size(100, 75),
};
posX += 120;
double s = double.Parse(id.ToString()) / 5;
if (int.TryParse(s.ToString(), out int i))
{
posY += 100;
posX = 70;
}
this.Controls.Add(ArtikelID);
foreach (var p in xmlArtikelliste.Descendants("Artikel"))
{
if (int.Parse(p.Attribute("ID").Value) == id)
{
ArtikelID.Text = p.Element("Name").Value + " " +
p.Element("Preis").Value + "€ " +
p.Element("Anzahl").Value + "Stk. ";
}
}
id++;
}
Edit: Adding Click event delegate will not do the trick since the buttons are generated in a while loop and the event handler's action will always have the values from the last loop iteration.
So, you can achieve what you need with the following:
Button's Tag property for storing the data
You can store Article ID from xmlArtikelliste.Descendants("Artikel"), or the whole article if you will:
ArtikelID.Tag = p.Attribute("ID")
Event handler which will be added to the Button's Click event.
Sample code:
ArtikelID.Click += (sender, e) =>
{
if(sender is Button button)
{
// Pass button.Tag.ToString() as parameter when navigating to the other form;
}
};
You can read more about it on the official docs.
Complete code:
while (id < artikelAnzahl)
{
Button ArtikelID = new Button
{
Location = new Point(posX, posY),
Size = new Size(100, 75),
};
posX += 120;
double s = double.Parse(id.ToString()) / 5;
if (int.TryParse(s.ToString(), out int i))
{
posY += 100;
posX = 70;
}
this.Controls.Add(ArtikelID);
foreach (var p in xmlArtikelliste.Descendants("Artikel"))
{
if (int.Parse(p.Attribute("ID").Value) == id)
{
ArtikelID.Text = p.Element("Name").Value + " " +
p.Element("Preis").Value + "€ " +
p.Element("Anzahl").Value + "Stk. ";
ArtikelID.Tag = p.Attribute("ID");
}
}
ArtikelID.Click += (sender, e) =>
{
if (sender is Button button && button.Tag != null)
{
// Pass button.Tag.ToString() as parameter when navigating to the other form;
}
};
id++;
}
Edit 1:
(sender, e) is a standard delegate signature for event handlers which executes an Action.
Ideally, what you could do is create a list of buttons, add button template and bind the data received from xmlArtikell to it. You can have a look at the following introductory tutorial on data binding in Win Forms.
Edit 2: Added solution to store the data in Button's Tag property, so it can be utilized later in the Click event handler. Otherwise, the action will always have the last while loop iteration variables to work with.
Hoping you can help - I have programmatically created button & richtextbox.
// Button to Edit
Button butEditToDo = new Button();
butEditToDo.Location = new Point(285, 10);
butEditToDo.Size = new System.Drawing.Size(25, 25);
butEditToDo.BackColor = Color.Transparent;
butEditToDo.FlatStyle = FlatStyle.Flat;
butEditToDo.FlatAppearance.BorderSize = 0;
butEditToDo.FlatAppearance.MouseOverBackColor = Color.FromArgb(244, 244, 244);
butEditToDo.Cursor = Cursors.Hand;
butEditToDo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Edit_25));
pnlPendingNote.Controls.Add(butEditToDo);
// Pending Nane + Tag
RichTextBox rxtNotes = new RichTextBox();
rxtNotes.Size = new System.Drawing.Size(317, 68);
rxtNotes.Location = new Point(3, 37);
rxtNotes.Text = (read["notNote"].ToString());
rxtNotes.ReadOnly = true;
rxtNotes.BorderStyle = BorderStyle.None;
rxtNotes.DetectUrls = true;
rxtNotes.BackColor = Color.FromArgb(244, 244, 244);
pnlPendingNote.Controls.Add(rxtNotes);
So when ever I click on ButEditToDo_Click - I can get the right button clicked.
So when I click on this button I would like to enable the RichTextbox - and when I click the button again - I would like to update the database.
Button Click:
private void ButEditToDo_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
for (int i = 1; i < pendingcounter; i++)
{
if (btn.Name == ("PenNote" + i))
{
break;
}
}
}
Hope you can help please with enabling the button, I'm all good with the database.
Thank you.
Edit One
#Ed - thank you.
Please See Image.
What I would like to achieve - When i click on the tools icon - the RichTextBox will be enabled.
So if I click on the tools on first panel - then the R_TextBox will be enabled for me to edit the text.
Then the Icon will change and I will be able to click on it again to save to the database.
Hope that makes more sense for you Ed.
Just give the button an event handler that does stuff. Use a lambda so you can reference the local reference to the RichTextBox.
Button butEditToDo = new Button();
// ...snip...
RichTextBox rxtNotes = new RichTextBox();
// ...snip...
butEditToDo.Click += (sender, args) =>
{
CycleNoteState(rxtNotes);
};
And here's the guts of the event handler. You could put this all in the event handler, but the code's more readable this way. CycleNoteState isn't a very good name, but I'm not clear about the semantics of your program.
I may have misunderstood the logic for what the button does on successive clicks. If it's more complicated than this, you can introduce a state enum or something. Let me know and we'll get it figured out.
private void CycleNoteState(RichTextBox rtb)
{
if (!rtb.Enabled)
{
rtb.Enabled = true;
}
else
{
// Do save stuff here
}
}
i'm really new to C# and i been thinking why the label text changed when i set it in a different value without the help of a event handler?
Here's the my code :
class Main_Program : Form
{
Button btnAttack = new Button();
Label[] lblEnemyInfo = new Label[4];
public Main_Program()
{
btnAttack.Text = "ATTACK";
btnAttack.Location = new Point(350, 450);
btnAttack.Width = 150;
btnAttack.Height = 50;
btnAttack.FlatStyle = FlatStyle.Popup;
btnAttack.MouseClick += new MouseEventHandler(btnAttack_MouseClick);
for (short i = 0; i < 4; i++)
lblEnemyInfo[i] = new Label();
string enemyHealth = "Health : " + enemy.Health; //I'm going set the values on label 2
lblEnemyInfo[1].Text = enemyHealth;
lblEnemyInfo[1].Font = new Font("Segoi UI", 12, FontStyle.Italic);
lblEnemyInfo[1].Location = new Point(500, 50);
for (short i = 0; i < 4; i++)
Controls.Add(lblEnemyInfo[i]);
}
private void btnAttack_MouseClick(Object sender, EventArgs e)
{
short totalDamage = totalDamageDeal(enemy.Armor, player.Attack);
string log = "You Attack the enemy, you deal " + totalDamage + " damage";
enemy.Health -= totalDamage;
string result = "Health : " + enemy.Health;
lblEnemyInfo[1].Text = result;
lblEnemyInfo[1].TextChanged += new EventHandler(lblEnemyInfo_TextChanged); //The label's text changed even without the eventhandler
}
private void lblEnemyInfo_TextChanged (object sender, EventArgs e)
{
//i don't know what statements to put here
}
}
An EventHandler is used to execute code when an event happens. If you don't want anything to happen, then don't use an event handler.
In your case, lblEnemyInfo is a Label. It has a property called Label.Text (of type string). You can change this value to update the string that the Label displays. Using lblEnemyInfo[1].Text = "Health Down"; will make the corresponding Label display the value "Health Down" whether or not an event handler is associated with it.
You are setting an event handler for the Label with this code :
lblEnemyInfo[1].TextChanged += new EventHandler(lblEnemyInfo_TextChanged);
That means "whenever this Label's Text changes, call the function named lblEnemyInfo_TextChanged".
There is good documentation on event handling in this link (MSDN docs).
I was wondering how to go about doing something such as this:
I need to create a Form with a specific number of buttons based on an integer value representing the number of buttons needed, then give them their own specific names so that each can have their own unique event handlers.
A real example I can think of doing this would be the Windows log-in screen, where the number of controls created is based on the number of users and whether there is a Guest account or not. How do you think that they programmed that?
Thank you.
for (int i = 0; i < 5; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = "Button #" + i.ToString();
newButton.Location = new Point(32, i * 32);
newButton.Click += new EventHandler(button1_Click);
this.Controls.Add(newButton);
}
private void button1_Click(object sender, EventArgs e)
{
if (((Button)sender).Name == "button0")
MessageBox.Show("Button 0");
else if (((Button)sender).Name == "button1")
MessageBox.Show("Button 1");
}
Somehow you have to define the names of all the buttons. I would suggest you to create a new string array and write the button names inside, and then use them in buttons creation loop:
//do the same length as the for loop below:
string[] buttonNames = { "button1", "button2", "button3", "button4", "button5" };
for (int i = 0; i < buttonNames.Lenght; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = buttonNames[i]; //each button will now get its own name from array
newButton.Location = new Point(32, i * 32);
newbutton.Size = new Size(25,100); //maybe you can set different sizes too (especially for X axes)
newButton.Click += new EventHandler(buttons_Click);
this.Controls.Add(newButton);
}
private void buttons_Click(object sender, EventArgs e)
{
Button btn = sender as Button
MessageBox.Show("You clicked button: " + btn.Text + ".");
}
I have an array of button which is dynamically generated at run time. I have the function for button click in my code, but I can't find a way to set the button's click name in code. So,
what is the code equivalent for XAML:
<Button x:Name="btn1" Click="btn1_Click">
Or, what should I place for "????" in the following Code:
Button btn = new Button();
btn.Name = "btn1";
btn.???? = "btn1_Click";
Button btn = new Button();
btn.Name = "btn1";
btn.Click += btn1_Click;
private void btn1_Click(object sender, RoutedEventArgs e)
{
// do something
}
The following should do the trick:
btn.Click += btn1_Click;
// sample C#
public void populateButtons()
{
int xPos;
int yPos;
Random ranNum = new Random();
for (int i = 0; i < 50; i++)
{
Button foo = new Button();
Style buttonStyle = Window.Resources["CurvedButton"] as Style;
int sizeValue = ranNum.Next(50);
foo.Width = sizeValue;
foo.Height = sizeValue;
foo.Name = "button" + i;
xPos = ranNum.Next(300);
yPos = ranNum.Next(200);
foo.HorizontalAlignment = HorizontalAlignment.Left;
foo.VerticalAlignment = VerticalAlignment.Top;
foo.Margin = new Thickness(xPos, yPos, 0, 0);
foo.Style = buttonStyle;
foo.Click += new RoutedEventHandler(buttonClick);
LayoutRoot.Children.Add(foo);
}
}
private void buttonClick(object sender, EventArgs e)
{
//do something or...
Button clicked = (Button) sender;
MessageBox.Show("Button's name is: " + clicked.Name);
}
I don't think WPF supports what you are trying to achieve i.e. assigning method to a button using method's name or btn1.Click = "btn1_Click". You will have to use approach suggested in above answers i.e. register button click event with appropriate method
btn1.Click += btn1_Click;
You should place below line
btn.Click = btn.Click + btn1_Click;