Results 1 to 11 of 11

Thread: controls.add

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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:
    1. Dim txtValues(30) As TextBox
    2. For i = 0 To 29
    3.             txtValues(i).Size = New System.Drawing.Size(textWidth, textHeight)
    4.             txtValues(i).Name = "txtValue" & i
    5. Next
    6.  
    7. 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!!

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Look into AddHandler and RemoveHandler to add and remove event handlers at run time.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    TNX!!! LOVE YA!!!!


    eeh I think you can forget about the last part
    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!!

  4. #4
    hellswraith
    Guest
    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.

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    I'm acting REAL dumb


    I cant figure this out, such a shame

    VB Code:
    1. Private Sub TextBoxValues_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.  
    3.     End Sub
    let's say this happens for a textbox, how can I say "textbox". text = something?

    I'm adding the events with addhandler as you guys said but how can I access the textbox in that event?
    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!!

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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:
    1. Private Sub TextBoxValues_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.         Dim MyTextbox As TextBox
    3.         MyTextbox = CType(sender, TextBox)
    4.         MyTextbox.Text = "something"
    5.     End Sub

    But are you sure you want to change the text in the TextChanged event?

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    tnx alot works!
    But 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?

    Oh and btw about the textbox.text, I was just trying to show an example of what I wanted to do I dont want to change the text in text_change I'm writing a program to hold information about my CDs and it's suppose to add some textboxes dynamically to input data....
    thanks for the help
    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!!

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    oh well another question
    let's say I want to access a texbox named txtValues & i and change one of it's properties. How can I do this?
    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!!

  9. #9
    hellswraith
    Guest
    You can loop through the controls to find one that has a name that matches.

  10. #10
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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.

  11. #11

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    tnx alot guys that solved it
    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!!

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