|
-
Jun 11th, 2002, 12:29 AM
#1
controls.add
If you get bored reading this, just skip it and read the bold part 
I'm adding some controls to my form with the Controls.Add method, but the problem that I have is that I dont know how to write events for them
I am adding a whole buncha textboxes, I want them to act as control arrays but how can I tell my event what to handel?
here's an example of what I'm doing if I wasnt clear:
VB Code:
Dim txtValues(30) As TextBox
For i = 0 To 29
txtValues(i).Size = New System.Drawing.Size(textWidth, textHeight)
txtValues(i).Name = "txtValue" & i
Next
Me.Controls.AddRange(txtValues)
Now I want to add a sub so that if ANY of those textboxes are changed, I would be able to do something, just like a control array.
This is simple if I was doing it during design time, but since I'm adding the controls at runtime I dont know how to add the events for them
the bold part is my problem I guess
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 11th, 2002, 01:59 AM
#2
Look into AddHandler and RemoveHandler to add and remove event handlers at run time.
-
Jun 11th, 2002, 02:09 AM
#3
-
Jun 11th, 2002, 05:08 AM
#4
I wrote this in C#, but it might help you. This a control that adds picture boxes to a panel control.
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.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.
pb.Click += new System.EventHandler(this.pb_Click);
What I do is have one event that they all subscribe to. In that event, I take the passed in object and get the name. Since I named them numbers sequentially, it is easy to refer to them and use a basic select statement if I want.
-
Jun 11th, 2002, 04:35 PM
#5
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 11th, 2002, 05:50 PM
#6
The caller is passed in the sender object. Cast the sender object in a textbox variable, and you can use this variable.
eg:
VB Code:
Private Sub TextBoxValues_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim MyTextbox As TextBox
MyTextbox = CType(sender, TextBox)
MyTextbox.Text = "something"
End Sub
But are you sure you want to change the text in the TextChanged event?
-
Jun 11th, 2002, 08:40 PM
#7
-
Jun 11th, 2002, 09:00 PM
#8
-
Jun 12th, 2002, 06:20 AM
#9
You can loop through the controls to find one that has a name that matches.
-
Jun 12th, 2002, 07:06 AM
#10
Originally posted by MrPolite
[BBut I dont get it, why is it like this? Why is the textbox sent as an "object"? Cant we just change the type to TextBox?
[/B]
Most events use generic parameters. This way you can use the event handler for more then just textboxes. You could have one event handler handle events for textboxes, pictureboxes, etc.
You can't change the type to textbox, because the procedure definition wouldn't match the event definition for TextChanged anymore.
If you want to refer to a different textbox you also loaded at runtime, you can either loop through the controls collection of the form untill you find a control with the correct name, or use the variable you used when you loaded the textbox. I would recommend to declare the variable(s) at module level (possibly an array of type Textbox), so you can use these variables anytime you need them. You could either remember the index in this array (instead of the name), or set a property (eg. the Name property) , and loop through the array until you find it.
-
Jun 12th, 2002, 04:44 PM
#11
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
|