By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,837 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

DataGridview column does not receive focus on form activation

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.