473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Creating events for controls

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
Nov 21 '05 #1
7 1551
Dragon wrote:
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
Nov 21 '05 #2
"Dragon" <ba***********@hotmail.com> schrieb:
#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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
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?

"Chris" <no@spam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Dragon wrote:
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

Nov 21 '05 #4
Thank you Herfried. I will have to look into this for sure.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
"Dragon" <ba***********@hotmail.com> schrieb:
#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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
Dragon wrote:
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?

"Chris" <no@spam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Dragon wrote:
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



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

Chris
Nov 21 '05 #6
"Dragon" <ba***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.
Nov 21 '05 #7
Thank you Phil.
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
news:dj**********@yarrow.open.ac.uk...
"Dragon" <ba***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ben | last post by:
My current project requires me to create part of a form that is created on the fly. The project consists a list of entries to an event. The name and address and such is easy. The design is detup so...
2
by: Anand Sagar | last post by:
I have a Panel1 and button1 on my webform. At runtime, I create 2 textboxes. I do it at the Page_Load event. I put the code within the " If Not isPostBack" For the button click event, I will do...
6
by: Glenn Owens | last post by:
I have an ASP.Net page on which there are serveral static controls (listboxes, radiobuttonlist and textboxes). These controls are used to create criteria from which the code-behind will dynamically...
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
1
by: bill | last post by:
I'm using VS2005. I am dynamically adding a Textbox control to a Placeholder control in the page_load event of the form, but the event handler isn't firing. What am I doing wrong? Thanks...
4
by: Scott McNair | last post by:
Hi, I'm creating a user control that has absolutely no visual pieces to it... it's essentially a piece that sniffs a serial port awaiting incoming data. I know that there are several controls...
0
by: jehugaleahsa | last post by:
Hello: We are implementing a work flow system. We would like there to be one site hosting all the different work flows. Each state in the work flow is to be associated with a web user control...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.