|
-
Mar 26th, 2010, 11:39 AM
#1
Thread Starter
Member
Creating Multiples of Controls
I have a program that utilized a numericupdown control. When the numeric value = 1 then it displays 1 text box on button click
When I add this code
If NumericUpDown1.Value = 1 Then Me.Controls.Add(Newbox)
If NumericUpDown1.Value = 2 Then Me.Controls.Add(Newbox * 2)
Trying to multiply the boxes it creates by 2, it doesn't work.
How do I utilize multiplication to create multiples of my new textbox control?
(the error I get, just in case it is needed is "Operator '*' is not defined for types 'System.windows.forms.textbox' and 'integer')
Last edited by Kasem; Mar 30th, 2010 at 03:21 PM.
-
Mar 26th, 2010, 12:04 PM
#2
Re: Creating Multiples of Controls
You can't use multiplication for that. Making multiplication work for textboxes might be possible, but it would be too bizarre to use generally, so it would have to be highly specific.
What you actually need to do is to use a loop:
Code:
For x as integer = 0 to NumericUpDown1.Value-1
Me.Controls.Add(Newbox)
Next
My usual boring signature: Nothing
 
-
Mar 26th, 2010, 12:20 PM
#3
Lively Member
Re: Creating Multiples of Controls
You could overload the operator although you will run into problems laying your control out if you do that:
vb Code:
Public Shared Operator *(ByVal val1 As Integer, ByVal tb As OverloadedTextbox) As ArrayList
Dim multiTb As TextBox = CType(tb, TextBox)
Dim arr1 As New ArrayList()
For i As Integer = 0 To val1 - 1
multiTb.Text = "Button " + i.ToString()
multiTb.Name = "Button" + i.ToString()
arr1.Add(multiTb)
Next
Return arr1
End Operator
But I've found a few bugs in it up to now, in that positioning the control will be a problem, it will not show up in intellisense, and therefore it will throw a compile time error if you try to manipulate it. You have to create a separate class too as the * operator is already configured for the form. But you might be able to polish it up as these overloadings can be a great time-saver sometimes.
Just seems like a headache for too little work if you ask me.
Technik ... Kniff, die Welt so einzurichten, dass wir sie nicht erleben mussen
Max Frisch
-
Mar 26th, 2010, 01:04 PM
#4
Thread Starter
Member
Re: Creating Multiples of Controls
 Originally Posted by Shaggy Hiker
You can't use multiplication for that. Making multiplication work for textboxes might be possible, but it would be too bizarre to use generally, so it would have to be highly specific.
What you actually need to do is to use a loop:
Code:
For x as integer = 0 to NumericUpDown1.Value-1
Me.Controls.Add(Newbox)
Next
This most probably worked but the positioning must be off and its just creating textboxes on top of eachother. Still pretty new to coding VB
Should I be setting the location via each new textbox manually? For instance adding a separate "If numericupdown1.value = 1 then NewBox.Location = " sort of thing?
It would probably be best if I just added the code. This is most probably a simple fix, I'm just not seeing it.
Code:
Dim Newbox As New TextBox
Dim X, Y As Integer
Dim xLeft, yTop As Integer
xLeft = 20
yTop = 20
Newbox.Height = 20
Newbox.Width = 20
Newbox.Location = New Point(X, Y)
Y = yTop * Newbox.Height
X = xLeft * Newbox.Width
For X = 0 To NumericUpDown1.Value - 1
Me.Controls.Add(Newbox)
Next
If NumericUpDown1.Value = "0" Then MsgBox("Please Choose a Number Greater Than 0", MsgBoxStyle.Critical) : Exit Sub
If NumericUpDown1.Value >= "10" Then MsgBox("Please Choose a Number Less Than 10", MsgBoxStyle.Critical) : Exit Sub
If NumericUpDown1.Value >= "0" Then MsgBox(NumericUpDown1.Value)
-
Mar 26th, 2010, 03:00 PM
#5
Re: Creating Multiples of Controls
Definitely.
I was assuming that you were just putting NewBox in there as a placeholder. I would actually be doing something more like this:
Code:
For x as Integer = 0 to NumericUpDownControl -1
Dim newBox as New TextBox
'These two can stay the same for all of them.
Newbox.Height = 20
Newbox.Width = 20
newBox.Top = 28 * x
newBox.Left = 20
My.Controls.Add(NewBox)
Next
The idea is that the height of each box is 20, and this would add a row of textboxes with a separation between them of 8 (which appears to be a good distance). I would actually be tempted to use 24 rather than all those 20s, as it does appear that multiples of 8 seem to work well for control spacing. That would also mean that the top would be 32 * x rather than 28 * x. This would put the first box at 0, the next at 32, then 64, and so on. You probably wouldn't want to start at 0, either.
The point is that the top property of each is set based on the number of times through the loop. I'm currently doing something kind of like that, but I split it off into a sub function, because I have to set a variety of properties and add some handlers, as well, or else I'd paste in the code. As it is, it's so different that it wouldn't really make anything more clear.
My usual boring signature: Nothing
 
-
Mar 26th, 2010, 04:19 PM
#6
Thread Starter
Member
Re: Creating Multiples of Controls
 Originally Posted by Shaggy Hiker
Definitely.
I was assuming that you were just putting NewBox in there as a placeholder. I would actually be doing something more like this:
Code:
For x as Integer = 0 to NumericUpDownControl -1
Dim newBox as New TextBox
'These two can stay the same for all of them.
Newbox.Height = 20
Newbox.Width = 20
newBox.Top = 28 * x
newBox.Left = 20
My.Controls.Add(NewBox)
Next
The idea is that the height of each box is 20, and this would add a row of textboxes with a separation between them of 8 (which appears to be a good distance). I would actually be tempted to use 24 rather than all those 20s, as it does appear that multiples of 8 seem to work well for control spacing. That would also mean that the top would be 32 * x rather than 28 * x. This would put the first box at 0, the next at 32, then 64, and so on. You probably wouldn't want to start at 0, either.
The point is that the top property of each is set based on the number of times through the loop. I'm currently doing something kind of like that, but I split it off into a sub function, because I have to set a variety of properties and add some handlers, as well, or else I'd paste in the code. As it is, it's so different that it wouldn't really make anything more clear.
That did it pretty well. I had an issue where if the value would = 1 it would put it 1 text box, if the value = 5 it would put out 1 textbox in the 5th position.
I think I found a way around that, but now I can't get it to remove the textboxes if the textboxes are scaled down. Like say I click 5 and then I want to click 3 instead, it won't remove the other textboxes. I suppose I just don't know the correct code to remove. I tried me.control.remove(newbox) as a test, but it didn't do anything. I suppose instead of scaling I could make sure of a button that clears the entire form of controls?
I really appreciate the help, its showing me quite a few new ways to do things .
-
Mar 26th, 2010, 04:29 PM
#7
Re: Creating Multiples of Controls
I have never tried removing a textbox. It's an interesting issue, but it certainly should be possible. You would have to be giving each textbox a distinct name (that wasn't included in any of the code that either you or I posted). You would then need to find the textbox with the correct name in the controls collection and remove it. However, if you have added any event handlers for the textbox, you would also want to remove those handlers, or else they would still handle the events, even though the textbox would be gone. This might cause no trouble at all, but it could also cause some real trouble. Doesn't look like you are adding any event handlers, though, in which case it isn't even an issue.
I would want to be adding names to each textbox that would make them easy to search for, but which wouldn't conflict with any existing boxes. Therefore, I would be setting the name to something like:
NewBox.Name = "tb" & x.ToString
inside that loop that creates them. Therefore, if you created 5 textboxes, they would be named tb0, tb1, tb2, tb3, tb4. If you then wanted 3, you'd have to decide whether you wanted to get rid of tb0 and tb1, or tb3 and tb4.
You can then use
Me.Controls.RemoveByKey(<control name>)
to remove the controls. Of course, doing this in a loop depends on figuring out which controls needed to be removed.
My usual boring signature: Nothing
 
-
Mar 29th, 2010, 12:11 PM
#8
Thread Starter
Member
Re: Creating Multiples of Controls
 Originally Posted by Shaggy Hiker
I have never tried removing a textbox. It's an interesting issue, but it certainly should be possible. You would have to be giving each textbox a distinct name (that wasn't included in any of the code that either you or I posted). You would then need to find the textbox with the correct name in the controls collection and remove it. However, if you have added any event handlers for the textbox, you would also want to remove those handlers, or else they would still handle the events, even though the textbox would be gone. This might cause no trouble at all, but it could also cause some real trouble. Doesn't look like you are adding any event handlers, though, in which case it isn't even an issue.
I would want to be adding names to each textbox that would make them easy to search for, but which wouldn't conflict with any existing boxes. Therefore, I would be setting the name to something like:
NewBox.Name = "tb" & x.ToString
inside that loop that creates them. Therefore, if you created 5 textboxes, they would be named tb0, tb1, tb2, tb3, tb4. If you then wanted 3, you'd have to decide whether you wanted to get rid of tb0 and tb1, or tb3 and tb4.
You can then use
Me.Controls.RemoveByKey(<control name>)
to remove the controls. Of course, doing this in a loop depends on figuring out which controls needed to be removed.
thank you for the help, it did assist me in naming newboxes that were created, but it will not remove the boxes that are already created. I've tried it a few different ways.
Code:
If NumericUpDown1.Value = 1 Then
Me.Controls.Add(Newbox)
Me.Controls.Add(Newbox2)
Me.Controls.RemoveByKey("Newbox4")
End If
If NumericUpDown1.Value = 1 And Newbox3.Created Then
Me.Controls.Remove(Newbox3)
I've also tried it as Me.Control.Remove(Newbox4)
I've hard coded the names in for testing purposes so that I can test this before copying it to the final program code. I cannot for the life of me figure out how to remove the existing boxes.
-
Mar 30th, 2010, 03:20 PM
#9
Thread Starter
Member
Re: Creating Multiples of Controls
Shaggy, you were right, it really helps to removebykey instead of trying it the way I was doing it by naming the entire control. I don't know why it didn't work that way, but I toyed with it enough to learn on my own the wisdom of your ways.
Thank you!
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
|