Windows Develop Bookmark and Share   
 index > Windows Forms General > Caret in text box focus
 

Caret in text box focus

Hi,

Apologies if this is an unbelievably obvious question:

I've scoured the forums for the answer to this, but haven't found anything so far that suits my needs.

I have a windows form with a dialog box, in the dialog box I have several text boxes. When the dialog box appears, I want the first text box (top left corner) to appear with the caret already blinking in it. How do I achieve this? I've set the tab index of that control to 0 but still no joy. I understand I have to set he focus of this control? How do I go about doing that?

Many thanks,

RB.

RighteousBrother  Monday, November 05, 2007 4:48 PM

See Setting Tab Order is Enough to Set the Caret in your text box there is no need to use Set Focus etc.Just Set the Tab Order of your Dialog Control once again and Your First Tab index should be your that text box in which you want to show the caret.means the tab Index of your text Box will be 1 .note it will not start from 0 i think 0 is used for Dialog and remaining controls start from 1 just try this .hope it will help you.

Thanx
Pintu Shukla  Thursday, November 08, 2007 5:02 PM

Hi

Do you have any control's TabIndex same as txtCode's? Generally, we should set all focusable control's TabIndex differently. TabStop property may also affects the tab and focus. If you still have any problems, you could post the InitializeComponent method.

Best Regards,

Wei Zhou

Wei Zhou - MSFT  Friday, November 09, 2007 2:27 AM

try textBox.Focus(); to give it input focus


H. _冬_ Tony  Monday, November 05, 2007 5:20 PM

Like Tony said, use textBox.Focus(); but use it in the Form_Shown event

Fábio Franco  Monday, November 05, 2007 5:27 PM

Hi,

I tried it in a quick test form I knocked up quickly and it worked perfectly, but on the actual form on the project I'm working on, it doesn't work at all - I'll have to investigate further.

RighteousBrother  Tuesday, November 06, 2007 9:38 AM

Hi

You could try to set focus in OnShown method. The following code shows how to do this.

Code Block

protected override void OnShown(EventArgs e)

{

base.OnShown(e);

this.textBox3.Focus();

}

Best Regards,
Wei Zhou
Wei Zhou - MSFT  Thursday, November 08, 2007 7:26 AM

Hi,

I've tried that but it doesn't work unfortunately, I've included some of the code in truncated form, if anyone has any suggestions txt.Code is the text box I want to have focus:

Code Block

{

public partial class FormPlanHeader : Form

{

private bool m_blnAdd = true;

private int m_intHeaderID = -1;

private PlanHeader m_planHeader;

//adding a new plan header

public FormPlanHeader()

{

InitializeComponent();

m_blnAdd= true;

this.Text = "Add/Edit a new plan";

}

// editing an existing plan header

public FormPlanHeader(int planID)

{

InitializeComponent();

m_blnAdd = false;

m_intHeaderID = planID;

this.Text = "Add/Edit Production plan";

}

private void FormPlan_Load(object sender, EventArgs e)

{

try

{

if (m_blnAdd)

{

//adding a new header so just create a blank one

m_planHeader = new PlanHeader();

}

else

{

// editing so show the existing plan header data

m_planHeader = new PlanHeader(m_intHeaderID);

txtCode.Text = m_planHeader.Code;

txtDescription.Text = m_planHeader.Description;

txtNotes.Text = m_planHeader.Notes;

//show the centre code and description

ProductionCentre myCentre = ProductionCentres.GetProductionCentreDetail(m_planHeader.ProductionCentreID);

txtCentreCode.Text = myCentre.Code;

txtCentreDescription.Text = myCentre.Description;

}

Many thanks,

RB.

RighteousBrother  Thursday, November 08, 2007 4:38 PM

See Setting Tab Order is Enough to Set the Caret in your text box there is no need to use Set Focus etc.Just Set the Tab Order of your Dialog Control once again and Your First Tab index should be your that text box in which you want to show the caret.means the tab Index of your text Box will be 1 .note it will not start from 0 i think 0 is used for Dialog and remaining controls start from 1 just try this .hope it will help you.

Thanx
Pintu Shukla  Thursday, November 08, 2007 5:02 PM

Hi

Do you have any control's TabIndex same as txtCode's? Generally, we should set all focusable control's TabIndex differently. TabStop property may also affects the tab and focus. If you still have any problems, you could post the InitializeComponent method.

Best Regards,

Wei Zhou

Wei Zhou - MSFT  Friday, November 09, 2007 2:27 AM

Hi,

I got it to work finally, all the controls were set within a panel so I set the tab index to 0, and the next tab index to 1 and so on.

Thanks for all the help!

RB.

RighteousBrother  Thursday, November 15, 2007 10:51 AM

You can use google to search for other answers

Custom Search

More Threads

• How to use backgroundworkercompoentn with progressbar?
• How do I add customized cursors to my application with some code?
• help needed on how to create a spinning wheel using MS VS.NET2003 in vb
• Shell help
• Need MDI Tab Control
• How to scroll to bottom of tableLayoutPanel, in code?
• DateTimePicker Events don't fire
• Need help with display data!
• Open file dialog opening multiple files and customization
• Reg Image editor control