Generic Dialog window to be called from other windows

So I am trying to reduce some coding…so I have a number of windows that all perform similar functionality against various tables in my sqlite db…as I am doing error checking or confirmations or info windows…I am wanting to reuse the same code passing parameters to make this happen. That way I don’t have to copy the same code from window to window to windows basically. So my dialog calls are basically ShowModalWithin(some window) - since I am trying to make this generic I know the name of the window that I would be calling from but not sure how to define the passing parameter. In parenthesis I want to be the ShowModalWindow(some variable name would go here).

Here is what my generic code looks like below…And I know generates a compile error. Any ideas on how to solve/approach this?

Dim d As New MessageDialog //declare the MessageDialog object
dim b As MessageDialogButton //for handling the result
d.Icon = MessageDialog.GraphicCaution //display warning icon

d.ActionButton.Caption = “OK”
d.CancelButton.Visible = False //show the Cancel button
d.Title = “Info”
d.Message = msgTxt '<- msgTxt comes from passed parm.
d.Explanation = “”

b = d.ShowModalWithin(me) //display the dialog in the window

–Mickey A

Pass the parent as a parameter. Something like

Sub ShowModalWindow(Parent As Window, MessageText As String
  If (Parent Is Nil) = False Then
    Parent = Parent.TrueWindow
  End If

  // Setup dialog

  Var Choice As MessageDialogButton
  If Parent Is Nil Then
    Choice = Dialog.ShowModal
  Else
    Choice = Dialog.ShowModalWithin(Parent)
  End If

  // Act on Choice here
End Sub

There is no need to do this. If you pass nil as a Within() parameter you get the normal ShowModal behavior. I’ve never understood why I see this code in projects.

1 Like

I wasn’t aware. I’ve never tried it because I just assumed it would NilObjectException. Makes ShowModalWithin kind of pointless, since they could have just done ShowModal(Within As Window = Nil) and achieve the same effect. Since there are two signatures, I just assumed passing nil wouldn’t have worked.

Perhaps it’s one of those hidden features like Listbox.Cell(-1, -1)

Thanks for the method solution. Nil works when passed to method. But what is the system variable for the current window may I ask?

ShowModalWithin is to provide context for what window a modal is referring to. I wouldn’t recommend attaching to whatever the frontmost window might just happen to be. You should have a context for what window the modal refers to.