Results 1 to 7 of 7

Thread: Adding Controls at Runtime problem

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Massachusetts
    Posts
    48

    Adding Controls at Runtime problem

    I have an odd problem that is really frustrating. I've created buttons at runtime and set up the handlers. When the user clicks the created buttons, I read the sender.text and respond to it. See below....

    If sender.text.ToString = WSAnswer Then
    MessageBox.Show("Yes")
    PlayWhatSays()
    Else
    MessageBox.Show("Nope" & " " & WSAnswer & " " & sender.text.ToString)
    End If

    The code works well except when I click the last button created. When I do that, for some strange reason it works as planned once then for some reason the program runs the event for a second time as if the user clicked the button twice. This occurs only when I click the last button created and happens for both a correct and an incorrect answer. I have walked through this a number of time and I know that I haven't created 2 buttons on the same position. Also the PlayWhatSays event does not trigger the created buttons click event. Any help?
    Thanks

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Post your code .

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Massachusetts
    Posts
    48

    Add controls at runtime poroblem

    Here is the code to create and remove the buttons and below that is the button click

    Private Sub AddWhatSaysButtons()
    'this sub adds what says text boxes and fills them
    Dim z As Integer
    Dim i As Integer
    Dim position As Point
    Dim iTop As Integer = 24
    Dim iLeft As Integer = 16
    Dim iTextBox As Integer = clsQD.WhatSaysCount - 1
    Dim iStandardWidth As Double = 54
    Dim iStandardHeight As Double = 74
    Dim fnt As New Font("Microsoft Sans Serif", 12, FontStyle.Regular)

    Try
    For z = 0 To Me.Controls.Count - 1
    If Me.Controls(z).GetType Is GetType(System.Windows.Forms.Panel) Then
    If Me.Controls(z).Name.ToString = "pnlWhatSays" Then
    PanelWSIndex = z 'holds the number of the control
    For i = 0 To iTextBox
    Select Case i
    Case Is = 0
    position.X = 16
    position.Y = 24
    Case Is = 10
    position.X = 16
    position.Y = 64
    Case Is = 20
    position.X = 16
    position.Y = 104
    Case Is = 30
    position.X = 16
    position.Y = 144
    End Select
    BTN = New Button
    With BTN
    .Size = New Size(54, 34)
    .Location = position
    .Name = "TB" & i.ToString
    .Tag = "Added"
    .Text = clsQD.TheWhatSaysLetter(i)
    .Font = fnt
    .BackColor = Color.Yellow
    End With
    position.X += 68
    Me.Controls(PanelWSIndex).Controls.Add(BTN)
    AddHandler BTN.Click, New System.EventHandler(AddressOf BTN_Click)
    Next
    Exit For
    End If
    End If
    Next
    Catch ex As Exception

    End Try


    End Sub
    Private Sub RemoveWhatSaysButtons()
    Dim z As Integer
    Dim i As Integer
    Dim ctlRemove() As Control

    'this removes the buttons and handlers when the user selects a new help word
    Try
    If Me.Controls(PanelWSIndex).Controls.Count > 2 Then 'change when control count changes
    ReDim ctlRemove(Me.Controls(PanelWSIndex).Controls.Count - 2)
    For i = 0 To Me.Controls(PanelWSIndex).Controls.Count - 1
    If Not Me.Controls(PanelWSIndex).Controls(i).Tag = "Keep" Then
    ctlRemove(z) = Me.Controls(PanelWSIndex).Controls(i)
    z += 1 'only move up ctlRemove tally
    End If
    Next

    For z = 0 To UBound(ctlRemove) - 1
    Me.Controls(PanelWSIndex).Controls.Remove(ctlRemove(z))
    Console.Write(ctlRemove(z).Name)
    RemoveHandler BTN.Click, AddressOf BTN_Click
    Next
    End If

    Catch
    UnhandledExceptionHandler()
    End Try

    End Sub

    Private Sub BTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN.Click
    If sender.text.ToString = WSAnswer Then
    MessageBox.Show("Yes")
    PlayWhatSays()
    Else
    MessageBox.Show("Nope" & " " & WSAnswer & " " & sender.text.ToString)
    End If

    End Sub

  4. #4
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    The event handler would fire twice if you have used addHandler for the same button twice.

    Sorry, don't really have time to debug your code, but I was wondering why you use:

    AddHandler BTN.Click, New System.EventHandler(AddressOf BTN_Click)

    instead of

    AddHandler BTN.Click, AddressOf BTN_Click

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Massachusetts
    Posts
    48
    PeteD:

    My real job is as a special ed teacher so I'm kind of self taught on VB.Net ---I adapted some code for the add handler from a vb.net book I purchased--either VB.Net black book or Mastering VB.Net--anyway the addhandler code has worked in the past and I have no problem adding controls (Text Boxes and/or Label---this is the first time I have created buttons at runtime)---the weirdness I have is caused just when I click on the last button created at runtime. It works perfectly when any of the other buttons are clicked. ---I need the functionality of Buttons so any help with this problem would be appreciated.

    Thanks,
    Rick

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i just quickly put you an example together, creating 1 textbox and 1 button at runtime ( on the click of an exsisting button ) and then adding the handling to make them operational for certain events.
    VB Code:
    1. Private WithEvents txtBox As TextBox
    2.     Private WithEvents btn As Button
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         '/// a new textbox.//////////////
    6.         Dim tb As New TextBox()
    7.         tb.Location = New Point(120, 50)
    8.         tb.Visible = True
    9.         Controls.Add(tb)
    10.         '////////////////////////////////
    11.         '/// a new button.
    12.         Dim btn1 As New Button()
    13.         btn1.Location = New Point(120, 90)
    14.         btn1.Text = "new button"
    15.         btn1.Visible = True
    16.         Controls.Add(btn1)
    17.         '////////////////////////////////
    18.         '/// now lets handle the new controls....
    19.  
    20.         AddHandler tb.KeyPress, AddressOf txtBox_KeyPress
    21.         AddHandler btn1.Click, AddressOf btn_Click
    22.     End Sub
    23.  
    24.     Private Sub txtBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBox.KeyPress
    25.         If e.KeyChar = Chr(Keys.Enter) Then
    26.             MessageBox.Show("enter pressed")
    27.         End If
    28.     End Sub
    29.  
    30.  
    31.     Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
    32.         MessageBox.Show("CLICKED!")
    33.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Massachusetts
    Posts
    48

    Adding controls at runtime

    Anyone:
    I wrote small program that isolates the problem I have with creating controls at runtime. (I have no problem creating and
    removing controls---its the click events I'm having trouble with)

    To recreate the program Set up a form with a panel(size it large). Add 3 buttons, a text box "tbInt" and a label "lblAns" (all outside the panel) to the form. Paste the code below. Type a number into the text box < 20, click Button1 to add the buttons, click button3 to chose a random number click on the number selected on the yellow created button. It will work fine until the
    number selected is the same as the last button created---then the BtnNew.click event will fire twice--
    Why?????

    (Button2 removes the created buttons.)

    Thanks a Lot!!


    Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents BTNNew As Button
    Private PanelWSIndex As Integer
    Private stAnswer As String
    ----------------------------------------------------------------------------

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'call to add the buttons
    AddButtons()
    End Sub

    Private Sub AddButtons()
    'this sub adds and spaces buttons and text within the panel
    Dim z As Integer
    Dim i As Integer
    Dim position As Point
    Dim iButtonCount As Integer = CInt(tbInt.Text)
    Dim fnt As New Font("Microsoft Sans Serif", 12, FontStyle.Regular)

    Try
    For z = 0 To Me.Controls.Count - 1 'find the panel I'm looking for
    If Me.Controls(z).GetType Is GetType(System.Windows.Forms.Panel) Then
    PanelWSIndex = z 'holds the number of the panel control
    For i = 0 To iButtonCount
    Select Case i
    Case Is = 0
    position.X = 16
    position.Y = 24
    Case Is = 10
    position.X = 16
    position.Y = 64
    BTNNew = New Button
    With BTNNew
    .Size = New Size(54, 34)
    .Location = position
    .Name = "TB" & i.ToString
    .Tag = "Added"
    .Text = i.ToString
    .Font = fnt
    .BackColor = Color.Yellow
    End With
    position.X += 68 'spaces location of control
    Me.Controls(PanelWSIndex).Controls.Add(BTNNew)
    AddHandler BTNNew.Click, New System.EventHandler(AddressOf BTNNew_Click)
    Next
    Exit For
    End If
    Next
    Catch ex As Exception

    End Try


    End Sub
    Private Sub RemoveButtons()
    Dim z As Integer
    Dim i As Integer
    Dim t As Integer
    Dim ctlRemove() As Control

    'this removes the buttons and handlers when the user selects a new number
    Try
    ReDim ctlRemove(Me.Controls(PanelWSIndex).Controls.Count - 1)
    For i = 0 To Me.Controls(PanelWSIndex).Controls.Count - 1
    If Not Me.Controls(PanelWSIndex).Controls(i).Tag = "Keep" Then
    ctlRemove(z) = Me.Controls(PanelWSIndex).Controls(i)
    z += 1 'only move up ctlRemove tally
    End If
    Next

    For t = 0 To UBound(ctlRemove)
    Me.Controls(PanelWSIndex).Controls.Remove(ctlRemove(t))
    Console.Write(ctlRemove(t).Name)
    RemoveHandler BTNNew.Click, AddressOf BTNNew_Click
    Next

    Catch
    MessageBox.Show(Err.Description)
    End Try

    stAnswer = "0"
    tbInt.Text = "0"
    End Sub

    Private Sub BTNNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNNew.Click

    If sender.text.ToString = stAnswer Then
    MessageBox.Show("Yes")
    'Beep()
    Else
    MessageBox.Show("Nope" & " " & stAnswer & " " & sender.text.ToString)
    End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    RemoveButtons()
    End Sub

    Function ChooseNumber() As String
    'will select a random number to be used as the in the btnNew click event for
    'the answer

    Dim rnd As New System.Random
    Dim stSelected As String
    Dim i As Integer
    Dim imax As Integer = CInt(tbInt.Text) + 1
    If Not imax = 0 Then
    Randomize()
    i = rnd.Next(0, imax)
    stSelected = CStr(i)
    Return stSelected 'returns the answer
    End If



    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    stAnswer = ChooseNumber()
    lblAns.Text = stAnswer
    End Sub

    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width