I have a program in which I open a child window from the parent with the window.show method. It opens on top but then immediately loses focus to the parent.
The screen is small so it has to share the same space with the parent.
I have tried setting the focus to the child window in various events. I have removed all of the reference to the parent window in the child, but the only way it stays on top is if I make it a modal window. I don’t want it to be modal.
What could be happening?
No idea. You’re asking us to guess. You wouldn’t ask your mechanic to guess why your car doesn’t start.
Show us a project where this is reproducible.
Can you show the code that opens the window? (paste the code and then use </> to format it).
How are you referring to the window?
There’s a chance that you are doing something like the following for a window called Window2.
Window2.show
If you are you are using explicit instances, which can lead to all sorts of issues. Hopefully it says something like:
Var oNewWindow as New Window2
oNewWindow.Show
Here we are explicitly creating an instance of the window explicitly and then opening it. Don’t refer to windows by their class name. Instead use a reference to the instance of the window you are targeting.
Thanks, I understand about the vagueness of my question. I was hoping it triggered something in someone who had this happen recently. Anyhow I am happy to share the code but it is specialized and requires an Automation Direct P100 PLC, otherwise if there is no connection then nothing happens. I would have to rewrite a lot of code to bypass.
Here is the code I use to open the window. It is in MenuSelected event of a DesktopBevelButton.
if selectedItem.Text = "View Log File" and mDeviceConnect = True then
wndLogging.Show
elseif selectedItem.Text = "Settings" then
tmrConnTimeout.RunMode = Timer.RunModes.Off
tmrGetData.RunMode = Timer.RunModes.Off
wndEditMain.ShowModal
elseif selectedItem.Text = "TestMode ON" and mDeviceConnect = True then
mTestMode = True
elseif selectedItem.Text = "TestMode OFF" and mDeviceConnect = True then
mTestMode = False
end if
What is wndLogging. I’m assuming it is the name of a Window you’ve created. If so then this is an example of Implicit instance and could be the cause problems. Referring to windows by their class name can lead to odd behaviour you wouldn’t, at first, expect, for example:
wndLogging.Close
if wndLogging.Closed then
// Some code here
end if
The first line closes the window. The second if line causes the window to be created, opened and shown. That’s because you’ve referred to wndLogging and so it creates an instance of it, if one does not exist.
Thank you for that. I found out about that a while ago. As you can see, I am only showing the window. The parent window is very busy do things, Maybe that is why it takes back the focus. I will keep trying. It is not the end of the world. I am the only one who uses this app. I do hate having to touch the screen to make the window get focus. (lazy)
Here is the whole program if anyone is interested in looking. I am still not complete though. It requires the Einhugur GuageControl plugin.
xojo.zip (1003.2 KB)
Here is the table and other sound files that I put on the desktop, only because that is the folder I have had the best luck with.
Desktiop.zip (235.6 KB)
It could be that the main window is also an implicit instance and causing the problem within it. If you search for SetFocus and comment them out (as a test), does the problem go away? Alternatively the main window having a .Show call?
I do not have any use of SetFocus. I tried in the child window in a couple of events to see if I could get that to work, but it did not.
main window.show?
Like I said. Without a project, we can only guess.
He provided the project above.
wndLogging.Opening is interacting with wndGuagePanel and wndGuagePanel is implicit instance. This is exactly the problem implicit instance causes.
Thank you. Well that shows my lack of knowledge with XOJO. I am not sure of what the solution would be.
Turn implicit instance off (on every window) and and pass the values in some other way. Implicit instance is on the advanced tab for very very bad reasons that make me wildly frustrated.
From where you open wndLogging you can set the title.
var oNew as new wndLogging
oNew.Title = self.lblTitle.Text + " at - " + self.tCtrlIP
You could create a new method to accept 9 values or set them as properties of wndLogging to be read during the Opening event. All this would be done from wndGuagePanel where you are opening the window, like the example above.
OK thanks again. I remember now why I used an Implicit Instance. I solved a bunch of other problems I had. I turned it off and now I have a bunch of errors. If that is the reason I will go through and fix the errors.
Implicit instance is 100% the reason the parent window is coming to the front. Designing without implicit instance can be a challenge, but implicit instance is unfortunately the source of your problem.
Thanks for taking the time to help me resolve this issue.
I’m happy to help debug, but I refuse to guess. I’m sorry if that came across harsh.
Store a reference to the wndLogging and wndGaugePanel windows somewhere you can get to them. For example properties in the app class:
wndGaugePanelRef as wndGaugePanel
wndLoggingRef as wndLogging
// When opening a Window
wndLoggingRef = New wndLogging
wndLoggingRef.Show
// Closing:
wndLoggingRef.Close
wndLoggingRef = Nil
Change your references to App.wndLoggingRef or App.wndGaugePanelRef, with implicit instance off you should be better off.