Adding buttons to form help

M

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)
 
L

lord.zoltar

Marc said:
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.
 
M

Marc

Thanks,

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

lord.zoltar

Marc said:
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()
 
L

lord.zoltar

Marc said:
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
 
M

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
 
L

lord.zoltar

Marc said:
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?
 

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