Question

i'm trying to come up with a very simple way to store a boolean value for each tabpage in a tabcontrol. each page has a textbox, and i would like to store a bool foreach page so if tabpage 1 has been saved, then bool1 is set to true, else false, and so on.

then when they go to close the program it will go over all the tabpages and see if each tabpage has been saved or not. i need to be able to create some sort of a list that i can search and manipulate.

i have previously used tag properties for this but for some reason that doesn't work PROPERLY anymore.

any help advice would be greatl;y appreciated :)

thank you,

bael.

Was it helpful?

Solution

Neither array, nor list, nor dictionary are the right data structure for tracking the boolean status of a set of objects. The structure that does this and nothing more is HashSet<T>. Either a T is in the set, or it isn't.

It's superior to a Dictionary<T, bool> because that actually maintains two states: whether the object is in the dictionary or not, and if it is, whether its value is true or false.

It's superior to a List<T> for two reasons: It's faster (though in your case, that will almost certainly be negligible), and it doesn't imply that there's some meaning to the order of the objects it contains, since the order of objects in a HashSet<T> is arbitrary.

OTHER TIPS

List<TabPage>

Store only those tabpage(s) that needs to be saved. If empty, nothing needs to be saved.

You can use for example a Dictionary<TabPage,bool>. At the start of your application:

var pageStates=new Dictionary<TabPage,bool>();

foreach(var page in tabControl.TabPages) {
    pageStates.Add(page, false);
}

To change the state of a TabPage:

pageStates[page]=true;

And when your application finishes:

foreach(var page in TabControl.TabPages) {
    if(pageStates[page]) {
        //The page is saved
    }
}

If you are iterating through without removing or adding elements use the array.

If you are iterating through but adding and removing elements use the List.

If you are using strings as keys then use the dictionary.

The dictionary has very fast lookup performance with a high number of elements compared to the List.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top