ChrisR has asked for the wisdom of the Perl Monks concerning the following question:

I have been trying to change the default button in a Win32::GUI script and have not had any luck. By default, I mean the button that gets its Click event called when I hit the enter key. As I understand it, the -default option sets the dark border around the button and the -ok option sets the action default. While getting the dark border around the button is nice, it is not necessarry in my application. I have tried quite a few different things and, of course, none of them worked. Here is an example of my last feable attmept:
use strict; use Win32::GUI; my $mainform = Win32::GUI::Window->new(-name=>'main',-text=>'test',-wi +dth=>200,-height=>200,-dialogui=>1); my $button1 = $mainform->AddButton(-name=>'button1',-top=>10,-left=>10 +,-text=>"button1",-ok=>1); my $button2 = $mainform->AddButton(-name=>'button2',-top=>40,-left=>10 +,-text=>"button2",-ok=>0); $mainform->Show(); Win32::GUI::Dialog(); exit; sub button1_Click { $mainform->MessageBox("You clicked button1. Changing the default +to button2.","button1"); $button1->Change(-ok=>0); $button2->Change(-ok=>1); } sub button2_Click { Win32::GUI->MessageBox("You clicked button2. Changing the default + to button1.","button2"); $button2->Change(-ok=>0); $button1->Change(-ok=>1); } sub main_Termintate { $mainform->Hide(); return -1; }
I have tried all sorts of combinations using the -default and -ok options like only setting one or the other or setting them to different values and have gotten nowhere. Is this even possible? I thought perhaps I needed to refresh the window, but I can't seem to find a function to do that other than those dealing with redraws. I tried to Hide() the window before making the change and then Show() the window after the change which makes the change of -default show up but nothing more. Any help would be greatly appreciated.

Thanks,
Chris

Replies are listed 'Best First'.
Re: How to change the default (ok) button in Win32::GUI
by ikegami (Patriarch) on Apr 20, 2005 at 21:53 UTC
    Don't default buttons only apply to Dialog boxes, as opposed to Windows in general?
      I have found no information / documentation that suggests default buttons apply only to dialog boxes. Even so, if you use Win32::GUI::DialogBox instead of Win32::GUI::Window, the resulting behavior is the same. Also, according to the docs for Window:
      -dialogui => 0/1 Act as a dialog box.
      I have been searching for an answer in the Win32.pm and GUI.pm code but can't find anything that leads me to the answer. I get the feeling that I need to manipulate the window somehow after changing the button but I'm just guessing.

        I did a little testing, and Enter did nothing when it was a Win32::GUI::Window, but it pressed button 1 when it was a Win32::GUI::DialogBox.

        I also could not change the default button. I suspect it cannot be changed once the window is created.

        That doesn't mean it's hopeless. What you can do is capture Enter and give it the behaviour you want. In fact, I think CDialog in MFC does just that. You could check the MFC sources for CDialog If you have access to VC++.

Re: How to change the default (ok) button in Win32::GUI
by jplindstrom (Monsignor) on Apr 21, 2005 at 11:19 UTC
    The -dialogui option is necessary to get the Window to act like a DialogBox, i.e. process the basic navigation messages. Why that isn't the default is beyond me, since that's almost always the desired behaviour.

    But that's not why it doesn't work. The problem is that your example only has Button controls in it.

    Pressing Return (or Space) when a button has focus sends the Click event to that button. If some other control has focus, then the default button gets the Click event.

    /J

      I use the -dialogui option on all my windows and I would have expected it to be the default also. I must disagree that the problem is that the example only contains the buttons. The project I'm working on has one main window that contains many tabstrips and controls that are hidden and shown at runtime depending on the tab chosen to give the effect of tabs within tabs and different pages. I have modified the example to include two textfields and the result is the same. Here's the code: As you can see in the code I have also added the Update method to the buttons and window after the changes are made but that does not help either. I also tried using -addstyle=>BS_DEFPUSHBUTTON which also didn't help. What worries me with that approach would be that I don't know how to remove the style after it is applied. But it doesn't work anyway, so that's not going to be an issue.