locked
Click event of a control in a device template RRS feed

  • Question

  • User731069546 posted

    If the mobile button control is on a form, I can just add the click event and it works. Since my mobile button control is in a device specific template, how do I handle it's click event? The environment doesn't know about my control so I just can't write something like

    btnShip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShip.Click

    I finally figured out how to find the control in the template but when I try to use addhandler command, nothing happens when user clicks on the button.

     

    Monday, March 26, 2007 2:10 PM

Answers

  • User731069546 posted

    OK, here is the solution. First you need to find the control. Once you have it's handle, you can use AddHandler statement to bind the event to your procedure. But you have to rememebr to put this code in Page_Init function.

    In your code behind class, delare something like:

    Protected myButton as MobileControls.Command

    Then in the Page_Init, do this

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

    InitializeComponent()

    myButton = GetControl("ID_OF_THE_BUTTON_FIND", Me)

    If (Not myButton Is Nothing) Then

    AddHandler myButton.Click, AddressOf myButton_Click

    End If

    End Sub

    Here is the button click event handler

    Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Me.Label1.Text = "Button clicked"

    End Sub

    GetControl just finds the control in a device template given the control's ID. Here is the code that I found on the web.

    Private Function GetControl(ByVal ID As String, ByRef cStart As Control)

    Dim c As Control, oRet As Control

    Dim t As Type = GetType(MobileControls.TemplateContainer)

    If cStart.HasControls() Then

    For Each c In cStart.Controls

    If oRet Is Nothing Then

    If t.IsInstanceOfType(c) Then

    oRet = c.FindControl(ID)

    Else

    If c.HasControls() Then

    oRet = GetControl(ID, c)

    End If

    End If

    End If

    Next

    End If

    Return oRet

    End Function

    Now there is another way this can be done.

    Protected WithEvents myButton As MobileControls.Command

    Now in the Page_Init function

    myButton = GetControl("ID_OF_THE_BUTTON_FIND", Me)

    No need to use AddHandler

    but you need to change the Event Handling procedure to this:

    Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton.Click

    Me.Label.Text = "Button clicked"

    End Sub

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 30, 2007 12:34 PM

All replies

  • User731069546 posted

    Anyone?

    Wish MS documentation had more examples. The support on this forum is sparse. Lots of views but no solutions. Sorry to say this.

    Tuesday, March 27, 2007 11:15 AM
  • User731069546 posted

    OK, here is the solution. First you need to find the control. Once you have it's handle, you can use AddHandler statement to bind the event to your procedure. But you have to rememebr to put this code in Page_Init function.

    In your code behind class, delare something like:

    Protected myButton as MobileControls.Command

    Then in the Page_Init, do this

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

    InitializeComponent()

    myButton = GetControl("ID_OF_THE_BUTTON_FIND", Me)

    If (Not myButton Is Nothing) Then

    AddHandler myButton.Click, AddressOf myButton_Click

    End If

    End Sub

    Here is the button click event handler

    Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Me.Label1.Text = "Button clicked"

    End Sub

    GetControl just finds the control in a device template given the control's ID. Here is the code that I found on the web.

    Private Function GetControl(ByVal ID As String, ByRef cStart As Control)

    Dim c As Control, oRet As Control

    Dim t As Type = GetType(MobileControls.TemplateContainer)

    If cStart.HasControls() Then

    For Each c In cStart.Controls

    If oRet Is Nothing Then

    If t.IsInstanceOfType(c) Then

    oRet = c.FindControl(ID)

    Else

    If c.HasControls() Then

    oRet = GetControl(ID, c)

    End If

    End If

    End If

    Next

    End If

    Return oRet

    End Function

    Now there is another way this can be done.

    Protected WithEvents myButton As MobileControls.Command

    Now in the Page_Init function

    myButton = GetControl("ID_OF_THE_BUTTON_FIND", Me)

    No need to use AddHandler

    but you need to change the Event Handling procedure to this:

    Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton.Click

    Me.Label.Text = "Button clicked"

    End Sub

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 30, 2007 12:34 PM