Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo
Adding buttons programmatically in visual basic .net (Wednesday, September 15, 2004)
 

Found the following interesting discussion in the Newsgroups:

Adding buttons programmatically in visual basic .net
by:Anonymous

Hi,

I have a form and I add buttons programmatically in de form_Load function.

Anybody know how implements de function button_click if in the designer mode the button doesn't exists?
I write in visual basic .net:
mybutton.Click = New EventHandler (AddressOf DetectedAClick)

private Sub DetectedAClick(ByVal sender as object, ByVal e as EventArgs)

I produced an erron in executing mode, its say: An unhandled exception of type "System.InvalidCastException' ocurred in
microsoft.visualbasic.dll. Additional information: Specific cast is not valid.

Do you know what is the problem?

Another question:

In a datagrid control is posible programmatically do autosize columns?

Another:

In a listview is possible that when I select a item doesn't view in blue color?

Thanks

Silvia


 Reply:
by:Armin Zingler

 
To add an event handler use Addhandler:
addhandler mybutton.click, addressof ButtonClick
--
Armin


 Reply:
by:Cor Ligthert

 Hi Silvia,

Now I know what you use.
:-)
> I write in visual basic .net:
Sample a simple methode where you do not have to set all

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
Dim nowdate As DateTime = DateTime.Now
Dim mybutton(System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month)) As Button
For i = 0 To System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month) - 1
mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
AddHandler mybutton(i).MouseHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).MouseLeave, AddressOf mybutton_Leave
start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AliceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AntiqueWhite
End Sub


Another question:
>
> In a datagrid control is posible programmatically do autosize columns?
The datagrid is to extended to tell that have a look here first.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridtablestyleclasstopic.asp

> Another:
>
> In a listview is possible that when I select a item doesn't view in blue color?

That I do not know, however I think one of the painters know that.

I hope this helps so far

Cor



 Reply:
by:Anonymous

 Hi Cor,

Thanks, you do help me a lot. :)

Silvia


 Reply:
by:hirf-spam-me-here@gmx.at (Herfried K. Wagner [MVP])

 

\\Dim btn As New Button()
AddHandler btn.Click, AddressOf Me.btn_Click
Me.Controls.Add(btn)
..
..
..
Private Sub btn_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
)
MsgBox("Hello World!")
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>



Posted by Xander Zelders



 
Previous Posts
    - convert function to IntPtr
    - Creating objects with New
    - Save record in datagrid while still editing
    - How to Get info from MDI child
    - About a datagrid row
    - How to Pass Parameters to an Event
    - How to delete all the contents in a datagrid
    - Build error for VB.NET but not C#
    - Comparing datareader result to a string
    - Dataviews and constrained datasources?

Archives
    - 08/01/2004 - 08/08/2004
    - 08/08/2004 - 08/15/2004
    - 08/15/2004 - 08/22/2004
    - 08/22/2004 - 08/29/2004
    - 08/29/2004 - 09/05/2004
    - 09/05/2004 - 09/12/2004
    - 09/12/2004 - 09/19/2004
    - 09/19/2004 - 09/26/2004
    - 09/26/2004 - 10/03/2004
    - 10/03/2004 - 10/10/2004
    - 01/02/2005 - 01/09/2005
    - 01/09/2005 - 01/16/2005
    - 01/30/2005 - 02/06/2005
    - 01/01/2006 - 01/08/2006


Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland