How to obtain the focus?

Greetings! I have a treeView on the MainForm. OnPaint of treeView is overriden so it draws a small visible rectangle in itself. Double-clicking on rectangle fires new dialog appearing. When I close the dialog, focus is not on the MainForm (to close the MainForm I should click the cross at the right top angle twice), it's somewhere :) How to get it back this.Focus() after catching the result of the dialog is not helpful. Thanks truetype

Answer this question

How to obtain the focus?

  • muga

    Okay, I had a bit of a play around and I was experiencing the same thing as you were with your code above. 

    It seems to be that you are interrupting the mouse processing (by showing a dialog on MouseDown) and causing focus issues.

    I found that you could achieve the same thing (without the focus issue), by overridding OnDoubleClick or OnMouseDoubleClick methods instead of the OnMouseDown method. However, these events are only raised when a TreeNode is double-clicked.

  • Kevin MacDonald

    David,

    I've solved my issue in a bit interesting way :) I changed inheritance from TreeView to inheritance from PictureBox. That's it. Everything is fine. But very strange solution to me.

    By the way, if it's not so hard, please provide me with overriding of OnDoubleClick for possible future using.

    Thanks a lot
    truetype

  • vikasamin

    What has the focus if the MainForm doesn't

    Can you post some small code that reproduces it here

  • BillTodd


    namespace MainFormNamespace
    {
    public delegate void OpenNewDialogEvent();

    public class MainForm : System.Windows.Forms.Form
    {
    private System.ComponentModel.Container components = null;
    private treeViewEnhanced treeView;

    public MainForm()
    {
    InitializeComponent();
    }

    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    private void InitializeComponent()
    {
    this.treeView = new treeViewEnhanced();

    this.SuspendLayout();

    this.treeView.OpenNewDialog += new OpenNewEventDialog(OpenNewDialog);

    this.ResumeLayout(false);
    }

    private void OpenNewDialog()
    {
    Form NewDialog = new Form();

    NewDialog.ShowDialog();

    this.Focus();
    }

    [STAThread]
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.Run(new MainForm());
    }
    }

    public class treeViewEnhanced : System.Windows.Forms.TreeView
    {
    private System.ComponentModel.Container components = null;
    public event OpenNewDialogEvent OpenNewDialog;

    public treeViewEnhanced()
    {
    InitializeComponent();
    }

    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    private void InitializeComponent()
    {
    components = new System.ComponentModel.Container();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    e.Graphics.FillRectangle(new SolidBrush(System.Drawing.Color.Olive), new Rectangle(0, 0, 100, 100));
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
    if (e.Clicks == 2)
    {
    if (e.X >= 0 && e.X <= 100 && e.Y >= 0 && e.Y <= 100)
    OpenNewDialog();
    }

    this.Invalidate();
    }
    }

    }

    As I've already stated, if once-clicking is used, then everything is normal.

    I didn't put in the code various event handlers (as leave, enter, mouseleave, etc.), as I tried everything and nothing worked properly.

    truetype

  • vvill

    Does this repro without owner drawing the TreeView   Also, could you share the code you're using to launch the dialog

    thanks
     - mike

  • musichenryviolin

    Thanks, David

    But some problems are still here. I can't find OnMouseDoubleClick method (even in MSDN, by the way, I use .NET 1.x), but OnDoubleClick I do have.

    Can't figure out how to use OnDoubleClick or how to override it. Can you help With a little of code.

    Also, I noticed that OnDoubleClick has EventArgs as a parameter, but hasn't MouseEventArgs. What if I would desire to determine the coordinates of mouse double-click

    truetype

  • Duckfoot

    Yep, it's the same without owner drawing! But as I stated double-clicking the rectangle in treeview causes dialog to open... Now I've changed double-click to once-click and things worked out. After closing the dialog focus is on the MainForm.

    May be this will help helping me... :)

    The code of launching the dialog:

    myFormClass newDialog = new myFormClass();

    DialogResult result = newDialog.ShowDialog();

    if (result == DialogResult.OK)
    /*something here*/

    this.Focus();

  • Madhu Ponduru ----MSFT

     David M. Kean wrote:
    What has the focus if the MainForm doesn't <br /><br />Can you post some small code that reproduces it here


    That is the problem. I can't figure out what has the focus! Can I get by any method, which returns the control that has the focus

    I'll post a code in a while...

  • How to obtain the focus?