DataGridview column does not receive focus on form activation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrus

    DataGridview column does not receive focus on form activation

    Steps to reproduce issue:

    1. Run code.
    2. Enter some data to grid
    3. Click other form caption
    4. Click original form caption
    5. Enter some characters

    Observed: entered characters are ignored

    Expected: entered characters must appear in textbox which was last active

    How to fix ?

    Andrus.

    using System.Windows. Forms;

    public class Test
    {
    static void Main()
    {
    Application.Run (new MainForm());
    }
    }

    class MainForm : Form
    {
    public MainForm()
    {
    WindowState = FormWindowState .Maximized;
    IsMdiContainer = true;
    Form frm = new Childform();
    frm.MdiParent = this;
    frm.Show();
    Form frm2 = new Childform();
    frm2.MdiParent = this;
    frm2.Show();
    frm2.Left = 2000;
    }
    }

    class Childform : Form
    {
    public Childform()
    {
    var grid = new DataGridView();
    grid.Columns.Ad d(new DataGridViewTex tBoxColumn());
    grid.EditMode = DataGridViewEdi tMode.EditOnEnt er;
    Controls.Add(gr id);
    }
    }

  • Berryl Hesh

    #2
    Re: DataGridview column does not receive focus on form activation

    "Andrus" <kobruleht2@hot .eewrote in message
    news:exgcCm5OJH A.4760@TK2MSFTN GP02.phx.gbl...
    Steps to reproduce issue:
    >
    1. Run code.
    2. Enter some data to grid
    3. Click other form caption
    4. Click original form caption
    Are you clicking on some part of the form that isn't the dgv? What happens
    if you click directly on the control if so?


    Comment

    • Andrus

      #3
      Re: DataGridview column does not receive focus on form activation

      Are you clicking on some part of the form that isn't the dgv? What happens
      if you click directly on the control if so?
      Clicking in control activates this control.
      I need activation when form is simply activated, when modeless lookup form
      is called during data enty.

      Andrus.

      Comment

      • Berryl Hesh

        #4
        Re: DataGridview column does not receive focus on form activation

        Try to set the focus to your dgv wherever you need to, something like this:

        private void MyForm_Click(ob ject sender, EventArgs e) {
        MyDataGridViewC ontrol.Focus();
        }

        private void MyForm_Load(obj ect sender, EventArgs e) {
        MyDataGridViewC ontrol.Focus();
        }

        Cheers,
        BH

        "Andrus" <kobruleht2@hot .eewrote in message
        news:uo$IvuAPJH A.1144@TK2MSFTN GP05.phx.gbl...
        >Are you clicking on some part of the form that isn't the dgv? What
        >happens if you click directly on the control if so?
        >
        Clicking in control activates this control.
        I need activation when form is simply activated, when modeless lookup form
        is called during data enty.
        >
        Andrus.

        Comment

        • Andrus

          #5
          Re: DataGridview column does not receive focus on form activation

          Berryl,
          Try to set the focus to your dgv wherever you need to, something like
          this:
          Thank you. I tried code below but problem persists.
          Probably we need to activate specific textbox in some special way, maybe
          remember and set cell coordinates directly?

          Andrus.

          using System.Windows. Forms;
          using System.Collecti ons.Generic;

          public class Test
          {
          static void Main()
          {
          Application.Run (new MainForm());
          }
          }

          class MainForm : Form
          {
          public MainForm()
          {
          WindowState = FormWindowState .Maximized;
          IsMdiContainer = true;
          Form frm = new Childform();
          frm.MdiParent = this;
          frm.Show();
          Form frm2 = new Childform();
          frm2.MdiParent = this;
          frm2.Show();
          frm2.Left = 2000;
          }
          }

          class Childform : Form
          {
          DataGridView grid;

          public Childform()
          {
          grid = new DataGridView();
          grid.Columns.Ad d(new DataGridViewTex tBoxColumn());
          grid.EditMode = DataGridViewEdi tMode.EditOnEnt er;
          Controls.Add(gr id);
          }


          protected override void OnClick(System. EventArgs e)
          {
          base.OnClick(e) ;
          grid.Focus();
          }

          protected override void OnLoad(System.E ventArgs e)
          {
          base.OnLoad(e);
          grid.Focus();
          }
          }

          Comment

          • Andrus

            #6
            Re: DataGridview column does not receive focus on form activation

            Steps to reproduce issue:

            Here is more advanced sample.
            Last TextBox in grid is *not* activated when form is activated.

            Andrus.

            using System.Windows. Forms;
            using System.Collecti ons.Generic;
            using System;

            public class Test
            {
            static void Main()
            {
            Application.Run (new MainForm());
            }
            }

            class MainForm : Form
            {
            public MainForm()
            {
            WindowState = FormWindowState .Maximized;
            IsMdiContainer = true;
            Form frm = new Childform();
            frm.MdiParent = this;
            frm.Show();
            Form frm2 = new Childform();
            frm2.MdiParent = this;
            frm2.Show();
            frm2.Left = 2000;
            }
            }

            class Childform : Form
            {
            DataGridView grid;
            Control LastFocus = null;

            public Childform()
            {
            ToolStripContai ner tc = new ToolStripContai ner();

            grid = new DataGridView();
            grid.Columns.Ad d(new DataGridViewTex tBoxColumn());
            grid.EditMode = DataGridViewEdi tMode.EditOnEnt er;
            grid.Top = 120;
            grid.Height = 300;
            Controls.Add(tc );
            tc.ContentPanel .Controls.Add(n ew MyUserControl() );

            tc.ContentPanel .Controls.Add(g rid);
            this.Activated += new EventHandler(Ch ildform_Activat ed);
            this.Deactivate += new EventHandler(Ch ildform_Deactiv ate);
            }

            void Childform_Activ ated(object sender, EventArgs e)
            {
            if (this.LastFocus != null)
            this.LastFocus. Focus();
            }

            void Childform_Deact ivate(object sender, EventArgs e)
            {
            this.LastFocus = this.ActiveCont rol;
            }
            }

            class MyUserControl : UserControl
            {
            internal MyUserControl()
            {
            Height = 100;
            Controls.Add(ne w TextBox());
            }
            }

            Comment

            Working...