Home All Groups Group Topic Archive Search About

Adding buttons to form help

Author
23 Nov 2006 7:38 PM
Marc
Hi,

I am using the below code to add a button to form. However it is not
working. If I change the form name from 'form1' to 'me' it does add the
button to the current form...any ideas.

Also does anyone know a way keep any user generated buttons in the form
even after the application is closed down,..Im guessing this requires a
DB link?

        button1.Text = "OK"
        ' Set the position of the button on the form.
        button1.Location = New Point(10, 10)
        ' Set the text of button2 to "Cancel".
        ' Add button1 to the form.
        form1.Controls.Add(button1)

Author
23 Nov 2006 7:53 PM
lord.zoltar
Marc wrote:
> Hi,
>
> I am using the below code to add a button to form. However it is not
> working. If I change the form name from 'form1' to 'me' it does add the
> button to the current form...any ideas.

You mean you actually changed the form's name to "Me"? Ot did you just
chagne a line like this:
form1.Controls.Add(button1)
to:
Me.Controls.Add(button1)
?

the "Me" keyword refers to the object that the "Me" keyword is used in,
much the same way people use it to refer to themselves when speaking
English. I prefer it over refering to a form by its name within itself.

> Also does anyone know a way keep any user generated buttons in the form
> even after the application is closed down,..Im guessing this requires a
> DB link?
>
>         button1.Text = "OK"
>         ' Set the position of the button on the form.
>         button1.Location = New Point(10, 10)
>         ' Set the text of button2 to "Cancel".
>         ' Add button1 to the form.
>         form1.Controls.Add(button1)

You probably don't need a database for this. You will probably have to
serialize the object (form), but it's up to you if you want to
serialize it as a file on the local machine where the program is
running, or into a database somewhere. Local is probably much easier.
By the way, I don't know if buttons, textboxes, or other controls are
serializable, I suspect not. I had a similar problem last week. I got
around it by only serializing the most relevant data: the text in the
text boxes, and the text of certain labels.
Search on MSDN for object serializing tutorials for more detailed info.
Author
23 Nov 2006 7:56 PM
Marc
Thanks,

Yes me as in the current form, however i want the button to appear on a
separate form?

lord.zol***@gmail.com wrote:
Show quoteHide quote
> Marc wrote:
> > Hi,
> >
> > I am using the below code to add a button to form. However it is not
> > working. If I change the form name from 'form1' to 'me' it does add the
> > button to the current form...any ideas.
>
> You mean you actually changed the form's name to "Me"? Ot did you just
> chagne a line like this:
> form1.Controls.Add(button1)
> to:
> Me.Controls.Add(button1)
> ?
>
> the "Me" keyword refers to the object that the "Me" keyword is used in,
> much the same way people use it to refer to themselves when speaking
> English. I prefer it over refering to a form by its name within itself.
>
> > Also does anyone know a way keep any user generated buttons in the form
> > even after the application is closed down,..Im guessing this requires a
> > DB link?
> >
> >         button1.Text = "OK"
> >         ' Set the position of the button on the form.
> >         button1.Location = New Point(10, 10)
> >         ' Set the text of button2 to "Cancel".
> >         ' Add button1 to the form.
> >         form1.Controls.Add(button1)
>
> You probably don't need a database for this. You will probably have to
> serialize the object (form), but it's up to you if you want to
> serialize it as a file on the local machine where the program is
> running, or into a database somewhere. Local is probably much easier.
> By the way, I don't know if buttons, textboxes, or other controls are
> serializable, I suspect not. I had a similar problem last week. I got
> around it by only serializing the most relevant data: the text in the
> text boxes, and the text of certain labels.
> Search on MSDN for object serializing tutorials for more detailed info.
Author
23 Nov 2006 8:09 PM
lord.zoltar
Marc wrote:
> Thanks,
>
> Yes me as in the current form, however i want the button to appear on a
> separate form?
>

So you want the user to do something to FormA and then have FormA cause
a button to appear on FormB?

The simplest way might be to have an "add_button" procedure in FormB
and then have FormA call that procedure.
Code in FormA could look like this

dim FormB as Windows.Forms.Form 'Assumes that FormB does not yet exist.
FormB.add_button()
Author
23 Nov 2006 8:16 PM
Marc
OK thanks , so what would the code in Form B be?


lord.zol***@gmail.com wrote:
Show quoteHide quote
> Marc wrote:
> > Thanks,
> >
> > Yes me as in the current form, however i want the button to appear on a
> > separate form?
> >
>
> So you want the user to do something to FormA and then have FormA cause
> a button to appear on FormB?
>
> The simplest way might be to have an "add_button" procedure in FormB
> and then have FormA call that procedure.
> Code in FormA could look like this
>
> dim FormB as Windows.Forms.Form 'Assumes that FormB does not yet exist.
> FormB.add_button()
Author
23 Nov 2006 8:28 PM
lord.zoltar
Marc wrote:
> OK thanks , so what would the code in Form B be?
>

umm... that really depends on what you want to do.
at the simplest, try:

public sub add_button
    dim myBtn as New Button
    Me.Controls.Add(myBtn)
end sub
Author
23 Nov 2006 8:38 PM
Marc
Hmmm...I tried that and its still not working!.

I tried adding the button from Form B' and it worked fine...its only
when i try to add a button from 'Form A' it doesnt work.i.e from one
form to another.


my code is

form B

Dim myBtn As New Button
        Me.Controls.Add(myBtn)

form a

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

        controldrag.add_button()
    End Sub


lord.zol***@gmail.com wrote:
Show quoteHide quote
> Marc wrote:
> > OK thanks , so what would the code in Form B be?
> >
>
> umm... that really depends on what you want to do.
> at the simplest, try:
>
> public sub add_button
>     dim myBtn as New Button
>     Me.Controls.Add(myBtn)
> end sub
Author
23 Nov 2006 8:42 PM
lord.zoltar
Marc wrote:
Show quoteHide quote
> Hmmm...I tried that and its still not working!.
>
> I tried adding the button from Form B' and it worked fine...its only
> when i try to add a button from 'Form A' it doesnt work.i.e from one
> form to another.
>
>
> my code is
>
> form B
>
> Dim myBtn As New Button
>         Me.Controls.Add(myBtn)
>
> form a
>
>  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
>         controldrag.add_button()
>     End Sub
>
>

What is controldrag?
what part of it doesn't work? what errors does it report?