468,321 Members | 1,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

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

Oct 31 '08 #1
5 3803
"Andrus" <ko********@hot.eewrote in message
news:ex**************@TK2MSFTNGP02.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?
Oct 31 '08 #2
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.

Nov 1 '08 #3
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

"Andrus" <ko********@hot.eewrote in message
news:uo**************@TK2MSFTNGP05.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.

Nov 1 '08 #4
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();
}
}

Nov 1 '08 #5
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());
}
}
Nov 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Aaron Smith | last post: by
1 post views Thread by mark carew | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
3 posts views Thread by Tony K | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.