Windows Develop Bookmark and Share   
 index > Windows Forms General > control.getnextcontrol not give me reference to a control from a control in a usercontrol
 

control.getnextcontrol not give me reference to a control from a control in a usercontrol

Duh, I write too much and when I try to send my session goes off

It's my first post (tecnically), I speak in spanish sorry for mistakes

I have a problem, I created a user control with textbox1, button1, textbox2

I put in on a form and next to it put a texbox (call it textbox3)


when I press enter on textbox1, button1 or texbox2 I try to send focus to textbox3, I not get the reference to it

When I use textbox1.parentform.getnextcontrol(activecontrol,true).focus(and another try with sender and textbox1), I can get reference to button1 from textbox1, textbox2 from button1, but when I view the textbox2.parentform.getnextcontrol(activecontrol,true) it contains nothing

How I can get reference with getnextcontrol to the control next to the usercontrol from a inner control of the usercontrol?

PowerMetal  Friday, November 02, 2007 9:19 PM

Hi MetalPower

Base on my understanding, I think that you could review your code to check that is there anything wrong. The following code is a my example, it works well.

Code Block

public partial class MainForm : Form

{

public MainForm()

{

InitializeComponent();

Button button = new Button();

button.Bounds = new Rectangle(0, 0, 100, 30);

this.Controls.Add(button);

TestUserControl userControl = new TestUserControl();

userControl.Bounds = new Rectangle(110, 0, 300, 300);

this.Controls.Add(userControl);

TextBox txtBox = new TextBox();

txtBox.Bounds = new Rectangle(420, 0, 100, 30);

this.Controls.Add(txtBox);

}

}

class TestUserControl:UserControl

{

TextBox textBox1;

Button button1;

TextBox textBox2;

public TestUserControl()

{

this.textBox1 = new TextBox();

this.textBox1.Bounds = new Rectangle(3, 3, 100, 20);

this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);

this.button1 = new Button();

this.button1.Bounds = new Rectangle(3, 29, 75, 23);

this.textBox2 = new TextBox();

this.textBox2.Bounds = new Rectangle(3, 58, 100, 20);

this.textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown);

this.Controls.Add(this.textBox1);

this.Controls.Add(this.button1);

this.Controls.Add(this.textBox2);

}

void textBox1_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Enter)

this.textBox1.FindForm().GetNextControl(this.ActiveControl, true).Focus();

}

void textBox2_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Enter)

this.textBox2.FindForm().GetNextControl(this.ActiveControl, true).Focus();

}

}

If you still have any problem, you could pastea sample code which can reproduce your problem.

Best Regards,

Wei Zhou

Wei Zhou - MSFT  Wednesday, November 07, 2007 8:22 AM
why do you need to get the next control like that? Maybe I misunderstand, but whenever you press enter on textbox1/textbox2/button1 you want to focus on textbox3? Just set the focus like this. Make sure you set this method to the keydown method of each textbox1/textbox2/button1
Code Block


private void ctl_KeyDown(object sender, KeyEventArgs e)

{

if(e.KeyCode == Keys.Enter) textbox3.Focus();

}

AustinW04  Friday, November 02, 2007 10:01 PM

Hello, thanks for reply, I like to drop a custom user control and set his tabindex, and when it lost his focus, set the focus on the next control, witout known it

PowerMetal  Monday, November 05, 2007 1:23 PM

Hi MetalPower

Base on my understanding, I think that you could review your code to check that is there anything wrong. The following code is a my example, it works well.

Code Block

public partial class MainForm : Form

{

public MainForm()

{

InitializeComponent();

Button button = new Button();

button.Bounds = new Rectangle(0, 0, 100, 30);

this.Controls.Add(button);

TestUserControl userControl = new TestUserControl();

userControl.Bounds = new Rectangle(110, 0, 300, 300);

this.Controls.Add(userControl);

TextBox txtBox = new TextBox();

txtBox.Bounds = new Rectangle(420, 0, 100, 30);

this.Controls.Add(txtBox);

}

}

class TestUserControl:UserControl

{

TextBox textBox1;

Button button1;

TextBox textBox2;

public TestUserControl()

{

this.textBox1 = new TextBox();

this.textBox1.Bounds = new Rectangle(3, 3, 100, 20);

this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);

this.button1 = new Button();

this.button1.Bounds = new Rectangle(3, 29, 75, 23);

this.textBox2 = new TextBox();

this.textBox2.Bounds = new Rectangle(3, 58, 100, 20);

this.textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown);

this.Controls.Add(this.textBox1);

this.Controls.Add(this.button1);

this.Controls.Add(this.textBox2);

}

void textBox1_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Enter)

this.textBox1.FindForm().GetNextControl(this.ActiveControl, true).Focus();

}

void textBox2_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Enter)

this.textBox2.FindForm().GetNextControl(this.ActiveControl, true).Focus();

}

}

If you still have any problem, you could pastea sample code which can reproduce your problem.

Best Regards,

Wei Zhou

Wei Zhou - MSFT  Wednesday, November 07, 2007 8:22 AM

You can use google to search for other answers

Custom Search

More Threads

• DB Type casting
• How to distinguish compiler versions in source
• position of my controls change
• toolstrip.overflowbutton BUG?
• Visual Studio 2008 - Menus & Toolbars supporting Inheritance
• Serializing UserControl
• Open Link in New Window
• NotifyIcon balloonTips not appearing in XP
• User Wizard
• Bitmap.LockBits and PixelFormat