Add eventhandler to a dynamically created button control
Last post 05-17-2008 6:09 AM by feddas. 4 replies.
Sort Posts:
05-15-2008 7:28 AM
Add eventhandler to a dynamically created button control

Hi!

Does anyone know how an event handler is added to a dynamically created button control. I have button which should be able to add new buttons once clicked. The new button needs to be movable using drag and drop. HELP!!!! ERik

inga2003

Loading...
Joined on 05-15-2008
Posts 1
05-15-2008 9:59 AM
Marked as Answer
Re: Add eventhandler to a dynamically created button control

Hi,

hope that helps:
 

 

  Private uc As New MyUserControl 

  Public Sub CreateUserControl
    AddHandler uc.Fire, AddressOf HandleMyUserControlEvent
  End Sub

  Private Sub HandleMyUserControlEvent(ByVal sender As System.Object)
   ...
  End Sub

  Public Sub DeleteUserControlHandler
    RemoveHandler uc.Fire, AddressOf HandleMyUserControlEvent
  End Sub 

 


Stefan

stefan.josten

Loading...
Joined on 04-29-2008
Germany
Posts 15
05-15-2008 4:02 PM
Re: Add eventhandler to a dynamically created button control

Hi!
I have watch you video (very good) but I'm trying to add an MouseLeftButtonDown, MouseLeftButtonUp and MouseMove event handlers to a dynamcally created button  iin order to enable drag and drop.

I write:

 void addButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = new Button();
            b.Content = "TEST";
            b.Width = 100;
            b.Height = 20;

            b.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
            b.MouseLeftButtonUp += new MouseButtonEventHandler(Page_MouseLeftButtonUp);
            b.MouseMove += new MouseEventHandler(Page_MouseMove);
          
            LayoutRoot.Children.Add(b);
        }

The event handlers works fine for a button i declare in xaml. The difference is that the button declared in xaml has a x:name and the code behind has not (can't be done)

Any ideas!

Regards Erik

erikliffner

Loading...
Joined on 02-05-2008
Posts 10
05-16-2008 11:47 AM
Marked as Answer
Re: Add eventhandler to a dynamically created button control

 Button b = new Button();
 Button.SetValue(Button.NameProperty, "Button1");  // this is how you set the button name in code

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Loading...
Joined on 03-05-2008
Indiana, US
Posts 1,534
05-17-2008 6:09 AM
Marked as Answer
Re: Re: Add eventhandler to a dynamically created button control

I asked this question on a comment on a developers blog and Erik saw it.  He e-mailed me about it.  I discovered the problem is the type of the parent control.

The problem with the code above is the type of "LayoutRoot".  If it is a Grid it will not allow absolute positioning.  It needs to be a Canvas to enable dragging of the control.  If you need LayoutRoot to be a grid for other reasons you can create a new Canvas and add the button to that.  if you named it "myCanvas" you'd use: myCanvas.Children.Add(b)

A dynamic fix that would probably work would be:
void addButton_Click(object sender, RoutedEventArgs e)
{
  Button b = new Button();
  
Canvas cva = new Canvas(); 
  b.Content = "TEST";
  b.Width = 100;
  b.Height = 20;

  b.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
  b.MouseLeftButtonUp += new MouseButtonEventHandler(Page_MouseLeftButtonUp);
  b.MouseMove += new MouseEventHandler(Page_MouseMove);

  cva.Children.Add(b); 
  LayoutRoot.Children.Add(b);
}

I mention it in comments here in slightly different wording: http://silverlight.net/blogs/videos/archive/2008/05/06/50561.aspx#51563

feddas

Loading...
Joined on 10-29-2007
Posts 3