CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    41

    assigning procedures to a new form

    thanks craig for your help
    now I've got a new form created it dosn't have a GUI and I cant get a procedure to work for the click events of the buttons

    this doen't work

    Code:
    public sub newbutton (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newbutton.Click
    
    end sub
    so how do I do it?
    Last edited by Robbo99; June 17th, 2004 at 11:32 PM.

  2. #2
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146
    OK, not sure what you mean about not having a GUI but I assume it's not important.

    The problem looks to me like you've named a sub with the same name as your control. This strikes me as not being a good idea.

    Try this:

    Code:
    Public Sub NewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewButton.Click
    
    End Sub
    On a sidenote, as a general rule, it's a good idea to be specific when saying something doesn't work. "Something doesn't work" is generally a bit too broad to get to the crux of the problem

    Everybody on this group is always keen to help (well, everyone I've ever had contact with anyway) and the more information you can provide with respect to your problem makes it easier and faster for us to provide a response. Stuff like whether it compiles or not, or whether you get a run-time error and what the error is are good starting points.

    Anway, hope this helps and let me know if this doesn't fix the problem.

    Nathan.

  3. #3
    Join Date
    Mar 2004
    Posts
    41
    Good point Nathen (i will give it a shot)

    I tryed your code but the problem is that the button isan't reconised I have this code and i want to give the click event for btnOK an Event procedure eg: form1.backcolor = color.red

    Code:
    Public Sub NewAnimal()
                  Dim form1 As New Form
    
                  Dim btnOK As New Button
        
            btnOK.Text = "OK"
            ' Set the position of the button on the form.
            btnOK.Location = New Point(10, 10)
           
            form1.Text = "My Dialog Box"
            ' Display a help button on the form.
            form1.HelpButton = True
    
            ' Define the border style of the form to a dialog box.
            form1.FormBorderStyle = FormBorderStyle.FixedDialog
            ' Set the MaximizeBox to false to remove the maximize box.
            form1.MaximizeBox = False
            ' Set the MinimizeBox to false to remove the minimize box.
            form1.MinimizeBox = False
            ' Set the accept button of the form to button1.
            form1.AcceptButton = btnOK
            ' Set the cancel button of the form to button2.
            form1.CancelButton = btnCANCEL
            ' Set the start position of the form to the center of the screen.
            form1.StartPosition = FormStartPosition.CenterScreen
    
            ' Add button1 to the form.
            form1.Controls.Add(btnOK)
           
            ' Display the form as a modal dialog box.
            form1.ShowDialog()
        End Sub
    thanks Michael

  4. #4
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146
    Ahhhh, now I see. Not 100% sure why you are trying do things this way but that aside, try this:

    Code:
        Public Sub NewAnimal()
            Dim form1 As New Form
    
            Dim btnOK As New Button
    
            btnOK.Text = "OK"
            ' Set the position of the button on the form.
            btnOK.Location = New Point(10, 10)
    
            form1.Text = "My Dialog Box"
            ' Display a help button on the form.
            form1.HelpButton = True
    
            ' Define the border style of the form to a dialog box.
            form1.FormBorderStyle = FormBorderStyle.FixedDialog
            ' Set the MaximizeBox to false to remove the maximize box.
            form1.MaximizeBox = False
            ' Set the MinimizeBox to false to remove the minimize box.
            form1.MinimizeBox = False
            ' Set the accept button of the form to button1.
            form1.AcceptButton = btnOK
            ' Set the cancel button of the form to button2.
            'form1.CancelButton = btnCANCEL
            ' Set the start position of the form to the center of the screen.
            form1.StartPosition = FormStartPosition.CenterScreen
    
            ' Add button1 to the form.
            form1.Controls.Add(btnOK)
    
            AddHandler btnOK.Click, AddressOf SetColor
    
    
            ' Display the form as a modal dialog box.
            form1.ShowDialog()
        End Sub
    
    
        Public Sub SetColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
            CType(sender, Button).FindForm.BackColor = Color.Red
        End Sub
    You use the AddHandler procedure to add the handler for the control you want capture an event for. The Sub declaration (in this example SetColor) must have the same parameters as the event, hence the sender and e arguments to the sub.

    I removed the btnCancel line because you hadn't declared a btnCancel.

    Hope this helps,

    Nathan.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured