Find which StringGadget has it’s text selected

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Find which StringGadget has it’s text selected

Post by Columbo »

I have 2 StringGadgets and I want to be able select the contents of one StringGadget or the other with my mouse and then determine which StringGadget has it’s text selected. Is this possible?

I have a button that populates the StringGadgets when pressed. I can’t use GetActiveGadget() because it shows the active gadget is the button. If I click in one of the StringGadgets and use GetActiveGadget() it still shows the active gadget as the button. I can’t use SetActiveGadget() because it could be either of the two that I want the text from.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: Find which StringGadget has it’s text selected

Post by eJan »

With the Little John's help.

Code: Select all

; Little John: http://www.purebasic.fr/english/viewtopic.php?p=375082#p375082

Define start.i, stop.i

OpenWindow(0, 0, 0, 322, 205, "StringGadget", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
StringGadget(1, 8, 10, 306, 20, "Bla Blib Blub")
StringGadget(2, 8, 40, 306, 20, "abc def")

Repeat
  Select WaitWindowEvent()
    Case #WM_LBUTTONUP           ; Mouse up
      If GetActiveWindow() = 0 And GetActiveGadget() = 1
        SendMessage_(GadgetID(1), #EM_GETSEL, @start, @stop)  ; get selected text
        Debug "'" + Mid(GetGadgetText(1), start + 1, stop - start) + "'"
      EndIf
      
      If GetActiveWindow() = 0 And GetActiveGadget() = 2
        SendMessage_(GadgetID(2), #EM_GETSEL, @start, @stop)  ; get selected text
        Debug "'" + Mid(GetGadgetText(2), start + 1, stop - start) + "'"
      EndIf

    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
Image
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Find which StringGadget has it’s text selected

Post by Columbo »

Thank you so much eJan. It works perfectly!

Cheers and stay safe!
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Post Reply