RighteousBrother

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.



Re: Windows Forms General Caret in text box focus

H. () Tony


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







Re: Windows Forms General Caret in text box focus

Fábio

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






Re: Windows Forms General Caret in text box focus

RighteousBrother

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.





Re: Windows Forms General Caret in text box focus

Wei Zhou - MSFT

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





Re: Windows Forms General Caret in text box focus

RighteousBrother

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.





Re: Windows Forms General Caret in text box focus

Pintu Shukla


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





Re: Windows Forms General Caret in text box focus

Wei Zhou - MSFT

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






Re: Windows Forms General Caret in text box focus

RighteousBrother

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.