Create button at runtime

T

Tony WONG

i wish to create a button at runtime with same string in the parameters.
Should i pass the parameter in this way? seems not smart! Thank you.

*************************
createbutton("ABC", "ABC", "ABC")

Private Sub createbutton(ByVal newbutton, ByVal buttontext, ByVal
buttonname)
newbutton = New Button()
newbutton.text = buttontext
newbutton.name = buttonname
Me.Controls.Add(newbutton)
End Sub
 
R

Rory Becker

Hello Tony,

If you have VS2008 then you can do this

-------------------------------------------------------------
' createbutton("ABC", "ABC", "ABC")
Private Sub createbutton(ByVal newbutton, ByVal buttontext, ByVal buttonname)
Me.Controls.Add(New Button() With {.text=buttontext, .name=buttonname})
End Sub
-------------------------------------------------------------

....but perhaps it would be more useful to make it a function in case you
need a reference to the new button

-------------------------------------------------------------
' createbutton("ABC", "ABC", "ABC")
Private Function createbutton(ByVal newbutton, ByVal buttontext, ByVal buttonname)
createbutton = New Button() With {.text=buttontext, .name=buttonname}
Me.Controls.Add(createbutton)
End Sub
 
F

Family Tree Mike

Rory Becker said:
Hello Tony,

If you have VS2008 then you can do this

-------------------------------------------------------------
' createbutton("ABC", "ABC", "ABC")
Private Sub createbutton(ByVal newbutton, ByVal buttontext, ByVal buttonname)
Me.Controls.Add(New Button() With {.text=buttontext, .name=buttonname})
End Sub
-------------------------------------------------------------

....but perhaps it would be more useful to make it a function in case you
need a reference to the new button

-------------------------------------------------------------
' createbutton("ABC", "ABC", "ABC")
Private Function createbutton(ByVal newbutton, ByVal buttontext, ByVal buttonname)
createbutton = New Button() With {.text=buttontext, .name=buttonname}
Me.Controls.Add(createbutton)
End Sub

I would go with the function, but drop the (ByVal newbutton) parameter, and
return that as the result of the function. I would also expect the calling
function to add it to the set of controls as it may not even be in the form,
but a container on the form. The caller may want to set the position of the
button as well.
 
R

Rory Becker

Hi Mike,
I would go with the function, but drop the (ByVal newbutton)
parameter, and return that as the result of the function.
Yeah that was a simple oversight. In truth I didn't even look at the signature
other than a change from sub to function.
Hence also forgetting to give an AS clause at the end to specify the return
type. Oops :)
I would
also expect the calling function to add it to the set of controls as
it may not even be in the form, but a container on the form.

If you remove the adding of the new button to the form from the function
then you essentially remove the purpose of the function at this point, as
it is reduced to a single line.

-------------------------------------------------------------
Private Function createbutton(ByVal buttontext, ByVal buttonname) as Button
return New Button() With {.text=buttontext, .name=buttonname}
End Sub
-------------------------------------------------------------

Depending on where this function is to be located you may want to hard code
the Container of the new button or alternatively allow the caller to pass
it in thus:

-------------------------------------------------------------
Private Function createbutton(ByVal buttontext, ByVal buttonname, container
as ContainerControl) as Button
createbutton = New Button() With {.text=buttontext, .name=buttonname}
container.Add(createbutton)
End Sub
-------------------------------------------------------------

The caller may want to set the position of the button as well.

Absolutely.
 
R

rowe_newsgroups

i wish to create a button at runtime with same string in the parameters.
Should i pass the parameter in this way? seems not smart! Thank you.

*************************
createbutton("ABC", "ABC", "ABC")

Private Sub createbutton(ByVal newbutton, ByVal buttontext, ByVal
buttonname)
newbutton = New Button()
newbutton.text = buttontext
newbutton.name = buttonname
Me.Controls.Add(newbutton)
End Sub

I'm about to puke, are you actually leaving off Option Strict, leaving
of type declarations and implicitly casting a string into a button? Or
is this just some quickly typed pseudocode? If it's not I would
suggest that before you continue with the application you read up on
good programming practices, or your application will bomb in many
places besides trying to create a button at runtime.

Thanks,

Seth Rowe [MVP]
 
P

Phill W.

Tony said:
i wish to create a button at runtime with same string in the parameters.
Should i pass the parameter in this way? seems not smart! Thank you.

Nothing wrong with it, assuming that the different arguments are used
for different things.
createbutton("ABC", "ABC", "ABC")

That could just as easily be

createbutton("ABC", "DEF", "GHI")
Private Sub createbutton(ByVal newbutton, ByVal buttontext, ByVal
buttonname)

Even /allowing/ for the [IMHO] abomination that is Type Inference, this
code just won't work. You pass in a String value ("ABC", in the
variable newbutton) and immediately assign newbutton a new Button
[control]. That just isn't going to work.

I think you want something more like

Private Function CreateButton( _
ByVal text as String _
, ByVal buttonName as String _
, ByVal parentControl as Control _
) as Button

Dim btn as New Button()
With btn
.Name = buttonName
.Text = text
End With
parentControl.Controls.Add( btn )

Return btn
End Function

If you want to make the API to this a bit cleaner (say, the Name and
Text can be the /same/), then create an overloaded method, something like:

Private Function CreateButton( _
ByVal textAndName as String _
, ByVal parentControl as Control _
) as Button
Return Me.CreateButton( textAndName, textAndName, parentControl )
End Function

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top