Creating events for controls

D

Dragon

Hi,

I am trying to create a form and add some controls to it programmatically. I
am pasting the code I have so far, below. here are the issues I am having.

#1. I am having to subtract additional 35 points from the
closeAbuotButton.Top ot the control goes out of form's bounds. I have no
clue why.
#2. I do not know how to access my Close Button's click event. It is asking
me to create "WithEvents" but I can't figure our where/how to use it.

Thank you.


'Code still not working...
Private Sub aboutHelpMenuItem_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles aboutHelpMenuItem.Click
'Create and Display About Dialog.

Dim nameLabel As New Label
Dim messageLabel As New Label
aboutForm = New Form

'Create Form
With aboutForm
.Text = "About"
.FormBorderStyle = FormBorderStyle.FixedDialog
.Height = 150
.Width = 300
'.ControlBox = False
End With

'Add Message Label
aboutForm.Controls.Add(messageLabel)
With messageLabel
.Text = "Programmed by Me"
.Top = 5
.Left = 5
.Width = aboutForm.Width - 5
.Height = 20
.TextAlign = ContentAlignment.MiddleCenter
End With

'Add Close Button
aboutForm.Controls.Add(closeAboutButton)
With closeAboutButton
.Hide()
.Text = "&Close"
.Width = 100
.Height = 25
.Top = aboutForm.Height - closeAboutButton.Height - 35 'For some reason
if I do not have -35, control goes out of form's bounds.
.Left = (aboutForm.Width / 2) - (closeAboutButton.Width / 2)
.Show()
End With

aboutForm.ShowDialog()

End Sub

Private Sub closeAboutButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles closeAboutButton.Click
'Close Form
Me.Close()
End Sub
 
C

Chris

Dragon said:
Hi,

I am trying to create a form and add some controls to it programmatically. I
am pasting the code I have so far, below. here are the issues I am having.

#1. I am having to subtract additional 35 points from the
closeAbuotButton.Top ot the control goes out of form's bounds. I have no
clue why.
#2. I do not know how to access my Close Button's click event. It is asking
me to create "WithEvents" but I can't figure our where/how to use it.

Thank you.


'Code still not working...
Private Sub aboutHelpMenuItem_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles aboutHelpMenuItem.Click
'Create and Display About Dialog.

Dim nameLabel As New Label
Dim messageLabel As New Label
aboutForm = New Form

'Create Form
With aboutForm
.Text = "About"
.FormBorderStyle = FormBorderStyle.FixedDialog
.Height = 150
.Width = 300
'.ControlBox = False
End With

'Add Message Label
aboutForm.Controls.Add(messageLabel)
With messageLabel
.Text = "Programmed by Me"
.Top = 5
.Left = 5
.Width = aboutForm.Width - 5
.Height = 20
.TextAlign = ContentAlignment.MiddleCenter
End With

'Add Close Button
aboutForm.Controls.Add(closeAboutButton)
With closeAboutButton
.Hide()
.Text = "&Close"
.Width = 100
.Height = 25
.Top = aboutForm.Height - closeAboutButton.Height - 35 'For some reason
if I do not have -35, control goes out of form's bounds.
.Left = (aboutForm.Width / 2) - (closeAboutButton.Width / 2)
.Show()
End With

aboutForm.ShowDialog()

End Sub

Private Sub closeAboutButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles closeAboutButton.Click
'Close Form
Me.Close()
End Sub

You dont' show the code where you create closeAboutButton, but the
declare needs to look like

dim WithEvents Variable as ObjectType

This gives you to hook into events of the object.

As for why you have to -35 off the top. This is because of the border
and Title bar. If you just want the inside height of the form use this.

..Top = aboutForm.ClientSize.Height - closeAboutButton.Height

Hope it helps
Chris
 
H

Herfried K. Wagner [MVP]

Dragon said:
#2. I do not know how to access my Close Button's click event. It is
asking me to create "WithEvents" but I can't figure our where/how to use
it.

Take a look at the documentation for the 'AddHandler' statement. You can
use this statement to dynamically add an event handler:

\\\
AddHandler btn.Click, AddressOf Me.Button_Click
///
 
D

Dragon

Thank you for your reply.

I have tried dimming the button like:

Dim WithEvents closeAboutButton As Button

but I get an error saying "WithEvents" is not valid on a local veriable
declaration.

Any ideas?
 
G

Guest

Dragon said:
Thank you for your reply.

I have tried dimming the button like:

Dim WithEvents closeAboutButton As Button

but I get an error saying "WithEvents" is not valid on a local veriable
declaration.

Any ideas?

You must declare the button on the form level for it to work, not inside
a sub or function.

Chris
 
P

Phill. W

I am trying to create a form and add some controls to it programmatically.
#1. I am having to subtract additional 35 points from the
closeAbuotButton.Top ot the control goes out of form's bounds.

Form.Height and .Width describe the Form /overall/, including
the borders and menubars.

Try using ClientArea.Height and .Width instead.
#2. I do not know how to access my Close Button's click event.
It is asking me to create "WithEvents" but I can't figure our where/how
to use it.

As you add each Control, use the AddHandler statement to
"wire up" the Events you're interested in to routines that can process
them, as in ...

Private Sub AnyButton_Click( sender as Object, e as EventArgs )
. . .
End Sub

.... then ...

x = New Button
AddHandler x.Click, AddressOf AnyButton_Click

HTH,
Phill W.
 
D

Dragon

Thank you Phil.


Phill. W said:
Form.Height and .Width describe the Form /overall/, including
the borders and menubars.

Try using ClientArea.Height and .Width instead.


As you add each Control, use the AddHandler statement to
"wire up" the Events you're interested in to routines that can process
them, as in ...

Private Sub AnyButton_Click( sender as Object, e as EventArgs )
. . .
End Sub

... then ...

x = New Button
AddHandler x.Click, AddressOf AnyButton_Click

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