C#form app making events on buttons from backend - c#

I'm working on a c# winforms application.I have created buttons from the back end, like the following:
Button b = new Button();
How can I attach a click to each button that is created from the back end, that each button when clicked, a picture should become visible.

You can register events like this:
Button b = new Button();
b.Click += customButton_Click;
public void customButton_Click(object sender, EventArgs e)
{
// code here
}
Here the += operator will helps you to register the event handlers.

Related

How to properly assign events to dynamically created buttons in c# Winforms?

My intention is to create buttons at runtime and have a click event handler subscribed to them. Once the dynamically created button is clicked, the click eventhandler is unsubscribed, such that the click event only fires once.
At runtime the desired behaviour only works if I create one button and click it immediately . If I create more than one button, than only the last created button unsubscribes from the click event. Did I miss something?
public partial class Form1 : Form
{
Button b;
int counter;
public Form1()
{
InitializeComponent();
}
// create more buttons
private void button1_Click(object sender, EventArgs e)
{
b = new Button();
b.Size = new Size(50, 50);
b.Click += b_Click; // dynamic button click event
this.Controls["flowLayoutPanel"].Controls.Add(b);
}
// dynamic button click eventhandler
void b_Click(object sender, EventArgs e)
{
b.Text = counter.ToString();
b.Click -= b_Click;
counter++;
}
}
Because b member will reference last created dynamic button, so clicking any buttons will remove click event handler of currently referenced button in b variable, which would be last created.
Use sender to access instance of "current" button and remove click event handler only from "current" button.
void b_Click(object sender, EventArgs e)
{
var button = sender As Button;
button.Text = counter.ToString();
button.Click -= b_Click;
counter++;
}
Don't use private member for dynamic button, but local variable
private void button1_Click(object sender, EventArgs e)
{
var button = new Button();
button.Size = new Size(50, 50);
button.Click += b_Click;
this.Controls["flowLayoutPanel"].Controls.Add(button);
}
If you need to reference collection of created button somewhere, you can access them from the controls of flow panel where buttons were added
var dynamicButtons = .
this.Controls["flowLayoutPanel"].Controls.OfType<Button>().ToList();
Or save them to the dedicated collection (in case flow panel has other buttons)

Button click event not responding c#

The problem we are having is accessing the click event for a button which is created in the click event of another button i.e. clicking the first button generates a new panel and controls, and we now want the button on this newly created panel to perform an action.
The controls have been declared at the top of the class as follows:
Panel createElementPage = null;
TextBox elementDescription = null;
TextBox elementName = null;
Button continueButton = null;
AuditSystem audit;
Here is an excerpt of the method that generates the new panel, the part that defines the continueButton is written as follows:
public void CE_Click(object sender, EventArgs e)
{
createElementPage.Controls.Add(elementDescription);
continueButton = new Button();
continueButton.Text = "Continue";
continueButton.Location = new Point(700, 500);
continueButton.Size = new Size(100, 50);
createElementPage.Controls.Add(continueButton);
}
We want to access the continueButton's click event handler but the method we have written does not seem to be working. This is what we have so far:
private void continueButton_Click(object sender, EventArgs e)
{
Console.WriteLine(" something");
}
Clicking the button yields no results, and we have tried a few solutions such as implementing a seperate eventHandler method. Does anybody have a fix for this?
You have to actually subscribe to the event:
continueButton.Click += continueButton_Click;
Events need to be told what they should handle. Without that, they won't "listen" to anything.
Friendly note: be careful when adding handlers "on demand" like this (i.e. outside of the designer). It doesn't really apply here (you have a new button each time), but it's fairly easy to accidentally subscribe to a control's event multiple times, and your handler will fire multiple times as a result. It's just nice to be aware of :)

C# - Associate panel to button when created programmatically

I wanna make an example application in C# to show to my classmates(I'm 10th grade) how would a Wireless Device Controller Interface work. I know how to write most of the program, but I don't know how to do one thing.
I wanna create a button programmatically and once it is created, associate to it a panel that will show and hide when that button is clicked. Can someone help me?
I forgot to tell you something. The panel needs to be created programmatically too.
Create panel:
var panel = new Panel();
this.Controls.Add(panel);
Create button:
var button = new Button();
this.Controls.Add(button);
Add event handler to button:
button.Click += (o,e) =>
{
panel.Visible = !panel.Visible;
};
Is the Panel also created dynamically? – Idle_Mind
#Idle_Mind yes, it is. I forgot to mention it – DannyDSB Official
The easiest way is to simply store a reference to the Panel in the Tag() property of the Button. Here's a silly example:
private void button1_Click(object sender, EventArgs e)
{
Panel pnl = new Panel();
pnl.BorderStyle = BorderStyle.FixedSingle;
pnl.BackColor = Color.Red;
Button btn = new Button();
btn.Text = "Toggle Panel";
btn.Tag = pnl;
btn.Click += delegate {
Panel p = (Panel)btn.Tag;
p.Visible = !p.Visible;
};
flowLayoutPanel1.Controls.Add(btn);
flowLayoutPanel1.Controls.Add(pnl);
}
First you need the name of the panel. You need to set its visibility property to change the visibility (duh.)
To do that on a button click, you need to attach an event handler to it. Let's first assume that you called your newly created button "myButton" for simplicities sake.
First you create the handler function
void myButton_Click(object sender, RoutedEventArgs e){
panel.visibility = whatever;
}
and later assign the function to the click handler with
myButton.Click += myButton_Click;

Create a submit button when I press Add, multiple events under one button

Any idea how I could make a click event create another button with different click event?
I have a WPF app to make using EF. So I'm stuck at the part where I need to press button "Add" which will freeze other buttons and then create another button "Submit" with code for adding data to the table. I have tried some advice from msdn, but it doesn't work. Here is the code (previously in XAML added a button named b1):
public partial class RoutedEventAddRemoveHandler {
void MakeButton(object sender, RoutedEventArgs e)
{
Button b2 = new Button();
b2.Content = "New Button";
// Associate event handler to the button. You can remove the event
// handler using "-=" syntax rather than "+=".
b2.Click += new RoutedEventHandler(Onb2Click);
root.Children.Insert(root.Children.Count, b2);
DockPanel.SetDock(b2, Dock.Top);
text1.Text = "Now click the second button...";
b1.IsEnabled = false;
}
void Onb2Click(object sender, RoutedEventArgs e)
{
text1.Text = "New Button (b2) Was Clicked!!";
}
I even tried the most obvious solution to simply create another button with click event directly in click event.
I would recommend an alternative approach and put the submitbutton in your xaml code right away but make it so that it is invisible and disabled.
Then in the event handler you simply have to make it visible and enable it.
Your event handler that handles the submit, the dynamic creation of the button, hooking it in the form and such can all be avoided and don't have to be done at runtime.
This will result in a lot better readable code and maintainable code than your original approach unless you have a very good reason for it.
I have done the following coding and it is working for me
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
Button oButton = new Button();
oButton.Name = "btnMessage";
oButton.Content = "Message Show";
oButton.Height = 50;
oButton.Width = 50;
oButton.Click += new RoutedEventHandler(oButton_Click);
//root is a stack panel
root.Children.Insert(root.Children.Count, oButton);
DockPanel.SetDock(oButton, Dock.Top);
}
void oButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello World !");
}

Access control properties from its dynamically created Event Handler

I am developing a C# WinForms application. On the FormLoad event, I dynamically create and add to the form 100 buttons without text, but with names like button1, button2 etc. Also, onto these buttons, after their creation, I dynamically link a unique event handler for the ButtonClick event. How can I access the button's properties from within the event handler (more specifically the button name)?
If I use this.Name, I get the name of the form, and not the name of the button.
Later Edit: (for those who might wonder here in search of solutions)
private void function1()
{
Button a = new Button();
a.Name = "button" + (i * j).ToString();
a.Click += new EventHandler(OnFieldButtonClicked);
}
private void OnFieldButtonClicked(object sender, EventArgs e)
{
Button button = (Button)sender;
MessageBox.Show(button.Name);
}
The sender argument is the event handler encapsulates an instance of the object that has triggered the event:
Button button = (Button) sender;
String text = button.Text;

Categories

Resources