Remove sound on error - stringgadget

Just starting out? Need help? Post your questions and find answers here.
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Remove sound on error - stringgadget

Post by GenRabbit »

How can I remove the Window error sound and text helper when I push like wrong type of char into a stringGadget, or go beyond limit with chars?

Code: Select all

EnableExplicit
Structure Ini_Structure
  MinutesFromTheBeginning.i
  MonthsInYear.i
  HourInDay.i
  MinutesInHour.i
  DaysInWeek.i
  List NameOfDays.s()
  List NameOfMonths.s()
  List DaysInMonths.i()
EndStructure
Define ini.Ini_Structure

	Procedure.w CreateDateAndTime(*Settings.Ini_Structure)
		Protected.i WindowID, Event
		Protected.i cbMonthAndDays, btAddMonth
		Protected.i cbDays, btAddDay
		Protected.i stgMinutesInHour, txtMinutesInHour
		Protected.i stgHourInDay, txtHoursInDya
		Protected.i StgMinutesFromTheBeginning, txtMinutesFromTheBeginning
		Protected.i x
		Protected.s sTemp
		WindowID = OpenWindow(#PB_Any,0,0,700,240,"Create Dates", #PB_Window_WindowCentered | #PB_Window_TitleBar | #PB_Window_SystemMenu)
		cbMonthAndDays = ComboBoxGadget(#PB_Any,20,20,400,25,#PB_ComboBox_Editable)
		btAddMonth = ButtonGadget(#PB_Any, 460,20,200,25,"Add Month and Number of Days")
		
		cbDays = ComboBoxGadget(#PB_Any,20,60,400,25,#PB_ComboBox_Editable)
		btAddDay = ButtonGadget(#PB_Any, 460,60,200,25,"Add Day")
		
		stgMinutesInHour = StringGadget(#PB_Any,20,100,40,25,"",#PB_String_Numeric)
		txtMinutesInHOur = TextGadget(#PB_Any, 70,100,200,25,"Minutes in an Hour")
		SetGadgetAttribute(stgMinutesInHour,#PB_String_MaximumLength,3)
		
		stgHourInDay = StringGadget(#PB_Any,20,140,40,25,"",#PB_String_Numeric)
		txtMinutesInHOur = TextGadget(#PB_Any, 70,140,200,25,"Hour in a Day")
		SetGadgetAttribute(stgHourInDay,#PB_String_MaximumLength,3)
		
		StgMinutesFromTheBeginning = StringGadget(#PB_Any,20,180,120,25,"",#PB_String_Numeric)
		txtMinutesFromTheBeginning = TextGadget(#PB_Any, 160,180,200,25,"Minutes from the Beginning")
		SetGadgetAttribute(StgMinutesFromTheBeginning,#PB_String_MaximumLength,15)
		
		SetGadgetText(stgHourInDay, FormatNumber(*Settings\HourInDay,0))
		SetGadgetText(stgMinutesInHour, FormatNumber(*Settings\MinutesInHour,0))
		SetGadgetText(stgMinutesFromTheBeginning, FormatNumber(*Settings\MinutesFromTheBeginning,0,"",""))
		
		
		SelectElement(*Settings\NameOfMonths(),0)
		SelectElement(*Settings\DaysInMonths(),0)
		While x < *Settings\MonthsInYear
			sTemp = *Settings\NameOfMonths() + "," + FormatNumber(*Settings\DaysInMonths(),0)
			AddGadgetItem(cbMonthAndDays,-1,sTemp)
			NextElement(*Settings\NameOfMonths())
			NextElement(*Settings\DaysInMonths())
			x + 1	
		Wend
		
		x = 0
		SelectElement(*Settings\NameOfDays(),0)
		While x < *Settings\DaysInWeek		
			AddGadgetItem(cbDays, -1, *Settings\NameOfDays())
			NextElement(*Settings\NameOfDays())
			x + 1
		Wend
		
		SetGadgetText(cbDays, "Days")
		SetGadgetText(cbMonthAndDays, "Month,Days")
		Repeat
			Event = WaitWindowEvent()
			Select Event
				; Gadget is activated	
				Case #PB_Event_Gadget
					
					
				Case #PB_Event_CloseWindow
					Break
			EndSelect
		ForEver	
		SetWindowCallback (0 ,stgMinutesInHour)
	EndProcedure

Ini\MinutesFromTheBeginning=233335017
Ini\MonthsInYear=14
Ini\DaysInWeek=8
Ini\HourInDay=24
Ini\MinutesInHour=60

AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Solan"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Dellios"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Rande"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Varian"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Mosicante"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Uliande"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Horitan"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Belizadore"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Koloss"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Jarajan"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Polokk"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Quartani"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Romante"
AddElement(Ini\NameOfMonths())
Ini\NameOfMonths()="Morvika"

AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=24
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=26
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=26
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=33
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=29
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=31
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=27
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=32
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=28
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=28
AddElement(Ini\DaysInMonths())
Ini\DaysInMonths()=26

AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Morrasday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Nirmoday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Yranday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Janmarday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Turenday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Ermaday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Ulvakday"
AddElement(Ini\NameOfDays())
Ini\NameOfDays()="Cortenday"
CreateDateAndTime(@ini)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Remove sound on error - stringgadget

Post by RASHAD »

Hi GenRabbit
1- Down the sound level while you are running your application then restore the default back at end
OR
2- Play with Windows Default theme [ Not recommended ]
Egypt my love
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Remove sound on error - stringgadget

Post by GenRabbit »

In VB6 I could intercept the keypress, set it to 0, and zero sound where made from windows.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Remove sound on error - stringgadget

Post by RASHAD »

Hi again
Search the forum for how to Mute System sound
Egypt my love
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Remove sound on error - stringgadget

Post by Axolotl »

Hi,
maybe I do not understand what you really want to achieve.
Anyway this is a way of subclassing, to get full control over every keystroke...... (see by yourself)

Code: Select all

;············································································
;' StringGadget() Subclassing ...  (Win Only) 
;' 
;'  but the old-fashioned way. For modern subclassing search for 
;'     Subclassing Controls with ComCtl32.dll version 6 (win only)
;'     LINK: https://www.purebasic.fr/english/viewtopic.php?p=601825#p601825
;' 

EnableExplicit

Procedure StringGadgetCallback(hWnd, uMsg, wParam, lParam)   
  Protected oldproc = GetProp_(hWnd, "oldproc")
  Protected hParent = GetParent_(hWnd)
  Protected hNext
   
  Select uMsg
    Case #WM_NCDESTROY
      RemoveProp_(hWnd, "oldproc")
         
    Case #WM_KEYDOWN
      Select wParam
        Case #VK_RETURN
          SetFocus_(GetNextDlgTabItem_(hParent, hWnd, #False))
          ProcedureReturn 0
           
        Case #VK_DOWN
          hNext = GetNextDlgTabItem_(hParent, hWnd, #False)
          If hNext = GadgetID(2)
            hNext = GetNextDlgTabItem_(hParent, hNext, #False)
          EndIf
          SetFocus_(hNext)
          ProcedureReturn 0
           
        Case  #VK_UP
          hNext = GetNextDlgTabItem_(hParent, hWnd, #True)
          If hNext = GadgetID(2)
            hNext = GetNextDlgTabItem_(hParent, hNext, #True)
          EndIf
          SetFocus_(hNext)
          ProcedureReturn 0
           
      EndSelect
         
    Case #WM_CHAR      : Debug " Char: "+Str(wParam)
      Select wParam
        Case  8 ; BACKSPACE is allowed 
        Case 44 : wParam = 46          ;' change comma to point
        Case 48 To 57                  ;' numbers are allowed 
        Default                        ;: Debug "do nothing " + Chr(wParam) + " - " + Str(wParam) 
          ProcedureReturn 0    ; ignore all other char's
      EndSelect   
      
    Default 
      If hWnd = GadgetID(2)  ;: Debug "Gadget is read only"
        HideCaret_(hWnd)
      EndIf   

  EndSelect  ;' uMsg
   
  ProcedureReturn CallWindowProc_(oldproc, hWnd, uMsg, wParam, lParam)
EndProcedure

Procedure SubClassingStringGadgetWithSetProp(Gadget)
  Protected hGadget
  hGadget = GadgetID(Gadget)
  ProcedureReturn SetProp_(hGadget, "oldproc", SetWindowLongPtr_(hGadget, #GWL_WNDPROC, @StringGadgetCallback()))
EndProcedure

; Example

Define i
OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

StringGadget(1, 10,  40, 200, 20, "")
StringGadget(2, 10,  70, 200, 20, "Read Only")
StringGadget(3, 10, 100, 200, 20, "")
StringGadget(4, 10, 130, 200, 20, "")
StringGadget(5, 10, 160, 200, 20, "")

SetActiveGadget(1)

For i = 1 To 5
  SubClassingStringGadgetWithSetProp(i)
Next

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow : Break
    Case #PB_Event_Gadget
      ;Debug "Gadget: " + Str(EventGadget()) + " : Type: " + Str(EventType())
  EndSelect
ForEver
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Remove sound on error - stringgadget

Post by GenRabbit »

Yes, this is it. I tried my own version, but keep getting an 1400 error.
As far as I can remember this is the way it works in VB6 and others, but here, no chance. Don't know if Win11 requires something special.

Code: Select all

EnableExplicit
#MW = 1
Global.i Addr
Define.i Event, ErrMaster
Global.i Integer1,integer2
	Procedure Callback(hWnd.i, uMsg.i, WParam.i, LParam.i) 
	; Windows fills the parameter automatically, which we will use in the callback...
	
		Debug "Run"
		Select uMsg
			Case #WM_CHAR
				Debug "keypush"
				Select WParam
			        Case  8 ; BACKSPACE is allowed 
			        Case 48 To 57                  ;' numbers are allowed 
			        Default
			        	WParam = 0    ; ignore all other char's
			    EndSelect 
		EndSelect 
	
		ProcedureReturn CallWindowProc_(integer2, hwnd, uMsg, wParam, lParam)
	EndProcedure 


If OpenWindow(#MW,100,100,600,200,"test")
	Integer1 = StringGadget(#PB_Any,20,20,120,25,"",#PB_String_Numeric)
	SetLastError_(0)
	integer2 = SetWindowLongPtr_(Integer1, #GWL_WNDPROC, @Callback())
	If integer2
	Else
		ErrMaster = GetLastError_()
		Debug "Error. " + FormatNumber(integer2,0) + "/"+ FormatNumber(ErrMaster,0)
	EndIf
	Repeat
		Event = WaitWindowEvent()
		Select Event
			; Gadget is activated	
			Case #PB_Event_Gadget
			Case #PB_Event_CloseWindow
				Break
		EndSelect
	ForEver
EndIf
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Remove sound on error - stringgadget

Post by Axolotl »

Hi
maybe you should overthink your variable names.....

Anyway, you have to change LINE 29 to this: (because if you use #PB_ANY the return value of any Gadget() is the Number not a handle (identifier).

Code: Select all

	integer2 = SetWindowLongPtr_(GadgetID(Integer1), #GWL_WNDPROC, @Callback())
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Remove sound on error - stringgadget

Post by GenRabbit »

Thanks that fixed it. This program was just a to test why it didn't work.
The final will not use INteger1, Integer2 and so on. Had a brain-fart in the naming department when I quickly wrote it.

Crap, I thought using #PB_any would give you the real handle... Doh...
That makes #PB_ANY pretty useless... :(
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Remove sound on error - stringgadget

Post by Axolotl »

GenRabbit wrote: Mon Dec 02, 2024 3:43 pm That makes #PB_ANY pretty useless... :(
I would not say that. It is still a different way to code. The return value is the Window or Gadget Number and can be used with (almost) all PB-Procedures to deal with the Window or Gadget.
In some cases when you use OS-specific stuff you need to convert the number to a handle (Identifier). Therefore you use WindowID() or GadgetID().
This is staightforward from my perspective.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Remove sound on error - stringgadget

Post by Fred »

GadgetID() is the way to get the real handle, the fact it returns one when using indexed id is just a shortcut to it.
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Remove sound on error - stringgadget

Post by Axolotl »

I could have made it easier for myself, everything is described in detail here:
Handles and Numbers
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply