Dynamically generated buttons

Aloha!
Currently, I am working on another small app which I will present here shortly. Its function will be disclosed later as that fact is of no importance to this post. What is important is that some buttons the app will create automatically depending on some user settings. The buttons will be arranged in, let’s call it excel-style, in a table with equal size cells. So, the buttons need to be generated in a loop. That seems to be an easy task, but it becomes a bit trickier if you want them to perform different action. Here’s how to do that.

The code above shows how to generate the buttons with individual action performed by each button. But the solution above requires you to know how many buttons your program will create and still manually write separate switch/case instructions for each but. Tricky, but in some cases might be enough.

If it isn’t enough C# gives you opportunity to create actions delegates and store them in a dictionary.

These are two approaches to dynamically created buttons with individual actions for each button.

This post has been compiled from:
http://stackoverflow.com/questions/1434282/how-do-i-create-5-buttons-and-assign-individual-click-events-dynamically
http://stackoverflow.com/questions/33107026/dynamic-switch-cases
http://stackoverflow.com/questions/8414930/is-it-possible-to-declare-generic-delegate-with-no-parameters

 

 

More on dictionaries

Shalom!

Sometime ago I mentioned usage dictionaries to workaround lack of possibility to use variable which name is stored in another variable. It is useful trick (at least for me – beginner) but dictionaries may be employed to perform multitude of tasks. So, to preserve my newly gained skills I present most important aspects of dictionaries.

The main idea behind dictionaries is to have a structure that holds certain values tied to certain keys. It may be depicted as 1-D array which instead of numeric indexes uses any type of variables as example below shows:

stock

INITIALISING A DICTIONARY

Dictionary is a collection. To use it you need to include appropriate namespace: System.Collections.Generic. Definition of a dictionary must specify three things: its name, type of key and type of stored value:

The example below initialises empty dictionary – it was created using a constructor that doesn’t take any arguments. However, this is not the only option. Visual Studio suggest six different variations of constructor. You can set initial size of a dictionary, method of comparing keys – I am not going to describe each constructor as Visual Studio does that sufficiently. I just mention that it is possible to initialise a dictionary using a constructor that takes existing dictionary as an argument. Such action will copy of an argument dictionary to the newly created one. I find it most useful.

 

OPERATIONS INSIDE DICTIONARY

Adding new element
It is that simple that I was tempted to omit that, but for sake of conscientiousness here’s a quick example. To add a new element to a dictionary you need to use Add method and provide key and value of new element:


Accessing/modifying value of an element

Again, it’s very simple. Code below illustrates that:


Looping through whole dictionary
It is no secret that to run through whole dictionary you have to use foreach loop. To correctly design that loop you need to use KeyValuePair as an iterator. It is also necessary to provide types of keys and values accordingly to dictionary we want to loop through.

Other useful methods for dictionaries
Here are some other methods that you may find useful. These are quite simple operations so their description will be presented together with the code.

 

EXAMPLE OF USAGE

So, now we have basic comprehension of what dictionaries are and how to use them. I am sure you will easily find a task that which can utilise dictionaries. But to provide an example I’ll show one possible implementation.
Currently, I am writing an application which will allow user to switch between different languages (dictionaries, languages – that obvious, isn’t it?). The plan is to store language data in an XML file and load its content to a dictionary. Then, instead of hard-coding every word that is visible for end-user the program will display data stored in the dictionary. Here’s a class that allows that:

The class is static – there’s no need to create various instances as only one language can be displayed. The class has only one property – LoadedLanguage – a dictionary that uses string key to store string values.
There are two methods: method ExistLanguge(string) checks if a language file chosen by user exists. If it does the method returns true and the program continues. If the file doesn’t exist the program displays a warning message. Additionally, if the chosen language is English and its XML file doesn’t exist the program terminates as English is default language.
Another method is used to load specified language to the dictionary. As earlier said the language data is stored in the XML file. That XML file is divided into sections depending on which form it relates to.

Method LoadLanguage(string, string) takes as arguments language to load and section of XML file to load. Then it loops through the XML file and creates key/value pairs in the dictionary according to nodes present between specified area. Then these pairs are used in various parts of the program to display labels in specified language.

So, that quick guide through dictionaries would be it for today. Enjoy!

Examples (*.cs)