-
May 8th, 2008, 03:58 PM
#1
Thread Starter
Lively Member
[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:
Private Sub btnField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewText.Click Dim lbl As Label Dim sControls(lblBody.Controls.Count) As String Dim iLoop As Integer 'Makes sure we do not have more than 10 fields If lblBody.Controls.Count = 10 Then MsgBox("The maximum amount of fields are being used.") Exit Sub End If 'Adds the names of the current fields to an array for lookup For Each c As Control In lblBody.Controls sControls(iLoop) = c.Name iLoop = iLoop + 1 Next 'Finds the first available field name and uses it For i = 0 To 9 If Not sControls.Contains("field" & i) Then lbl = New Label lbl.Top = 0 lbl.Left = 0 lbl.Parent = lblBody lbl.BackColor = Color.Cornsilk lbl.BorderStyle = BorderStyle.FixedSingle lbl.Name = "field" & i.ToString lbl.Show() Exit For End If Next 'For debugging purposes Debug.WriteLine("___Added new field___") For Each c As Control In lblBody.Controls Debug.WriteLine(c.Name & ": " & c.Text) Next 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!
-
May 8th, 2008, 05:16 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 8th, 2008, 09:14 PM
#3
Thread Starter
Lively Member
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.
-
May 8th, 2008, 09:23 PM
#4
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 -
-
May 9th, 2008, 09:43 AM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|