Adding buttons programmatically in visual basic .net

G

Guest

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
 
C

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/d...mwindowsformsdatagridtablestyleclasstopic.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
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?U2lsdmlh?= said:
Anybody know how implements de function button_click if in the designer mode the button doesn't exists?

\\\
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
///
 

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