[.net] Doubleclick opening another form - problem

Started by
8 comments, last by mijapkos 18 years, 4 months ago
I have a form (form1) which contains clickable data. When user doubleclicks some data, program opens an another form (form2) which shows details of this data. The problem is that after doubleclick, focus is shifted back to form1. Form2 is shown correctly, it loads the data correctly but I can't get the focus stay on the form2. User has to alt-tab to form2. Anyway around this? I'm using .NET Framework 2.0 and VB.NET.
Advertisement
Depnding on how your code is do you create the object of the new form to display it?
If so you should be able to just do myFormObject.Focus();
- GDKnight
Alternatively, you could change the style of the 2nd form to FixedSingle, assign Ok to the DialogResult field of 1 button on the 2nd form(For instance, the Close button) and treat the form like a Dialogbox:

using (Form2 frm = new Form2(detailedData)){    DialogResult res = frm.ShowDialog();}


This will make the 2nd form top-most in your application, something you MIGHT want to accomplish. Also, you can set the startup position to be center-parent.

Toolmaker

Quote:Original post by Toolmaker
This will make the 2nd form top-most in your application, something you MIGHT want to accomplish. Also, you can set the startup position to be center-parent.

Toolmaker


Actually that's not what I want to accomplish :) The second form is supposed to left open.

But thank you for response :)
Quote:Original post by GDKnight
Depnding on how your code is do you create the object of the new form to display it?
If so you should be able to just do myFormObject.Focus();


Setting the focus to form2 isn't working, for some reason. I'm using VB.NET 2005's new My-namespace to "create" the form. Code looks like this:

My.Forms.Form2.Show()
My.Forms.Form2.Focus()

And still the focus is lost from the Form2. It's like if I press mouse button three times and the last click is left in some buffer and executed after setting the focus to Form2.

There's an extra button in the Form1 which also shows the Form2. With the difference that when pressing it, focus remains on Form2.

Could this be because Form1 contains particular component (FarPoint's Spread) which is doubleclicked and it registers some other event after showing Form2? It shouldn't, but..
Another solution I can maybe think of is in the form load of the secondary form also put a focus command that way its focused there too?

I am also curious are you using DoubleClick event? I mean are you catching all clicks or only double clicks. Perhaps that is causing a slight error?

Some URL's That Might Help:
Control.DoubleClick Event (System.Windows.Forms)
Working with Multiple Forms in Visual Basic .NET: Upgrading to .NET
Multiple Forms in VB.NET. Part 1

The VB.NET Part1 Multiple Forms mentions a F2.BringToFront() method of some sort? I've included the code below:
Private WithEvents F2 As Form2Private Sub ShowForm2Button_Click(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles ShowForm2Button.Click'  If the instance still exists... (ie. it's Not Nothing)    If Not IsNothing(F2) Then        '  and if it hasn't been disposed yet        If Not F2.IsDisposed Then            '  then it must already be instantiated - maybe it's            '  minimized or hidden behind other forms ?            F2.WindowState = FormWindowState.Normal  ' Optional            F2.BringToFront()  '  Optional        Else            '  else it has already been disposed, so you can            '  instantiate a new form and show it            F2 = New Form2()            F2.Show()        End If    Else        '  else the form = nothing, so you can safely        '  instantiate a new form and show it        F2 = New Form2()        F2.Show()    End IfEnd Sub


If that doesn't work some workarounds I can think of are:
You could perhaps put a sleep function or a pause function of some sort. Then execute the focus function but I don't believe that is ideal how ever it is a workaround.

Perhaps disable the first form for one second and then re-enable it after setting the focus the secondary form that might work as a work around to.

Possibly even make a timer event.

I am not very familiar with the Visual Basic .NET enviorment but im used to C#. These are some work of the work arounds I can think of though.

[Edited by - GDKnight on November 30, 2005 8:24:45 AM]
- GDKnight
Thank you for a detailed reply.

BringToFront-method sounds like an ideal solution. Though I'm away from my development environment for couple days so I can't yet try it out. But I'll report back when I have a change :)

Thanks again.
I tried BringToFront() -method but it didn't do the trick.

Quote:Original post by GDKnight
I am also curious are you using DoubleClick event? I mean are you catching all clicks or only double clicks. Perhaps that is causing a slight error?


I'm catching only the DoubleClick event.

But I'm going to try out the other workarounds. I'll report back little later.
You could set the other form's TopMost property to true. This would keep it above the other form no matter what.
I eventually got around the problem. It was related to the 3rd party component which was catching the double click and wanted to do something with it even after the method ended. I was able to go around it by setting eventargs Cancel-property to true after opening the new form.

Thanks for all the replies.

This topic is closed to new replies.

Advertisement