Click here to Skip to main content
11,928,919 members (48,173 online)
Rate this:
Please Sign up or sign in to vote.
See more: WPF
i created a dynamic button in button click .how the dynamic button call a window when it is clicked

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 8-Mar-13 2:35am
Edited 8-Mar-13 12:07pm
Maciej Los210.1K
ThePhantomUpvoter 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 .
It was basically clear. Please see my answer.
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?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

For example something like that:
Public Partial Class MyWindow
	Inherits Window
	Public Sub New()
	End Sub	'MyWindow

	Protected Overrides Sub OnContentRendered(e As EventArgs)
		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:
		' ... 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.

Maciej Los 9-Mar-13 12:38pm
Thank you, Maciej.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100