Jump to content

Using Inputbox as Radio buttons?


Recommended Posts

I'm trying to use some inputboxes as radio buttons.

This code seems to work ok, but don't know if there's is a better 'option'

#include <GUIConstantsEx.au3>
#include <GuiConstants.au3>

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

Global $aInput[4]

$hGUI = GUICreate("Test", 500, 500)

$aInput[0] = GUICtrlCreateInput("Some text 1a", 10, 10, 200, 24)
$aInput[1] = GUICtrlCreateInput("Some text 1b", 10, 40, 200, 24)

$aInput[2] = GUICtrlCreateInput("Some text 2a", 10, 80, 200, 24)
$aInput[3] = GUICtrlCreateInput("Some text 2b", 10, 110, 200, 24)

GUISetState(@SW_SHOW, $hGUI)

While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop
   EndSwitch
WEnd

Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
   Local $nNotifyCode = BitShift($wParam, 16) ; high word
   Local $nID = BitAND($wParam, 0xFFFF) ; low word
   Local $hCtrl = $lParam

   Switch $nID
      Case $aInput[0]
         Switch $nNotifyCode
            Case $EN_SETFOCUS
               GUICtrlSetBkColor($aInput[0], 0x98FB98)
               GUICtrlSetBkColor($aInput[1], 0xD3D3D3)
         EndSwitch
      Case $aInput[1]
         Switch $nNotifyCode
            Case $EN_SETFOCUS
               GUICtrlSetBkColor($aInput[0], 0xD3D3D3)
               GUICtrlSetBkColor($aInput[1], 0x98FB98)
         EndSwitch

      Case $aInput[2]
         Switch $nNotifyCode
            Case $EN_SETFOCUS
               GUICtrlSetBkColor($aInput[2], 0x98FB98)
               GUICtrlSetBkColor($aInput[3], 0xD3D3D3)
         EndSwitch
      Case $aInput[3]
         Switch $nNotifyCode
            Case $EN_SETFOCUS
               GUICtrlSetBkColor($aInput[2], 0xD3D3D3)
               GUICtrlSetBkColor($aInput[3], 0x98FB98)
         EndSwitch
   EndSwitch

    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Would you recommend using _WinAPI_GetFocus instead?

 

 

Thanks,

 

Link to comment
Share on other sites

10 minutes ago, robertocm said:

I'm trying to use some inputboxes as radio buttons.

I'm not sure if you're aware of it or not, but that sounds really silly, use radio buttons as radio buttons. Why would you deliberately make things complicated when there's a simpler way to do it?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This is more or less the situation:

- There is one product description in the GUI

- This product has around 15 fields of components text information, to display in the GUI

- But i have 2 sources of text information for each component (like two database tables)

I want to dispaly both sources and also give the possibility of chosing between one of 'options'.

I also looking for a way to make possible the editing of the selected info, before generating a report.

Link to comment
Share on other sites

Here is another option.

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WinAPIDlg.au3>

Global $g_idFocus, $g_aInput[2][2]
Global $g_hWnd = GUICreate("Test", 500, 500)

$g_aInput[0][0] = GUICtrlCreateInput("Some text 1a", 10, 10, 200, 24)
$g_aInput[0][1] = GUICtrlCreateInput("Some text 1b", 10, 40, 200, 24)

$g_aInput[1][0] = GUICtrlCreateInput("Some text 2a", 10, 80, 200, 24)
$g_aInput[1][1] = GUICtrlCreateInput("Some text 2b", 10, 110, 200, 24)

GUISetState()

AdlibRegister("_Highlight")

While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop
   EndSwitch
WEnd

Func _Highlight()
    ;~ Get currently focused control id
    $idFocus = _WinAPI_GetDlgCtrlID(ControlGetHandle($g_hWnd, "", ControlGetFocus($g_hWnd, ""))) ;~ Get Control Id
    ;~ Compare local control id and global control id to determine if the focused control has already been processed, stops flickering
        If $g_idFocus = $idFocus Then Return
    ;~ Set global control id to local control id to show focused control has begun processing, stops flickering
        $g_idFocus = $idFocus
    ;~ Search input array for focused control id
    Local $iSearch = _ArraySearch($g_aInput, $idFocus)
    ;~ If search is unsuccessful return
        If @error Then Return
    ;~ Highlights for focused control id and unfocused control id of the selected array index item
    If $g_aInput[$iSearch][0] = $idFocus Then
        GUICtrlSetBkColor($g_aInput[$iSearch][0], 0x98FB98)
        GUICtrlSetBkColor($g_aInput[$iSearch][1], 0xD3D3D3)
    Else
        GUICtrlSetBkColor($g_aInput[$iSearch][0], 0xD3D3D3)
        GUICtrlSetBkColor($g_aInput[$iSearch][1], 0x98FB98)
    EndIf
EndFunc

 

Link to comment
Share on other sites

21 hours ago, robertocm said:

Would you recommend using _WinAPI_GetFocus instead?

Hi robertocm :)

I tried _WinAPI_GetFocus() and it gave good results (code below) without the need of GUIRegisterMsg() or AdlibRegister() or $GUI_EVENT_PRIMARYDOWN (coupled with GUIGetCursorInfo() to retrieve the ID of the control that the mouse cursor is hovering over)

Also the _WinAPI_GetFocus() approach allows you to move through your Input controls using the Tab key, while the background colors change, which you can't do when using $GUI_EVENT_PRIMARYDOWN

As Melba23 wrote in one of your links : "It seems Input controls do not send messages, so we have to take a roundabout way..."

#include <GuiConstants.au3>
#include <WinAPISysWin.au3>

Global $idInput[2][5], $hInput[2][5], $hGetFocus = 0, $hGetFocus_Old = 0, $iRow = -20

$hGUI = GUICreate("Test", 500, 500)
For $i = 0 To 1
    For $j = 0 To 4
        $iRow += 30
        $idInput[$i][$j] = GUICtrlCreateInput("Some text " & ($i + 1) & Chr(97 + $j), _
            10, $iRow + $i*50, 200, 24)
        $hInput[$i][$j] = GUICtrlGetHandle($idInput[$i][$j])
    Next
Next

GUISetState(@SW_SHOW, $hGUI)

While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         ExitLoop
    EndSwitch

    $hGetFocus = _WinAPI_GetFocus()
    If $hGetFocus <> $hGetFocus_Old Then Focus_Changed()
WEnd

Func Focus_Changed()
    For $i = 0 To 1
        For $j = 0 To 4
            If $hGetFocus = $hInput[$i][$j] Then
                GUICtrlSetBkColor($idInput[$i][$j], 0x98FB98)
                For $k = 0 To 4
                    If $k <> $j Then GUICtrlSetBkColor($idInput[$i][$k], 0xD3D3D3)
                Next
                ExitLoop(2) ; exit both For loops
            EndIf
        Next
    Next
    $hGetFocus_Old = $hGetFocus
EndFunc

 

_WinAPI_GetFocus.png.64201d69a47843ee1b19419c7a09dbee.png

Edited by pixelsearch
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...