Accept button

pachjo

Well-known member
Joined
Dec 12, 2006
Messages
370
Programming Experience
10+
I see on forms you can specify an default accept button, is this possible on controls?

For example I have a series of panels that have transcation controls, text boxes etc and a save button and I have to click the save button to commit the transaction.

Apart from shortcut keys is there another way that if in the panle and the user presses the enter key the application fires the save button code?

Thanks
 
Choose the button in forms AcceptButton property (Design view property)
The property description is:
Gets or sets the button on the form that is clicked when the user presses the ENTER key.
 
Yes but my form has a tab control with 8 pages all of which have many buttons.

Do I create an invisible dummy button and assign that to the AcceptButton and then have a select statement decide which transaction panel was active at the time?
 
You can also assign the AcceptButton property in code, for example each time user select a different tabpage. (use tabcontrols selectedindexchanged event)
 
Mmm I like that idea...

But before I enter a load of code, as I am not too sure if what I am thinking would be a messy approach?

So I have 8 tabpages each has a variable number of panels grouping various controls along with a save button.

So using your suggestion would this be valid or very messy?

Query the selected tabpage

Have a select case statement that determines which control is currently selected (so I might need to check say up to 5 controls per panel) and then set its related save button to the AcceptButton property?


Thanks
 
This is what I would do:
VB.NET:
    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles TabControl1.SelectedIndexChanged
        Select Case TabControl1.SelectedIndex
            Case 0
                Me.AcceptButton = tabpage1savebutton
            Case 1
                Me.AcceptButton = tabpage2savebutton
            Case 2
                Me.AcceptButton = tabpage3savebutton
            Case 3
            Case 4
            Case 5
            Case 6
            Case 7
        End Select
    End Sub
 
Hi, yes I understand that bit, but remember each tabpage has at least 3 panels that contain save buttons for different transactions so for example:

Tabpage1
panel1 has text1, text2, text3, datetimepicker, checkbox, save button
panel2 has text1, datetimepicker, save button
panel3 has text1, text2, combobox, datetimepicker, save button

Tabpage2
panel1 has text1, checkbox, save button
panel2 has text1, datetimepicker, save button
panel3 has text1, combobox, datetimepicker, save button

Tabpage3
panel1 has text1, datetimepicker, checkbox, save button
panel2 has datetimepicker, save button
panel3 has text1, text2, combobox, save button

Therefore I need to determine not only which tabpage is selected but also which group panel is in use. Maybe I did not explain myself so good:eek:

So what I am concerned about is putting in a load of condition code for what is a convienience for the user? Erm is the overhead woth the result?
 
Well, I missed that you have many Save buttons each tabpage. That may be a real problem since Panels don't take focus, so you may never know what Panel you're in.
 
Yeah, that was my thought...and problem. I guess I shall have to omit that bit of functionality :(

hey ho.....

thanks for you help all the same ;)
 
hm, not sure if this is the best way to do it, but create a class which has some variables that can be accessed across the project, and then with an OnEnter event for any specific panel, set a boolean indicating which panel is currently selected.

then when you click save check all the booleans...

it will be a tad messy i think, but at least you would get to keep the functionality.

just a thought :)
adam
 
Mmmmm I like the cut of your jib sir!

I shall have a mull over that and try it to see the results and get back to you.

Cheers ;)
 
Ho ho tis done :D

Where applicable I set the AcceptButton when the panel Enter event fires. Where the panel approach did not work I set the Enter event code of the required fields.

So now I have exactly what I was trying to acheive....thanks folks ;)
 
Well, I missed that you have many Save buttons each tabpage. That may be a real problem since Panels don't take focus, so you may never know what Panel you're in.

True, but some control in the panel can take the focus. And some control knows what container it is in.. It ought to be easy enough to write a generic event handler that:

Upon gaining focus, a control starts a search of its peers.
(For Each ctrl as Control in focusedcontrol.Parent.Controls)

One of them shall be a button, with a Tag property set to be a string constant "AcceptButton"
If TypeOf(ctrl) is button AndAlso ctrl.Tag Is ACCEPT_BUTTON_CONST_STR ' reference compare

Then nominate that button as the accept button of the form///
 
Back
Top