Click here to Skip to main content
15,885,196 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i created a dynamic button in button click .how the dynamic button call a window when it is clicked


VB
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As                    System.Windows.RoutedEventArgs) Handles Button2.Click


  Dim button1 As System.Windows.Controls.Button = New System.Windows.Controls.Button()

      
   End Sub
Posted
Updated 8-Mar-13 11:07am
v2
Comments
[no name] 8-Mar-13 7:49am    
The answer to your question is, just like any other button, you assign a click handler and then in that click handler you do whatever is it that you want to do. The code you have presented here will never work. You create a button but do not do anything with it. It just goes out of scope and gets destroyed.
Maciej Los 8-Mar-13 17:10pm    
Please, be more specific and provide more details...
kalisiddayya 9-Mar-13 1:14am    
i want to create dynamic buttons.i want to add add handler to those dynamic buttons . Each dynamic button perform different function .how to call all add handler .
Sergey Alexandrovich Kryukov 9-Mar-13 1:42am    
It was basically clear. Please see my answer.
—SA
Sergey Alexandrovich Kryukov 8-Mar-13 17:31pm    
What does it mean, "call a window"? Windows is not a method, function, procedure, subroutine or operator...
Are you adding one button on click of another? Why? How will you add a first button? Why?
—SA

1 solution

For example something like that:
VB
Public Partial Class MyWindow
	Inherits Window

	Public Sub New()
		InitializeComponent()
	End Sub	'MyWindow

	Protected Overrides Sub OnContentRendered(e As EventArgs)
		MyBase.OnContentRendered(e)
		Dim myButton As New Button()
		myButton.Content = "_My Button" ' or something
		' let's assume you created some panel in XAML, for example, a grid,
		' and named it "myGrid"; then add a button:
		myGrid.Children.Add(myButton)
		' ... arrange it, etc.
		myButton.Click += Sub(sender As Object, eventArgs As RoutedEventArgs) 
                AddHandler myButton.Click, Sub(sender As Object, eventArgs As RoutedEventArgs)
                   ' ... call some method(s) from here, using sender, eventArgs, but usually not,
                   ' to handle the click event
                End Sub 'myButton.Click handler

	End Sub	'OnContentRendered

End Class 'class MyWindow


Of course, you can add a button on the event you want, even when the window is already showing.
I demonstrated the most convenient way to add a handler to the invocation list of a routed event instance, with anonymous delegate. Not only you won't need to write method which you don't use otherwise, but you can use handling method with the signature convenient for you, without passing both sender and eventArgs parameters if you don't need them.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 9-Mar-13 12:38pm    
+5!
Sergey Alexandrovich Kryukov 9-Mar-13 19:56pm    
Thank you, Maciej.
—SA

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