Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am trying to do the addition of two no and i am create code for both 3 textbox and a add button its working runtime but how do i generate a click event of my runtime created button please help me ?
Posted
Comments
Suvabrata Roy 10-Oct-14 0:19am    
I did not get you what you want to say/what is your problem? Please explain briefly

protected void Page_Load(object sender, EventArgs e)
        {
            Button testButton = new Button();
            testButton.Text = "pleaseClick";
            testButton.Click += new EventHandler(button_Click);
            form1.Controls.Add(testButton);
         
         
        }

code on click event 
protected void button_Click (object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(GetType(), "alert", "alert('Hi its a demo');", true);
    }
 
Share this answer
 
v2
Comments
jagdish vaghela 10-Oct-14 1:45am    
it's working in windows form application but it can't be working in windows phone 8 app in mainpage.xaml.cs help for this
VC.J 10-Oct-14 1:48am    
jagdish I am not Window phone developer so in that case I can't help you sorry about that
jagdish vaghela 10-Oct-14 1:58am    
sorry but its can't run by me in the visual studio 2012 C# form application
jagdish vaghela 10-Oct-14 1:59am    
the error of the page is listed here
"Error 1 The name 'Btn_Click' does not exist in the current context C:\Users\jrv72\Documents\Visual Studio 2012\runtimebutton\runtimebutton\Form1.cs 24 41 runtimebutton
"
jagdish vaghela 10-Oct-14 2:04am    
ohhh i get it now with change something ...
Please check the below code.. for you reference

C#
....
Button cmd = new Button("Click");
cmd.Click += new EventHandler(button1_Click);

....
private void button1_Click(object sender, System.EventArgs e)
{
Message.Show("Hi");
}


Please refer below link

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click%28v=vs.110%29.aspx[^]

http://stackoverflow.com/questions/16347072/c-sharp-create-dynamic-buttons-and-onclick-dynamic-eventhandlers[^]

Also read events and delegates
 
Share this answer
 
C#
private void Form1_Load(object sender, EventArgs e)
{
    Button n = new Button();
    n.Text = "click me";
    n.Click += n_Click;
    this.Controls.Add(n);
}

void n_Click(object sender, EventArgs e)
{
    MessageBox.Show("hello");
}
 
Share this answer
 
Comments
jagdish vaghela 10-Oct-14 2:09am    
its run in visual studio 2012
BillWoodruff 10-Oct-14 5:45am    
Does it work in WinPhone8 ? That was your original question.
jagdish vaghela 10-Oct-14 6:09am    
ya realy its working with windows phone 8 ..
i will submit working code for windows phone 8
jagdish vaghela 10-Oct-14 6:11am    
this is the code which i used in my windows phone 8 app
public MainPage()
 {
     InitializeComponent();
     Button mybtn = new Button();
     mybtn.Name = "Meow";
     mybtn.Content = "Meow";
     mybtn.Margin = new Thickness(210,0,0,0);
     mybtn.Background = new SolidColorBrush(Colors.Red);
     mybtn.Height = 200;
     mybtn.Width = 200;
     mybtn.VerticalAlignment = VerticalAlignment.Top;
     mybtn.HorizontalAlignment = HorizontalAlignment.Right;
     mybtn.Click+=mybtn_Click;
     ContentPanel.Children.Add(mybtn);
 }
 private void mybtn_Click(object sender, RoutedEventArgs e)
 {
     MediaElement.Stop();
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900