I am using Tabcontrol and I created a class for all new Tabpages.
For examle when I open a new Tabpage the class creates controls and places them.
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
tp.Controls.Add(bttn1);
So my question is how can I check if this button is clicked?
Also my other question is the same with a Timer tick event.
You can easily attach to the button's Click event from the code:
bttn1.Click += new EventHandler(butt1_Click);
And here's the handler:
void button1_Click(object sender, EventArgs e)
{
// ...
}
Visual Studio will help you when you type the Click +=. After typing +=, hit the Tab key twice to get the handler.
I hope that you have created a UserControl for this or have sub-classed the TabPage class to create your controls. You should expose the Click event of the button from this newly created class through some new event you create:
public class MyTabPage : TabPage
{
private Button bttn1;
public event EventHandler Button1Clicked;
public MyTabPage()
{
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
bttn1.Click += bttn1_Click;
this.Controls.Add(bttn1);
}
void bttn1_Click(object sender, EventArgs e)
{
OnButton1Clicked();
}
protected virtual void OnButton1Clicked()
{
var h = Button1Clicked;
if (h != null)
h(this, EventArgs.Empty);
}
}
Now when you create an instance of MyTabPage, you can attach a handler to the Button1Clicked event:
MyTabPage page = new MyTabPage();
page.Button1Clicked += page_Button1Clicked;
tabControl.TabPages.Add(page);
...
void page_Button1Clicked(object sender, EventArgs e)
{
}
Related
I'm creating Buttons programmatically with a method and am wanting to attach a Click event handler. However, that data currently comes from a string parameter which can't be used with += RoutedEventHandler.
public Button CreateButton(string Display, string Name, string ClickEventHandler)
{
Button Btn = new Button
{
Content = Display,
Name = "Btn_" + Name
};
Btn.Click += new RoutedEventHandler(ClickEventHandler);
return Btn;
}
void Btn_save_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
// later
Button MyButton = CreateButton("Save", "save", "Btn_save_Click");
Error is RoutedEventHandler expects a Method and not a String. Is there a different approach to programmatically binding events that allows this sort of behaviour?
Thanks
From what I understand you wish to pass the method that should be executed when Click event is triggered. You could do something along the lines of:
Button button = CreateButton("Save", "save", (s, e) => SomeOnClickEvent(s, e));
Button button2 = CreateButton("Create", "create", (s, e) => SomeOtherOnClickEvent(s, e));
public Button CreateButton(string display, string name, Action<object, EventArgs> click)
{
Button b = new Button()
{
Content = display,
Name = $"Btn_{name}"
};
b.Click += new EventHandler(click);
return b;
}
void SomeOnClickEvent(object sender, EventArgs e)
{
}
void SomeOtherOnClickEvent(object sender, EventArgs e)
{
}
I am not entirely sure what you are trying to accomplish with this.
Here is an example of how to create an event at run time.
public void CreateButton()
{
Button Btn = new Button();
Btn.Click += new EventHandler(btn_Clicked);
}
private void btn_Clicked(object sender, EventArgs e)
{
// Your Logic here
}
In the Form1_Load method what code should I write to create a simple button?
private void Form1_Load(object sender, System.EventArgs e)
{
}
So that on Load the button would show.
As you said it is Winforms, you can do the following...
First create a new Button object.
Button newButton = new Button();
Then add it to the form inside that function using:
this.Controls.Add(newButton);
Extra properties you can set...
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
Your issue you're running to is that you're trying to set it on Form_Load event, at that stage the form does not exist yet and your buttons are overwritten. You need a delegate for the Shown or Activated events in order to show the button.
For example inside your Form1 constructor,
public Form1()
{
InitializeComponent();
this.Shown += CreateButtonDelegate;
}
Your actual delegate is where you create your button and add it to the form, something like this will work.
private void CreateButtonDelegate(object sender, EventArgs e)
{
Button newButton= new Button();
this.Controls.Add(newButton);
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
newButton.Location = new Point(20, 50);
}
on your eventload form put this code
private void Form1_Load(object sender, EventArgs e)
{
Button testbutton = new Button();
testbutton.Text = "button1";
testbutton.Location = new Point(70, 70);
testbutton.Size = new Size(100, 100);
testbutton.Visible = true;
testbutton.BringToFront();
this.Controls.Add(testbutton);
}
It's simple :
private void Form1_Load(object sender, System.EventArgs e)
{
Button btn1 = new Button();
this.Controls.add(btn1);
btn1.Top=100;
btn1.Left=100;
btn1.Text="My Button";
}
Let me tell you what I want to do.
1.there are two button in my form.One of them let button when I click it.Another button finish to create button.
bool active = false;
private void button1_Click(object sender, EventArgs e)
{
active = true;
}
private void button2_Click(object sender, EventArgs e)
{
active = false;
}
2.I will create button with mousedown event I want to set location of this buttons with coordinate like this.
Button button_create;
private void frm_tr_MouseDown(object sender, MouseEventArgs e)
{
if (active)
{
button_create = new Button();
button_create.Location = new Point(e.X + 5, e.Y - 15);
button_create.Size = new Size(75, 30);
button_create.Text = "Button";
this.Controls.Add(button_create);
button_create.MouseClick += new MouseEventHandler(button_create_MouseClick);
}
}
3.I started mouseclickevent of my created button.When I click this created button I will create new form textbox and button.
TextBox button_text;
void button_create_MouseClick(object sender, MouseEventArgs e)
{
Form frm = new Form();
frm.Show();
button_text = new TextBox();
Button accept = new Button();
accept.Location = new Point(frm.Width / 2, frm.Height / 2);
frm.Controls.Add(button_text);
frm.Controls.Add(accept);
accept.MouseClick += new MouseEventHandler(accept_MouseClick);
}
4.I started mouseclickevent of my last created button.I want to change text of my first created button.
void accept_MouseClick(object sender, MouseEventArgs e)
{
button_create.Text = button_text.Text;
}
5.I will click button1 and I click anywhere of this form new button will create and I also click button2 to finish create new button.I can change my created button with textbox but
If I click button1 and I create a new one button I cant change text of previous created button How can I do that?
I add buttons dynamically.. e.g. Button newButton=new Button();
Now i want each button to be triggered. So i wrote them the following events:
public void response_Click(object sender, EventArgs e)
{
}
public void edit_Click(object sender, EventArgs e)
{
}
public void quote_Click(object sender, EventArgs e)
{
}
Button quote = new Button();
Button reply = new Button();
Button edit = new Button();
quote.ID = "quote";
reply.ID = "reply";
edit.ID = "edit";
How do i trigger them, as soon as the user clicks on the button..will my functions above be triggered? do i need to do the following:
this.Clicked+=quote;
this.Clicked+=reply;
this.Clicked+=edit;
if i do need to do that..where do i put those lines of code?
i use visual studio 1010. asp.net
You can do like..
quote.Click += new EventHandler(quote_Click);
reply.Click += new EventHandler(response_Click);
edit.Click += new EventHandler(edit_Click);
Yes, you would need to register the event of the button and associate it with particular method.
for example.
myButton.Click += new EventHandler(Button_Click);
void Button_Click(object sender, EventArgs e)
{
}
I am creating one button on a page dynamically. Now I want to use the button click event on that button.
How can I do this in C# ASP.NET?
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);
//protected void button_Click (object sender, EventArgs e) { }
The easier one for newbies:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
Simply add the eventhandler to the button when creating it.
button.Click += new EventHandler(this.button_Click);
void button_Click(object sender, System.EventArgs e)
{
//your stuff...
}
It is much easier to do:
Button button = new Button();
button.Click += delegate
{
// Your code
};
You can create button in a simple way, such as:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
But event probably will not fire, because the element/elements must be recreated at every postback or you will lose the event handler.
I tried this solution that verify that ViewState is already Generated and recreate elements at every postback,
for example, imagine you create your button on an event click:
protected void Button_Click(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) != "true")
{
CreateDynamicElements();
}
}
on postback, for example on page load, you should do this:
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) == "true") {
CreateDynamicElements();
}
}
In CreateDynamicElements() you can put all the elements you need, such as your button.
This worked very well for me.
public void CreateDynamicElements(){
Button button = new Button();
button.Click += new EventHandler(button_Click);
}
Let's say you have 25 objects and want one process to handle any one objects click event. You could write 25 delegates or use a loop to handle the click event.
public form1()
{
foreach (Panel pl in Container.Components)
{
pl.Click += Panel_Click;
}
}
private void Panel_Click(object sender, EventArgs e)
{
// Process the panel clicks here
int index = Panels.FindIndex(a => a == sender);
...
}