DataGridview column does not receive focus on form activation

A

Andrus

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.Add(new DataGridViewTextBoxColumn());
grid.EditMode = DataGridViewEditMode.EditOnEnter;
Controls.Add(grid);
}
}
 
B

Berryl Hesh

Andrus said:
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?
 
A

Andrus

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.
 
B

Berryl Hesh

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

private void MyForm_Click(object sender, EventArgs e) {
MyDataGridViewControl.Focus();
}

private void MyForm_Load(object sender, EventArgs e) {
MyDataGridViewControl.Focus();
}

Cheers,
BH
 
A

Andrus

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.Collections.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.Add(new DataGridViewTextBoxColumn());
grid.EditMode = DataGridViewEditMode.EditOnEnter;
Controls.Add(grid);
}


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

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

Andrus

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.Collections.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()
{
ToolStripContainer tc = new ToolStripContainer();

grid = new DataGridView();
grid.Columns.Add(new DataGridViewTextBoxColumn());
grid.EditMode = DataGridViewEditMode.EditOnEnter;
grid.Top = 120;
grid.Height = 300;
Controls.Add(tc);
tc.ContentPanel.Controls.Add(new MyUserControl());

tc.ContentPanel.Controls.Add(grid);
this.Activated += new EventHandler(Childform_Activated);
this.Deactivate += new EventHandler(Childform_Deactivate);
}

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

void Childform_Deactivate(object sender, EventArgs e)
{
this.LastFocus = this.ActiveControl;
}
}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top