Click here to Skip to main content
11,802,442 members (70,971 online)
Rate this: bad
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 1:35am
Edited 8-Mar-13 11:07am
Maciej Los202.3K
ThePhantomUpvoter at 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 at 8-Mar-13 17:10pm
Please, be more specific and provide more details...
kalisiddayya at 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 at 9-Mar-13 1:42am
It was basically clear. Please see my answer.
Sergey Alexandrovich Kryukov at 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?

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 at 9-Mar-13 12:38pm
Sergey Alexandrovich Kryukov at 9-Mar-13 19:56pm
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
0 OriginalGriff 465
1 KrunalRohit 366
2 F-ES Sitecore 295
3 Sergey Alexandrovich Kryukov 241
4 Afzaal Ahmad Zeeshan 230
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,645
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web02 | 2.8.151002.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