I have multiple rows of textboxes which I want to scroll using a
vertical scroll bar. I'm also going to have a command button to add
and delete rows of textboxes based on the user's needs. The problem is
that I have this accomplished in VB6 but am not sure how to handle it
using VB.net because there are no control arrays.
Here is the code I have a for a sample of what i am trying to
accomplish done in VB6.. Anyone know how to get this functionality
with VB.net
I'm using a textbox, two command buttons, and 2 picture boxes.
Thanks,
Option Explicit
Private Sub cmdAddAppt_Click()
'Add a new textbox for another appointment and adjust the
scrollbar.
'Note that the scrollbar's max property cannot be less than zero
Dim Index As Integer
Dim ListIndex As Integer
Index = txtAppt().UBound + 1
Load txtAppt(Index)
txtAppt(Index).Top = txtAppt(Index - 1).Top + txtAppt(Index -
1).Height
txtAppt(Index).Text = Empty
txtAppt(Index).Visible = True
picInner.Height = txtAppt(0).Height * txtAppt().Count +
txtAppt(0).Top * 2
vsbAppt.Max = Max(picInner.Height - picOuter.Height, 0)
vsbAppt.SmallChange = picInner.Height * 0.02
vsbAppt.LargeChange = picInner.Height * 0.2
End Sub
Private Sub cmdDelAppt_Click()
'Remove the last appointment textbox and adjust the scrollbar.
'Note that the scrollbar's max property cannot be less than zero
'Warn when trying to remove the last textbox.
Dim Index As Integer
Index = txtAppt().UBound
If Index > 0 Then
Unload txtAppt(Index)
picInner.Height = txtAppt(0).Height * txtAppt().Count +
txtAppt(0).Top
vsbAppt.Max = Max(picInner.Height - picOuter.Height, 0)
Else
MsgBox "Can't unload the last one!"
End If
End Sub
Private Sub vsbAppt_Change()
'Scroll
picInner.Top = -vsbAppt.Value
End Sub
Private Function Max(a As Integer, b As Integer) As Integer
'Return the larger of two values
Dim result As Integer
If a > b Then
result = a
Else
result = b
End If
Max = result
End Function
Try putting the textboxes on a panel object. Then set the panel object's AutoScroll propery to true. When the contents of the panel extend past the edges of it, then it will automatically enable a scrollbar. You shouldn't even have to write a single line of code.
didnt know there was a autoscroll for the panel control but i'll take a look into that for sure. what about instantiating new textboxes, i'm having a hard time figuring out how to use the add and delete button because of the lack of control arrays? any insight?
I think you want to use the collection class if you want to keep controls together. I haven't used it myself. Another idea is if you do put them in the panel, the panel has it's own controls collection it keeps track of.
Here is some C# code that I posted here on the forum a bit back, even though it isn't in VB, you should still be able to understand it.
Code:
PictureBox pb = new PictureBox();
pb.Size = new System.Drawing.Size(iThumbSizeX, iThumbSizeY);
pb.BackColor = System.Drawing.Color.Black;
// Unique names for each thumb added.
pb.Name = iThumbCounter.ToString();
iThumbCounter++;
// 'this' actually refers to a panel object.
this.Controls.Add(pb);
Bitmap pic = new Bitmap(filename);
pb.Image = (Bitmap)pic.GetThumbnailImage(iThumbSizeX,iThumbSizeY,null,(IntPtr)0);
// Subscribe to the newly created thumbs click event.
// Again, 'this' refers to a panel object.
pb.Click += new System.EventHandler(this.pb_Click);
This is from a control I wrote that inherits from a panel control. When I refer to 'this.Controls.Add(pb)', I am refering to the panel object that I am inheriting from. Hopefully this will give you an idea of how to create a control in code and add it to a panel object. Deleting the control completely is easy, just call the Dispose method of the control.
Now that I am thinking about it, your problem is really close to what I have done with my new image panel control, but instead of pictureboxes, you need textboxes... If you want that code just tell me. I warn you though, it is all in C#, and hasn't been looked over with a fine tooth comb yet (but in general, it works quite well, just need to add more error handling for 'just in case things go wrong type stuff'). With it you set the height and width of the pictureboxes (which would be textboxes for you), the padding and spacing of the contained controls, and whether you want the controls arranged horizontal or vertical. It has methods for add, remove, and remove all. I have added a custom event that will tell when a picture was clicked, and which one was clicked.
i really appreciate your insight, i'm a novice programmer with VB6 and i'm having to develop this application in Vb.net at work so i'm really just having some difficulties as far as some functionality goes. I would definitely appreciate the c# code, even though there is syntactical differences the concepts are very similar. I think i could adapt some ideas and learn a few tricks.
Ok, here it is. Keep in mind that it hasn't been given the fine tooth comb yet, and there is probably some areas that could be improved. If you find anything to improve, I ask that you post the new code here so I can benefit from your work also. Hope it helps.