How to assign tab order from left to right - winforms C#












1















I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



So here there is two rows since user has given the input as 2. All the components in the image are dynamically created. For each component i have created a class to set the property and its behaviour.



Now the issue is I need to traverse the component using tab.



When i tried to set tabindex = 1 for the first textbox and tabindex = 2 for the second textbox. I'm traversing the components vertically like mentioned below



Actual Output : enter image description here



The code in which i have added are following.



        public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
//assigned the tabindex as 2 for the second textbox
txtBox.TabIndex = 2;
}
}

public class addDynamicDateofServiceComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;

//assigned the tabindex as 1 for first textbox
txtBox.TabIndex = 1;
}
}


But what i need is , I need to traverse the components horizontally as mentioned below.



Expected Ouput: enter image description here



The Requried tab order is specified in the above image.










share|improve this question























  • Hard-coding the TabIndex does not look healthy. This invariably ends better if you create a UserControl. Or a new panel inside panel1. With a new container, the TabIndex starts back at 0.

    – Hans Passant
    Nov 20 '18 at 15:45











  • Hans makes a good comment about having a usercontrol or panel that represents the set of controls you're inserting. If you don't want to do that, then how about your add method takes a "rowindex" input, and you make the tab indexes rowindex*controlsperrow + 1 or 2 as per your current plan. Then all of row0 will have numbers smaller than all of row1 etc.

    – SazooCat
    Nov 20 '18 at 16:38











  • @SazooCat thanks for your idea. It solved my issue.

    – Ramji R
    Nov 20 '18 at 17:46
















1















I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



So here there is two rows since user has given the input as 2. All the components in the image are dynamically created. For each component i have created a class to set the property and its behaviour.



Now the issue is I need to traverse the component using tab.



When i tried to set tabindex = 1 for the first textbox and tabindex = 2 for the second textbox. I'm traversing the components vertically like mentioned below



Actual Output : enter image description here



The code in which i have added are following.



        public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
//assigned the tabindex as 2 for the second textbox
txtBox.TabIndex = 2;
}
}

public class addDynamicDateofServiceComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;

//assigned the tabindex as 1 for first textbox
txtBox.TabIndex = 1;
}
}


But what i need is , I need to traverse the components horizontally as mentioned below.



Expected Ouput: enter image description here



The Requried tab order is specified in the above image.










share|improve this question























  • Hard-coding the TabIndex does not look healthy. This invariably ends better if you create a UserControl. Or a new panel inside panel1. With a new container, the TabIndex starts back at 0.

    – Hans Passant
    Nov 20 '18 at 15:45











  • Hans makes a good comment about having a usercontrol or panel that represents the set of controls you're inserting. If you don't want to do that, then how about your add method takes a "rowindex" input, and you make the tab indexes rowindex*controlsperrow + 1 or 2 as per your current plan. Then all of row0 will have numbers smaller than all of row1 etc.

    – SazooCat
    Nov 20 '18 at 16:38











  • @SazooCat thanks for your idea. It solved my issue.

    – Ramji R
    Nov 20 '18 at 17:46














1












1








1








I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



So here there is two rows since user has given the input as 2. All the components in the image are dynamically created. For each component i have created a class to set the property and its behaviour.



Now the issue is I need to traverse the component using tab.



When i tried to set tabindex = 1 for the first textbox and tabindex = 2 for the second textbox. I'm traversing the components vertically like mentioned below



Actual Output : enter image description here



The code in which i have added are following.



        public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
//assigned the tabindex as 2 for the second textbox
txtBox.TabIndex = 2;
}
}

public class addDynamicDateofServiceComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;

//assigned the tabindex as 1 for first textbox
txtBox.TabIndex = 1;
}
}


But what i need is , I need to traverse the components horizontally as mentioned below.



Expected Ouput: enter image description here



The Requried tab order is specified in the above image.










share|improve this question














I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.



So here there is two rows since user has given the input as 2. All the components in the image are dynamically created. For each component i have created a class to set the property and its behaviour.



Now the issue is I need to traverse the component using tab.



When i tried to set tabindex = 1 for the first textbox and tabindex = 2 for the second textbox. I'm traversing the components vertically like mentioned below



Actual Output : enter image description here



The code in which i have added are following.



        public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
//assigned the tabindex as 2 for the second textbox
txtBox.TabIndex = 2;
}
}

public class addDynamicDateofServiceComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;

//assigned the tabindex as 1 for first textbox
txtBox.TabIndex = 1;
}
}


But what i need is , I need to traverse the components horizontally as mentioned below.



Expected Ouput: enter image description here



The Requried tab order is specified in the above image.







c# winforms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 15:36









Ramji RRamji R

248




248













  • Hard-coding the TabIndex does not look healthy. This invariably ends better if you create a UserControl. Or a new panel inside panel1. With a new container, the TabIndex starts back at 0.

    – Hans Passant
    Nov 20 '18 at 15:45











  • Hans makes a good comment about having a usercontrol or panel that represents the set of controls you're inserting. If you don't want to do that, then how about your add method takes a "rowindex" input, and you make the tab indexes rowindex*controlsperrow + 1 or 2 as per your current plan. Then all of row0 will have numbers smaller than all of row1 etc.

    – SazooCat
    Nov 20 '18 at 16:38











  • @SazooCat thanks for your idea. It solved my issue.

    – Ramji R
    Nov 20 '18 at 17:46



















  • Hard-coding the TabIndex does not look healthy. This invariably ends better if you create a UserControl. Or a new panel inside panel1. With a new container, the TabIndex starts back at 0.

    – Hans Passant
    Nov 20 '18 at 15:45











  • Hans makes a good comment about having a usercontrol or panel that represents the set of controls you're inserting. If you don't want to do that, then how about your add method takes a "rowindex" input, and you make the tab indexes rowindex*controlsperrow + 1 or 2 as per your current plan. Then all of row0 will have numbers smaller than all of row1 etc.

    – SazooCat
    Nov 20 '18 at 16:38











  • @SazooCat thanks for your idea. It solved my issue.

    – Ramji R
    Nov 20 '18 at 17:46

















Hard-coding the TabIndex does not look healthy. This invariably ends better if you create a UserControl. Or a new panel inside panel1. With a new container, the TabIndex starts back at 0.

– Hans Passant
Nov 20 '18 at 15:45





Hard-coding the TabIndex does not look healthy. This invariably ends better if you create a UserControl. Or a new panel inside panel1. With a new container, the TabIndex starts back at 0.

– Hans Passant
Nov 20 '18 at 15:45













Hans makes a good comment about having a usercontrol or panel that represents the set of controls you're inserting. If you don't want to do that, then how about your add method takes a "rowindex" input, and you make the tab indexes rowindex*controlsperrow + 1 or 2 as per your current plan. Then all of row0 will have numbers smaller than all of row1 etc.

– SazooCat
Nov 20 '18 at 16:38





Hans makes a good comment about having a usercontrol or panel that represents the set of controls you're inserting. If you don't want to do that, then how about your add method takes a "rowindex" input, and you make the tab indexes rowindex*controlsperrow + 1 or 2 as per your current plan. Then all of row0 will have numbers smaller than all of row1 etc.

– SazooCat
Nov 20 '18 at 16:38













@SazooCat thanks for your idea. It solved my issue.

– Ramji R
Nov 20 '18 at 17:46





@SazooCat thanks for your idea. It solved my issue.

– Ramji R
Nov 20 '18 at 17:46












1 Answer
1






active

oldest

votes


















0














Guessing from the name of your class you are adding rows dynamically to your form. But since you are hard coding the tab index the result per row looks like in your expected output. This means by tabbing you go from index 1 to index 1 to index 2 to index 2 and so on and so forth.



I'd advise you to have an incrementing tab index stored somewhere in your application which is incremented after it is assigned to a new dynamically created control.



As a really simple example I created a fresh forms project which just has two buttons. The first one adds a new textbox and the second button switches into a new row. And in this example everything has the tab index you require. The code behind looks like this:



public partial class Form1 : Form
{
private int currentX = 0;
private int currentY = 0;
private const int tbWidth = 75;
private const int tbHeight = 23;
private int currentTabIndex = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
var textBoxOne = new TextBox();
this.Controls.Add(textBoxOne);
textBoxOne.Location = new Point(this.currentX, this.currentY);
textBoxOne.Size = new Size(tbWidth, tbHeight);
textBoxOne.TabIndex = currentTabIndex;
textBoxOne.Text = $"{currentTabIndex}";

currentTabIndex++;

this.currentX += tbWidth + 5;
}

private void button2_Click(object sender, EventArgs e)
{
currentY += tbHeight + 5;
currentX = 0;
}
}


Please bare in mind this is just a simple example. I could help you better in the context of your application if I knew more about it.






share|improve this answer





















  • 1





    @zelenka Thanks for the effort, your answer and user Sazoocat comment gave the spark to solve the issue.

    – Ramji R
    Nov 20 '18 at 17:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396448%2fhow-to-assign-tab-order-from-left-to-right-winforms-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Guessing from the name of your class you are adding rows dynamically to your form. But since you are hard coding the tab index the result per row looks like in your expected output. This means by tabbing you go from index 1 to index 1 to index 2 to index 2 and so on and so forth.



I'd advise you to have an incrementing tab index stored somewhere in your application which is incremented after it is assigned to a new dynamically created control.



As a really simple example I created a fresh forms project which just has two buttons. The first one adds a new textbox and the second button switches into a new row. And in this example everything has the tab index you require. The code behind looks like this:



public partial class Form1 : Form
{
private int currentX = 0;
private int currentY = 0;
private const int tbWidth = 75;
private const int tbHeight = 23;
private int currentTabIndex = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
var textBoxOne = new TextBox();
this.Controls.Add(textBoxOne);
textBoxOne.Location = new Point(this.currentX, this.currentY);
textBoxOne.Size = new Size(tbWidth, tbHeight);
textBoxOne.TabIndex = currentTabIndex;
textBoxOne.Text = $"{currentTabIndex}";

currentTabIndex++;

this.currentX += tbWidth + 5;
}

private void button2_Click(object sender, EventArgs e)
{
currentY += tbHeight + 5;
currentX = 0;
}
}


Please bare in mind this is just a simple example. I could help you better in the context of your application if I knew more about it.






share|improve this answer





















  • 1





    @zelenka Thanks for the effort, your answer and user Sazoocat comment gave the spark to solve the issue.

    – Ramji R
    Nov 20 '18 at 17:48
















0














Guessing from the name of your class you are adding rows dynamically to your form. But since you are hard coding the tab index the result per row looks like in your expected output. This means by tabbing you go from index 1 to index 1 to index 2 to index 2 and so on and so forth.



I'd advise you to have an incrementing tab index stored somewhere in your application which is incremented after it is assigned to a new dynamically created control.



As a really simple example I created a fresh forms project which just has two buttons. The first one adds a new textbox and the second button switches into a new row. And in this example everything has the tab index you require. The code behind looks like this:



public partial class Form1 : Form
{
private int currentX = 0;
private int currentY = 0;
private const int tbWidth = 75;
private const int tbHeight = 23;
private int currentTabIndex = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
var textBoxOne = new TextBox();
this.Controls.Add(textBoxOne);
textBoxOne.Location = new Point(this.currentX, this.currentY);
textBoxOne.Size = new Size(tbWidth, tbHeight);
textBoxOne.TabIndex = currentTabIndex;
textBoxOne.Text = $"{currentTabIndex}";

currentTabIndex++;

this.currentX += tbWidth + 5;
}

private void button2_Click(object sender, EventArgs e)
{
currentY += tbHeight + 5;
currentX = 0;
}
}


Please bare in mind this is just a simple example. I could help you better in the context of your application if I knew more about it.






share|improve this answer





















  • 1





    @zelenka Thanks for the effort, your answer and user Sazoocat comment gave the spark to solve the issue.

    – Ramji R
    Nov 20 '18 at 17:48














0












0








0







Guessing from the name of your class you are adding rows dynamically to your form. But since you are hard coding the tab index the result per row looks like in your expected output. This means by tabbing you go from index 1 to index 1 to index 2 to index 2 and so on and so forth.



I'd advise you to have an incrementing tab index stored somewhere in your application which is incremented after it is assigned to a new dynamically created control.



As a really simple example I created a fresh forms project which just has two buttons. The first one adds a new textbox and the second button switches into a new row. And in this example everything has the tab index you require. The code behind looks like this:



public partial class Form1 : Form
{
private int currentX = 0;
private int currentY = 0;
private const int tbWidth = 75;
private const int tbHeight = 23;
private int currentTabIndex = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
var textBoxOne = new TextBox();
this.Controls.Add(textBoxOne);
textBoxOne.Location = new Point(this.currentX, this.currentY);
textBoxOne.Size = new Size(tbWidth, tbHeight);
textBoxOne.TabIndex = currentTabIndex;
textBoxOne.Text = $"{currentTabIndex}";

currentTabIndex++;

this.currentX += tbWidth + 5;
}

private void button2_Click(object sender, EventArgs e)
{
currentY += tbHeight + 5;
currentX = 0;
}
}


Please bare in mind this is just a simple example. I could help you better in the context of your application if I knew more about it.






share|improve this answer















Guessing from the name of your class you are adding rows dynamically to your form. But since you are hard coding the tab index the result per row looks like in your expected output. This means by tabbing you go from index 1 to index 1 to index 2 to index 2 and so on and so forth.



I'd advise you to have an incrementing tab index stored somewhere in your application which is incremented after it is assigned to a new dynamically created control.



As a really simple example I created a fresh forms project which just has two buttons. The first one adds a new textbox and the second button switches into a new row. And in this example everything has the tab index you require. The code behind looks like this:



public partial class Form1 : Form
{
private int currentX = 0;
private int currentY = 0;
private const int tbWidth = 75;
private const int tbHeight = 23;
private int currentTabIndex = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
var textBoxOne = new TextBox();
this.Controls.Add(textBoxOne);
textBoxOne.Location = new Point(this.currentX, this.currentY);
textBoxOne.Size = new Size(tbWidth, tbHeight);
textBoxOne.TabIndex = currentTabIndex;
textBoxOne.Text = $"{currentTabIndex}";

currentTabIndex++;

this.currentX += tbWidth + 5;
}

private void button2_Click(object sender, EventArgs e)
{
currentY += tbHeight + 5;
currentX = 0;
}
}


Please bare in mind this is just a simple example. I could help you better in the context of your application if I knew more about it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 16:08

























answered Nov 20 '18 at 15:57









Manuel ZelenkaManuel Zelenka

1,1282924




1,1282924








  • 1





    @zelenka Thanks for the effort, your answer and user Sazoocat comment gave the spark to solve the issue.

    – Ramji R
    Nov 20 '18 at 17:48














  • 1





    @zelenka Thanks for the effort, your answer and user Sazoocat comment gave the spark to solve the issue.

    – Ramji R
    Nov 20 '18 at 17:48








1




1





@zelenka Thanks for the effort, your answer and user Sazoocat comment gave the spark to solve the issue.

– Ramji R
Nov 20 '18 at 17:48





@zelenka Thanks for the effort, your answer and user Sazoocat comment gave the spark to solve the issue.

– Ramji R
Nov 20 '18 at 17:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396448%2fhow-to-assign-tab-order-from-left-to-right-winforms-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?