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

|
0 Comments:
Post a Comment
<< Home