Results 1 to 5 of 5

Thread: [2008] Adding controls during run-time

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Iowa
    Posts
    73

    [2008] Adding controls during run-time

    I'm working with a form that is adding Labels to a larger, parent Label for moving and entering values into. I've included the code I put together. I was wondering if there was a better way to do this. I'm still fairly new to VB.NET and some of the things I'm used to have been changed.

    vb Code:
    1. Private Sub btnField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewText.Click
    2.         Dim lbl As Label
    3.         Dim sControls(lblBody.Controls.Count) As String
    4.         Dim iLoop As Integer
    5.  
    6.         'Makes sure we do not have more than 10 fields
    7.         If lblBody.Controls.Count = 10 Then
    8.             MsgBox("The maximum amount of fields are being used.")
    9.             Exit Sub
    10.         End If
    11.  
    12.         'Adds the names of the current fields to an array for lookup
    13.         For Each c As Control In lblBody.Controls
    14.             sControls(iLoop) = c.Name
    15.             iLoop = iLoop + 1
    16.         Next
    17.  
    18.         'Finds the first available field name and uses it
    19.         For i = 0 To 9
    20.             If Not sControls.Contains("field" & i) Then
    21.                 lbl = New Label
    22.                 lbl.Top = 0
    23.                 lbl.Left = 0
    24.                 lbl.Parent = lblBody
    25.                 lbl.BackColor = Color.Cornsilk
    26.                 lbl.BorderStyle = BorderStyle.FixedSingle
    27.                 lbl.Name = "field" & i.ToString
    28.                 lbl.Show()
    29.                 Exit For
    30.             End If
    31.         Next
    32.  
    33.         'For debugging purposes
    34.         Debug.WriteLine("___Added new field___")
    35.         For Each c As Control In lblBody.Controls
    36.             Debug.WriteLine(c.Name & ": " & c.Text)
    37.         Next
    38.     End Sub

    The users can add and remove fields and I always want the newly added field to be the first available. So if there are already fields 0-5 and the user removes field2, I want the next new field to be field2 instead of field6.

    Thank you for the help!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [2008] Adding controls during run-time

    instead of adding Labels to a larger parent Label, use a panel.
    labels aren't intended to be container controls

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Iowa
    Posts
    73

    Re: [2008] Adding controls during run-time

    Thank you for your response. I am aware that a label used as the parent is odd. This is what the original developer used and I just hadn't bothered changing it yet. I've already redesigned the whole form and they're now inside of a split container panel. I'm really just wanting to know if the code I posted is the most efficient way to make sure the correct labels are created with the correct names. It works perfectly fine, but if there is more of a built-in way of doing it, I'd like to learn of it.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: [2008] Adding controls during run-time

    I'm just wondering, since you're naming your labels with only generic names ("filed1", "field2", ...), why is it so important to find a hole in the series? Maybe there are other better approaches if we know what the purpose of those serialized names.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Iowa
    Posts
    73

    Re: [2008] Adding controls during run-time

    It's a form for a printer label design program. The fields will all be dynamically generated by the user and the resulting template is saved in xml.

    The labels will also have their names in the text property by default, I didn't add that because I wasn't sure of the format I was going to use yet. I decided that enclosing the text in brackets would define it is a lookup value(from database/excel file) and not having it enclosed in brackets will make it static text. So when the user opens an older template with 4 fields on it and wishes to add a 5th field, it might confuse the user to have it say field15 when it's only the 5th field. It would likely also be confusing to some users to see the newly added 5th field say "field2" but I imagined they would understand that before field15. I assumed it would be less likely to grab the attention of someone if the field generated would have a number within the amount of shown fields and possibly save some confusion of "Where are the other 10 fields?" if it did show up as field15.

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