Question Indexing controls on tabcontrol pages question.

rog_rickert

Member
Joined
Dec 17, 2020
Messages
11
Programming Experience
Beginner
Hello, looking for advice on an issue. What I have is working, I'm just wondering if there is an easier way to implement it. On a form in my project I have a tabcontrol with 4 tag pages for Channels 1-4. Each tab page has the same controls on it and are populated with values from a list of objects. Here's some shortened code to help better illustrate what I'm doing:
C#:
//class with fields:
public class ChannelFields
{
    public string apple;
    public string orange;
    public string grape;
}

//   Create List of Channel objects with fields from ChannelFields
public static List<ChannelFields> Channel = new List<ChannelFields>(4)
{
    new ChannelFields(),
    new ChannelFields(),
    new ChannelFields(),
    new ChannelFields(),
};
On each tab, say I have 3 textboxes. leading to 12 total (txtApple1,txtApple2,txtApple3, txtApple4, txtOrange 1.......txtGrape4)

Right now, I'm populating the textboxes manually on each form like:
C#:
txtApple1.text = Channel[0].apple;
txtApple2.text = Channel[1].apple;
// ...
txtGrape4.text = Channel[3].grape;
Like I said this works, but this is only a representation with a simple example. My project has more fields and controls that I'm working with, so I was hoping there'd be a way to do something to reference them by index. I know my controls aren't indexed, I'm just trying to illustrate what I'd like to do.
C#:
for(int ch =-0; ch<4;ch++)
{
    int tab = ch + 1;
    txtApple[tab].text = Channel[ch].apple;
    txtOrange[tab].text = Channel[ch].orange;
    txtGrape[tab].text = Channel[ch].grape;
}
Any help is appreciated.
 
Last edited by a moderator:
If you have the same set of controls multiple times then you should be creating a user control first. That user control will encapsulate the group of repeated controls. You can then add an instance of that control to each TabPage. All the controls in the user control would be private and you would expose only what is required. You could provide pass-through properties for the Text of each TextBox and/or provide a method that allows you to pass all the data into the user control in one go and then have it distributed to the appropriate controls internally.
 
And furthermore, I'd each tab page is the same (e.g. contains the same kind of user control), then create a custom tab page and just instantiate 4 instances of the custom tab page. For each instance, pass in a specific ChannelFields instance from the list. Each custom tab page should then pass a reference to that ChannelFields instance to the user control. Each user control then takes care of binding the ChannelFields' properties to the controls in the user control.
 
Thanks for the input. I returned to this today and have it working thanks to your suggestions. I have one other question regarding this. In the same scenario listed above, I now have a tab control that loads custom tab pages with each tab page containing an instance of the same user control. If I wanted to set properties of a control on tabs 2-4 based on the input of the same control on tab page 1, is this possible? This is only for one control, and whatever is selected on the first tab page needs to set that same value on the same controls on the other three tab pages.
 
Of course it's possible. Just use a for loop and set the limits appropriately. If you haven't already, you should have created members in your user control to pass-through the relevant data to the child controls and then corresponding members on the custom TabPage. That way, you only ever have to deal with the TabPage from the outside, which is the point. For instance, if you had givenNameTextBox as a child control, it would be private and you would add this property to the user control:
C#:
public string GivenName
{
    get => givenNameTextBox.Text;
    set => givenNameTextBox.Text = value;
}
You would then add a similar property to the TabPage and pass through that property from the user control. Your form code would then deal only with the GivenName property of the TabPage, which you would have to cast as your custom type.
 
Back
Top Bottom