Modal Widow Issue

I just posted this question but since I thought I found the answer, I deleted it but alas…

Anyways, I’ve created a custom DialogMessage using a Sheet Window and the “ShowModal(parent window)” method. The window has a TextArea and two Buttons. I want to repurpose the window so instead of hard coding the Text and Captions, I created a method with two parameters; one for the message and one for the Button Caption. The problem is, it only works after the first run. When I run it the first time, the window appears but the TextArea is blank and the Buttons are set to their defaults. But each time I run it after that, the window displays correctly. Does anyone know why this is happening? Thanks!

First run:

Second run:

Show us your code where you’re calling ShowModal and setting the values.

1 Like

ShowModal is called from within a DisplayDialog method that I created with two parameters for the message and the button caption.

DialogWindow.ShowModal(Window3)
DialogWindow.Button1.Caption = theCaption
DialogWindow.TextArea1.Text = theMessage

This is the code block that calls the DisplayDialog method and sets the parameter values:

chordNumber = chordArray.IndexOf(ChordNameField.Text)
If chordNumber <> - 1 Then
  DisplayDialog("Add To Library", ChordNameField.Text + " already exists in the library. Would you like to add it again using these notes?")
  If DialogWindow.Clicked = True Then
    noteFileOutput.WriteLine(newChord)
    noteFileOutput.Close
    MessageBox(ChordNameField.Text + " has been added to the library.")
  End
End

The line

DialogWindow.ShowModal(Window3)

needs to be after where you set the values of the two controls. From the Window.ShowModal documentation, “Displays the window and causes the current method to stop executing until the window closes or becomes invisible.” [emphasis mine]

Once you call ShowModal, the next line will not execute until the dialog closes, so you’re setting the values after OK or Cancel is clicked.

2 Likes

Thank you, Ed. You’re a genius. It works like a charm now. And I looked at that exact page in the documentation too. But it’s as they say, the devil is in the details (no props to the devil). Thank you again, sir.

No genius here at all, just many years of experience. I got into Xojo picking up maintenance on code that had tons of modal windows and ShowModal calls. So many, in fact, that it took me a while to get the hang of writing Xojo code with Window.Show and not getting surprised that the rest of the code in the calling method ran before the new window closed.

I think you’re being modest, Ed. But at minimum, you’ve established another well known saying which is that, “Experience is your best teacher.” :wink:

1 Like