p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro VB 6 (http://p2p.wrox.com/forumdisplay.php?f=73)
-   -   add control at runtime, pls HELP (http://p2p.wrox.com/showthread.php?t=24580)

jimmyfrank January 26th, 2005 06:06 PM

add control at runtime, pls HELP
 
Re: pro vb.net 2003 "Add Dynamic Control Demo"

this sub does not work as it should: see end of line notation.
the "new" button is displayed on the screen, but thats all.


    Private Sub AddNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewButton.Click
        Dim NewButton As Button

        'Create the new control.
        NewButton = New Button()

        'Set it up on the form.
        NewButton.Location() = New System.Drawing.Point(500, 500) ' *** Not working
        NewButton.Size() = New System.Drawing.Size(275, 123) ' *** Not working
        NewButton.Text() = "Button3" ' *** Not working

        'Addti to the forms control collection.
        Me.Controls.Add(New Button())

        'Hook up the Event Handler.
        AddHandler NewButton.Click, AddressOf Me.Button_Clicked ' *** Not working

    End Sub


Private Sub Button_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
        Dim ButtonClicked As Button
        ButtonClicked = CType(sender, Button)

        'Tell the world what button was clicked
        MessageBox.Show("You clicked " & ButtonClicked.Text)

    End Sub

gbianchi January 27th, 2005 09:30 AM

hi there...

you are in the VB6 forum, not vb.net...

anyway... what type of error do you received?? if your button show correctly it has to work... or you have a problem with your handler??

try reposting this on the .net forum...

HTH

Gonzalo

jimmyfrank January 29th, 2005 07:04 AM

thanks


ejan February 4th, 2005 03:39 PM

Hi Jimmy,

Here is what's wrong with your code. Frankly nothin' except a spelling mistake!

The control you are trying to add is a Button named "NewButton". You set some of its properties and finally try to add it to the Controls collection of the Form using this line of code:

                Me.Controls.Add(New Button())

Look closely, you should have written it like this:

                Me.Controls.Add(NewButton())

So what's the difference? an EXTRA SPACE between the words "New" and "Button". What this line of code does is create a new Button control and add this newly created button to the controls collection which is then displayed on the form. And none of its properties, such as Text and Size, will be set, of course!

That's the problem. Change the aforementioned line of code to this:

                Me.Controls.Add(NewButton())

and it will work.

Finally, you should have posted this question to the VB.Net forum. Plz take care with such issues coz it helps to keep the forums organized and thus make life easier for us poor mortals.

Hope your problem is solved.

ejan


All times are GMT -4. The time now is 05:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.