Archive for Ottobre 5th, 2007

Get Collection of web/app config setting by group

Ottobre 5th, 2007

Description: Gets a namevaluecollection populated with a section of setting within a web/app config file in .net 2.0 applications

Link: http://www.codekeep.net/snippets/b4f05146-cce3-4826-919e-267ecc27b81d.aspx

<!-- web/app config example -->

<SectionName>

<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
<add key="key3" value="value3"/>

</SectionName>

// C# code example to get back the above values from config in a single collection.

NameValueCollection configSettings = (NameValueCollection)ConfigurationManager.GetSection("SectionName");

Adding and removing items between ListBoxes

Ottobre 5th, 2007

Description: One function for adding items, and one for removing items. Just add the functions in your code and call them with the name of the listbox as parameter.

Link: http://www.codekeep.net/snippets/e870819e-1ce0-47e6-818f-8feebf3f570b.aspx

        /// <summary>
        /// Adding selected item from one listbox to another.
        /// If listBoxTarget already has the value selected in listBoxSource, it will not be added.
        ///
        /// Example how to call:
        /// AddSelectedItem(listBox1, listBox2);
        /// </summary>
        /// <param name="listBoxSource"></param>
        /// <param name="listBoxTarget"></param>
        private static void AddSelectedItem(ListBox listBoxSource, ListBox listBoxTarget)
        {
            if (listBoxSource.SelectedIndex != -1 && !listBoxTarget.Items.Contains(listBoxSource.SelectedItem))
            {
                listBoxTarget.Items.Add(listBoxSource.SelectedItem);
            }
        }

        /// <summary>
        /// Removes the selected item from the given listbox.
        ///
        /// Example how to call:
        /// RemoveSelectedItem(listBox2);
        /// </summary>
        /// <param name="ListBoxName">Name of the listbox.</param>
        private static void RemoveSelectedItem(ListBox ListBoxName)
        {
            //Is there a selected item in the listbox?
            if (ListBoxName.SelectedIndex != -1)
            {
                ListBoxName.Items.Remove(ListBoxName.SelectedItem);
            }
        }