I am creating a program where it is required to have a list of buttons which relate to different things on the host computer.
I have achieved this as shown in the code below (example code):
Button btn = new Button();
btn.Name = "Button1";
btn.Text = "Item 1";
this.formLayoutProject.Controls.Add(btn);
This will eventually go into a for loop and more buttons will be on the flow layout, but i am starting small scale and working up so i don't get problems later.
This produces the button as expected (formatting will be done here too but haven't got that far).
What I am stuck on is how would I add a click event/right click event context menu onto the button?
I can put the click events in the code for buttons and items which will be fixed on the form and they work but I don't know how to do this dynamically as the buttons will be different on each target machine.
I have searched on Google but I haven't found what im looking for (probably due to the fact im not sure what the keywords are for the search).
How would I accomplish this?
You can add a button click event handler by using += operator on the event of your new btn object:
// Add 10 buttons dynamically.
// Bind to the same method.
for (var i = 1; i <= 10; i += 1)
{
Button btn = new Button();
btn.Name = "Button" + i;
btn.Text = "Item " + i;
this.formLayoutProject.Controls.Add(btn);
// Add handler.
btn.Click += btn_Click;
}
Now you just need to define the handler method:
private void btn_Click(object sender, EventArgs e)
{
// This is the button which was clicked.
var button = (Button)sender;
var buttonName = button.Name;
// Do some stuff.
// To detect certain mouse click events, cast e as a MouseClick.
MouseEventArgs mouseEvents = (MouseEventArgs)e;
// Now use the mouseEvents object to handle specific events based on the button clicked.
if (mouseEvents.Button == MouseButtons.Right)
{
// Right button clicked.
}
else if (mouseEvents.Button == MouseButtons.Left)
{
// Left button clicked.
}
}
Related
I am attempting to learn c# and wpf. I have a segment of code that works in that it shows the controls correctly. I also attempt to create a mouse button handler and when I debug it, the handler is never called. The buttons are nested within a couple of StackPanels, that can't seem to be the problem?
StackPanel tabPanel = new StackPanel();
for (int i = 0; i < 20; i++)
{
StackPanel micPanel = new StackPanel();
micPanel.Orientation = Orientation.Horizontal;
// Add the Calibration Button
Button micButton = new Button();
micButton.Height = 25;
micButton.Name = string.Format("Mic{0}", i);
micButton.Content = string.Format("Mic{0}", ((20 * index) + i));
micButton.MouseLeftButtonUp += new MouseButtonEventHandler(mic_MouseLeftButtonUp);
micPanel.Children.Add(micButton);
// Add the calibrated Value
Label micLabel = new Label();
micLabel.Height = 25;
micLabel.Content = string.Format("Value: {0}", ((20 * index) + i));
micPanel.Children.Add(micLabel);
tabPanel.Children.Add(micPanel);
}
tab.Content = tabPanel;
The handler looks like this:
private void mic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Button but = sender as Button;
}
I set a breakpoint and it never calls the handler?
This is typical: use the preview event handler to make sure it is raised first in the tree view.
micButton.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(mic_MouseLeftButtonUp);
Why don't you handle the Click event of the Button?
micButton.Click += new ClickEventHandler(mic_Click);
...
private void mic_Click(object sender, RoutedEventArgs e)
{
Button but = sender as Button;
}
Certain controls do "swallow" certain events but the Button control doesn't raise any MouseButtonUp event. It raises a Click event. The Click event is actually a combination of the LeftButtonDown and LeftButtonUp event:
WPF MouseLeftButtonUp Not Firing
You can read more about routed events and how they work in the documentation on MSDN: https://msdn.microsoft.com/en-us/library/ms742806%28v=vs.110%29.aspx
Code :
public partial class Form3 : Form
{
...
...
private void button1_Click(object sender, EventArgs e)
{
Panel p = new Panel();
TextBox diaryName = new TextBox();
Button b = new Button();
Label l = new Label();
diaryName.Font = new Font("Consolas", 12, FontStyle.Bold);
b.Font = buttonFont;
l.Font = buttonFont;
b.BackColor = Color.Wheat;
l.Text = "Diary Name : ";
b.Text = "Add Diary";
Point lbl = l.Location;
diaryName.Location = new Point(l.Location.X + l.Width + 5, lbl.Y);
Point txtbox = diaryName.Location;
b.Location = new Point(txtbox.X + diaryName.Width + 20, txtbox.Y);
p.Controls.Add(l);
p.Controls.Add(diaryName);
p.Controls.Add(b);
p.Location = new Point(12,272);
p.Size = new Size(20 + 20 + 20 + diaryName.Width + l.Width + b.Width, diaryName.Height);
// I need help here..
// b.Click += new EventHandler(); ???
this.Controls.Add(p);
this.Height += 50;
this.Width += 30;
this.FormBorderStyle = FormBorderStyle.Fixed3D;
}
...
}
The above code adds a panel that contains a label,a textBox and a button to the form , that's all working fine, my problem is that I want to handle click event of the dynamically added button (b) , In my event handling code I should be able to access the dynamically added TextBox (diaryName) for validation purposes, but I don't know how to do It. I tried adding another function within the same class Form3 , but since The textbox t is created within the button1_Click function, I am unable to access the textbox , so How can I get around this ?
I am new to c#, I have a Java background so is there any way in c# to declare event handlers like this
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the button");
}
});
You can easy assign a handler to the button's event this way:
b.Click += new EventHandler(newButtonClick);
where
protected void newButtonClick(object sender, EventArgs e)
{
//Access your textbox like this
var myTextBox = this.Controls.OfType<TextBox>().FirstOrDefault(tb=>tb.Name == "diaryName");
if(myTextBox!=null)
{
//rest of your code here
}
}
However it's a poor practice. Your button will depend heavily on the objects created dynamically somewhere outside - that breaks encapsulation rule of OOP. Secondly - did you think what will happen if you'll click your original button (the one you showed your handler for) twice?
edit: When I've come to think about it, your method is not that dynamic really. It creates those controls on the fly, but they're not generic in any way - it's a static piece of code, that creates always the same result. So in this case I'd think about putting your new panel, textbox and button in the form as a public items and then initialize them inside your method.
It'd be even better to create them in the visual studio's designer already, hide them using Visible properties and then in button1_Click you could only change their sizes and show them up.
Ok, need to add event for button click,
1) So after you create new Button
Button b = new Button();
2) next add click event for that
b.Click += new EventHandler(b_Click);
3) and then write the actual function body for click event in your code
void b_Click(object sender, EventArgs e)
{
//Your code for click operation
}
I have a set of buttons that are added dynamically. As the user keeps clicking the buttons, new buttons are added to the window. I am using a winforms. I am binding the onclick event of all these buttons to the same function. I am using the following code.
System.EventHandler myEventHandle= new System.EventHandler(this.Button_Clicked);
Once a new dynamic button is created I add the event handler with the following code:
b1.Click += myEventHandle;
Now in the function Button_Clicked() I want to get the Button which invoked this event. I want to disable this Button so that it cannot be clicked again and I want the name of the Button that was clicked as I want to do various actions depending on button name. I am newbie in C#.
This is what I have tried so far, but doesn't seem to work:
Button b = sender as System.Windows.Forms.Button;
b.Font = new Font(b.Font, FontStyle.Bold);
Console.WriteLine(""+b.Name);
b.Enabled = false;
Use the sender of the event
if(sender is Button)
{
Button b = sender as Button;
b.Enabled = false;
///something = b.Name;
}
Well, your Button_Clicked method must look like
void Button_Clicked(object sender, EventArgs e) {
Button clickedButton = (Button)sender;//if sender is always a Button
clickedButton.Enabled = false;
}
I'm on to a small project where I try to make my own web browser.
I found out that a web browser is worthless without "New Tabs"-function, so I thought that I could use buttons as tabs and every time I press "ctrl + T" a new button appears.
The problems I encountered is:
-Array of buttons in a way that makes it possible for me to spawn a new button every time I press "ctrl + T"
-When the button is spawned it should be clickable and disabled when clicked until another tab (button) is click.
At the moment I focus on getting 1 tab to work, so here's an example:
private void TB_Address_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
{
Button tabButton = new Button();
tabButton = new System.Windows.Forms.Button();
tabButton.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
tabButton.Cursor = System.Windows.Forms.Cursors.Hand;
tabButton.ForeColor = System.Drawing.Color.Lime;
tabButton.Location = new System.Drawing.Point(154, 32);
tabButton.Name = "tabButton";
tabButton.Size = new System.Drawing.Size(152, 23);
tabButton.TabIndex = 13;
tabButton.Text = "Tab 2";
tabButton.UseVisualStyleBackColor = false;
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Controls.Add(tabButton);
}
}
I also have this click function:
private void tabButton_Click(object sender, EventArgs e)
{
tab_1.Enabled = true;
tabButton.Enabled = false;
}
"tab_1" is a button created in the design mode.
"tabButton.Enabled" is red marked because it cannot find tabButton.
I understand why it cannot be found. But I have no idea about how to solve the problem in a good way.
You are assigning the tabButton_Click to all buttons with this line:
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Just cast the sender to button and you will get the button who fired the event:
void tabButton_Click(object sender, EventArgs e)
{
Button buttonSender = (Button) sender;
buttonSender.Enabled=false;
}
You are not finding "tab_1" because it is not a valid name inside the tabButton_Click scope.
That's why you have to cast the sender object to WindowsForms Button, and then change its properties.
I'm going with a different method.
Creating all the buttons needed initially.
Sorry for wasting your time.
I'm absolute beginner when it comes to mono for android.
I've used following code snippet to create 50 buttons programmatically:
for(int i=0;i<50;i++)
{
//code to calculate x and y position
btn=new Button(this);
//btn.SetBackgroundColor(Android.Resource.Color.);
btn.SetTextSize(Android.Util.ComplexUnitType.Sp,8);
btn.Text="Scrip "+i+"\n"+"CMP "+i+"\n"+"%Chg "+i;
lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams((width+30)/5, (height-10)/10));
btn.LayoutParameters=lp;
lp.SetMargins(leftMargin,topMargin, 0, 0);
main.AddView(btn);
}
String str="";
btn.Click += (sender, e) =>
{
str=btn.Text;
Toast.MakeText(this, "Selected="+str,ToastLength.Short).Show();
Console.WriteLine("Selected="+str);
};
But one big problem with this code is at the end of the loop, btn object has reference of the last button only.
So if any button other than last button is pressed, button click event is not fired.
How to resolve it?
Ideally, it should return the text of the clicked button.
Also, referring to below screenshot, the default button style doesn't look good here. So I want to change it to exact rectangle and not rounded-rectangel(the default one).
Any idea on this?
As I'm very new to this, any help will be appreciated !!
EDIT
Asa result of your help, I'm able to create and refer all buttons appropriately.
But how to set their style as exact rectangle??
Move btn.Click += (sender, e) subscription inside for loop.
Even better - create one named method instead of creating many anonymous. E.g. Button_Click and subscribe to it:
btn = new Button(this);
btn.Click = Button_Click;
Inside that method you can cast sender object to Button and know which button was clicked.
UPDATE: here is complete code
const int rowsCount = 10;
const int columnsCount = 5;
int buttonsCount = rowsCount * columnsCount;
for (int i = 0; i < buttonsCount; i++)
AddButton();
I prefer not to use magic numbers in code :)
private void AddButton()
{
Button button = new Button(this);
button.Click += Button_Click;
// add text and other properties
main.AddView(button);
}
private void Button_Click(object sender, EventArgs e)
{
Button button = (sender as Button);
// use clicked button e.g. Console.WriteLine("Selected = {0}", button.Text);
}
Your for loop operates on a button and creates a new instance of it every time, but your click event is only added once (it is outside your for loop). Therefore it will only be added to the last instance of Button that is put into btn (the last button from the for loop).
You need to either create each button separately (putting them in a List say) and then outside the for loop you will have a reference to each that you can add the click event to each button individually. Or (the much better way) add the click event inside the for loop, so that each button you create adheres to it. Bear in mind that since the event will then be reached by an arbitrary button(one of your 50) that it will be best to use the sender parameter to determine its text value.
i.e.:
btn.Click += (sender, e) =>
{
Button b = sender as Button;
if ( b == null ) return;
String str;
str=b.Text;
Toast.MakeText(this, "Selected="+str,ToastLength.Short).Show();
Console.WriteLine("Selected="+str);
};
You have created 50 buttons, so you need to have 50 references to it. The easiest way to achieve this is to create an array of buttons, like so:
Button[] btns = new Button[50];
for(int i=0;i<50;i++)
{
{
//code to calculate x and y position
btns[i]=new Button(this);
//btn.SetBackgroundColor(Android.Resource.Color.);
btns[i].SetTextSize(Android.Util.ComplexUnitType.Sp,8);
btns[i].Text="Scrip "+i+"\n"+"CMP "+i+"\n"+"%Chg "+i;
lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams((width+30)/5, (height-10)/10));
btns[i].LayoutParameters=lp;
lp.SetMargins(leftMargin,topMargin, 0, 0);
main.AddView(btn);
}
btns[i].Click += (sender, e) =>
{
String str= ( (sender as Button) != null) ? (sender as Button).Content.ToString() : "";
Toast.MakeText(this, "Selected="+str,ToastLength.Short).Show();
Console.WriteLine("Selected="+str);
}
}
//EDIT: You'll also need to create an eventhandler for each button
it's because you are setting the button click event outside the for loop. put it on the inside so it gets assigned to every button, not just the last one.