Results 1 to 13 of 13
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AcceptButton not working? (VB.Net 2003)

    I have a form with several buttons on it. I selected the form and set the AcceptButton property to the button I want to be the default. In the form designer the button I want to be the default is highlighted and if I check the code there is a line in the code describing the form that is "Me.AcceptButton = Me.<button name>" but when I run the code a totally different button is highlighted and acts as the default. Also, at various places in the code where I want the default button to change I have "Me.AcceptButton = Me.<new button name>" but the default never chages.

    I also do this on a child form and it works there but for some reason AcceptButton on the main form is being ignored.

    Any ideas?

    Thanks,
    Thanks,

    C DeWitt

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    Are you calling a Focus() method to set the focus to a different button - perhaps in the Form_Load() event?

    Also, verify that your button is not named "AcceptButton" or it will cause problems since AcceptButton is also the name of a form property.

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    No on both counts. I don't use the Focus method anywhere. I searched the code for it anyway and didn't find it. I don't have button names anywhere near that.

    Thanks
    Thanks,

    C DeWitt

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: AcceptButton not working? (VB.Net 2003)

    Are all your handlers properly connected and handling the right controls? If you ever cut and paste controls, they can be garbled.
    Charlotte

  5. #5
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    Thanks for the info, I did create a button on the graphic designer, copy it and paste it but then went and set all its properties the way I wanted. I looked at the code generated and couldn't see anything amiss, maybe you can -

    This is the button I want to have focus -
    'SD_Button
    '
    Me.SD_Button.Location = New System.Drawing.Point(8, 24)
    Me.SD_Button.Name = "SD_Button"
    Me.SD_Button.Size = New System.Drawing.Size(105, 23)
    Me.SD_Button.TabIndex = 1
    Me.SD_Button.Text = "Use SD Search"
    '

    This is the button that's getting focus -
    '
    'Exit_Button
    '
    Me.Exit_Button.Location = New System.Drawing.Point(24, 272)
    Me.Exit_Button.Name = "Exit_Button"
    Me.Exit_Button.Size = New System.Drawing.Size(105, 23)
    Me.Exit_Button.TabIndex = 6
    Me.Exit_Button.Text = "Exit"
    '

    Here is the form -
    '
    'QEDesignCopy_Form
    '
    Me.AcceptButton = Me.SD_Button
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(1000, 333)
    Me.Controls.Add(Me.SD_Patern)
    Me.Controls.Add(Me.SearchPaternLabel)
    Me.Controls.Add(Me.Copyright_Label)
    Me.Controls.Add(Me.Find_Design_GrpBox)
    Me.Controls.Add(Me.Exit_Button)
    Me.Controls.Add(Me.Copy_Files_Button)
    Me.Controls.Add(Me.Destination_Path_Label)
    Me.Controls.Add(Me.Find_Destination_Button)
    Me.Controls.Add(Me.Source_Path_Label)
    Me.Controls.Add(Me.Prompt)
    Me.Name = "QEDesignCopy_Form"
    Me.Text = "QE Design Copy"
    Me.Find_Design_GrpBox.ResumeLayout(False)
    Me.ResumeLayout(False)

    The SD_Button is part of the Find_Design_GrpBox-
    '
    'Find_Design_GrpBox
    '
    Me.Find_Design_GrpBox.Controls.Add(Me.Browse_Butto n)
    Me.Find_Design_GrpBox.Controls.Add(Me.SD_Button)
    Me.Find_Design_GrpBox.Location = New System.Drawing.Point(16, 64)
    Me.Find_Design_GrpBox.Name = "Find_Design_GrpBox"
    Me.Find_Design_GrpBox.Size = New System.Drawing.Size(120, 100)
    Me.Find_Design_GrpBox.TabIndex = 7
    Me.Find_Design_GrpBox.TabStop = False
    Me.Find_Design_GrpBox.Text = "Find Design"
    '
    The handlers themselves where all made by double clicking on the button in the graphic.

    Private Sub SD_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SD_Button.Click

    and

    Private Sub Exit_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit_Button.Click
    End
    End Sub

    One curious thing as I've said before it that when I set Me.AcceptButton = Me.SD_Button using the GUI on the form designer, the SD_Button was highlighted like it should be, but when I run it, the Exit button is highlighted.

    If you can see anything please let me know.

    Thanks
    Thanks,

    C DeWitt

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: AcceptButton not working? (VB.Net 2003)

    I only took a quick look through the code, but I notice that you're adding the SD_Button to the Find_Design_GrpBox controls collection rather than the main controls collection, which contains the exit button. Maybe that's why you're having the problem, the SB_Button isn't in the same tab collection.
    Charlotte

  7. #7
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    I thought AcceptButton was a property of the form not a grouping. Besides, I also have a problem programaticly assigning different buttons to the property even though those buttons are on the form proper at the same level as the Exit button, not in a group.
    Thanks,

    C DeWitt

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    I can see the event handler itself, but I can't see where the event handler is associated with the button.It may only bee because you haven't included all the code, but there should be a line when SD_Button is defined which calls SD_Button_Click.

    To add event handlers to a control
    .Select the controls
    .In the property window, select the "methods" (a lightning symbol)
    .Double click the event you want handled (eg click)

    That should create both the handler and associating the control with the handler
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  9. #9
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    I did include the header for the procedure -

    Private Sub SD_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SD_Button.Click

    I didn't include all the code there were too many lines.
    Thanks,

    C DeWitt

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    You have the procedure, but I didn't see the code you need to link the button to the procedure. (I'm at work now so it's a little easier)<pre>this.SD_Button.Location = new System.Drawing.Point(8,24);
    this.SD_Button.Name = "SD_Button";
    this.SD_Button.Size = new System.Drawing.Size(105,23);
    this.SD_Button.TabIndex = 1;
    this.SD_Button.Text = "Use SD Search";
    this.SD_Button.Click += new System.EventHandler(this.SD_Button_Click);</pre>

    If you DO have that line of code, then the problem lies elsewhere.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #11
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    Geoff, that only applies to C# - he had Handles SD_Button.Click after the SD_Button_Click() sub.

    Chris, try removing the button from the group and see if you have the same problem.

  12. #12
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    Chris,
    When you say that "a totally different button is highlighted and acts as the default", do you mean that this other button is highlighted when you press Enter, or does it already have the focus? I notice that the button you want to use as the AcceptButton has Tabindex 1- does the button that's being triggered have Tabindex 0?
    If another Button has focus, then the AcceptButton will not work when you press Enter. Instead, the button with the focus will have its click event triggered (which, generally, is what a user would expect, I think).
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  13. #13
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Mundelein, Illinois, USA
    Posts
    198
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: AcceptButton not working? (VB.Net 2003)

    Rory -
    By highlight I mean that when the app starts the Exit_Button has a heavy border arround it. The SD_Button that I want to be default has a light border and if I do nothing else after it starts bugt hit enter the Exit_Button_Click routine is run.

    As for TabIndex -
    Me.SD_Button.TabIndex = 1
    Me.Exit_Button.TabIndex = 6


    MarkJ -

    Yes, moving the buttons out of the group box fixes the problem. Too bad, I was only using it for the graphic effect but I guess it has to go.

    Thanks all,
    Thanks,

    C DeWitt

Posting Permissions

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