DirBox and FileBox Cancel

Post your Gambas programming questions here.
Post Reply
User avatar
thatbruce
Regular
Posts: 295
Joined: Sat Sep 04, 2021 11:29 pm

DirBox and FileBox Cancel

Post by thatbruce »

How can I detect that the user has clicked on the cancel button in a DirBox? This possibly happens with the FileBox also, but I haven't checked.
I want to propagate the cancellation up a couple of levels of the stack trace.
The only events I know of are _Click and _Change. Neither of which are raised when the Cancel button is clicked?

tia
thatbruce
BruceSteers
Legend
Posts: 2110
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: DirBox and FileBox Cancel

Post by BruceSteers »

You can access the internal ButtonBox and override the click event (the ButtonBox Click not the DirBox Click)..


Private $hObs As Observer

Public Sub Form_Open()

  $hObs = New Observer(DirBox1.Children[0]) As "DBox"  ' get the DirBox's ButtonBox events

End

'' This is a copy of the DirBox Button_Click() event adapted to not use it's internal variables $sTitle and $sText

Public Sub DBox_Click()

  Stop Event

  Dim sSaveTitle As String
  Dim bCancel As Boolean
  
  sSaveTitle = Dialog.Title
  
  Dialog.Title = DirBox1.Title
  Dialog.Path = DirBox1.Path
  
  bCancel = Dialog.SelectDirectory()
  
  Dialog.Title = sSaveTitle
  
  If Not bCancel Then
    DirBox1.Path = Dialog.Path
    Object.Raise(DirBox1, "Click")  ' use Object.Raise to raise normal Click event
  Else 
    Debug "cancelled"  ' or it got cancelled
  Endif
  
End

BruceSteers
Legend
Posts: 2110
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: DirBox and FileBox Cancel

Post by BruceSteers »

Or an Auto-Inherited override ,
Save this as DirBox.class in your project...


' Gambas class file

Export

Event Cancelled

Public Sub Button_Click()

  Dim sSaveTitle As String
  Dim bCancel As Boolean

  sSaveTitle = Dialog.Title

  Dialog.Title = Me.Title
  Dialog.Path = Me.Path

  bCancel = Dialog.SelectDirectory()

  Dialog.Title = sSaveTitle

  If Not bCancel Then
    Me.Path = Dialog.Path
    Object.Raise(Me, "Click")
  Else
    Raise Cancelled
  Endif

End




That will add DirBox1_Cancelled() event


Or a more condensed version...

' Gambas class file  (DirBox.class)

Export

Event Cancelled

Public Sub Button_Click()

  ' note the current path, open the dialog, then if path is the same the user must have cancelled.
  Dim sPath As String = Me.Path
  Super.Button_Click
  If sPath = Me.Path Then Raise Cancelled

End
User avatar
thatbruce
Regular
Posts: 295
Joined: Sat Sep 04, 2021 11:29 pm

Re: DirBox and FileBox Cancel

Post by thatbruce »

Well after some testing and soul-searching I finally went with option 2, the auto-inherit.

I discounted the shortened version as there is nothing wrong with them selecting the same directory.

Thanks for the options!
I wonder if the native DirBox should have that event as I can see other cases where it could apply. In fact any control that uses a child control that is cancellable.

b
BruceSteers
Legend
Posts: 2110
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: DirBox and FileBox Cancel

Post by BruceSteers »

You're welcome :)
Post Reply