Click here to Skip to main content
Rate this: bad
good
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 Los151.6K
v2
Comments
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.
—SA
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?
—SA

1 solution

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

Solution 1

For example something like that:
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
  Permalink  
v2
Comments
Maciej Los at 9-Mar-13 12:38pm
   
+5!
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2014
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