-3
if (Convert.ToString( checkedListBox1.SelectedItem )== "Bold")           
{
    label1.Font = new Font(label1.Font, FontStyle.Bold);           
}          
else if(Convert.ToString( checkedListBox1.SelectedItem )== "Italic")
{
    label1.Font = new Font(label1.Font, FontStyle.Italic);
}
else if (Convert.ToString(checkedListBox1.SelectedItem) == "UnderLine")
{
    label1.Font = new Font(label1.Font, FontStyle.Underline);
}
6
  • 2
    So what's the question? Dec 27, 2013 at 6:18
  • windows or asp.net which one you have doing Dec 27, 2013 at 6:35
  • windows ,its working but when i change the selection index the fontstyle is changed i want the previous changed font remained stored . like in word we can make bold and italic of our font at same time .
    – Mohit
    Dec 27, 2013 at 6:39
  • bcz of if condition i can set only one value at one time. i want code like that if i change the selected value the previous value remains stored in it unless i unselect it bcz in checkbox list we can select many values at same time .
    – Mohit
    Dec 27, 2013 at 6:47
  • where do you execute this condition? do you call it on checklistBox event ? or for example a button?
    – chouaib
    Dec 27, 2013 at 8:54

2 Answers 2

3

Pass label1.Font.Style every time and add your current style with it:

label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Bold);

Here is the complete code:

if (Convert.ToString(checkedListBox1.SelectedItem) == "Bold")
            {
                label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Bold);
            }
            else if (Convert.ToString(checkedListBox1.SelectedItem) == "Italic")
            {
                label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Italic);
            }
            else if (Convert.ToString(checkedListBox1.SelectedItem) == "UnderLine")
            {
                label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Underline);
            }
1

Try this

if (Convert.ToString(checkedListBox1.SelectedItem) == "Bold")
    {
        label1.Font.Bold = true;
    }
    else if (Convert.ToString(checkedListBox1.SelectedItem) == "Italic")
    {
        label1.Font.Italic = true;
    }
    else if (Convert.ToString(checkedListBox1.SelectedItem) == "UnderLine")
    {
        label1.Font.Underline = true;
    }

Update: This works in Webapplication only.

8
  • 'System.Drawing.Font.Bold' cannot be assigned, it is read only
    – Shaharyar
    Dec 27, 2013 at 6:54
  • not working Error gives an error Property or indexer 'System.Drawing.Font.Bold' cannot be assigned to -- it is read only.
    – Mohit
    Dec 27, 2013 at 6:55
  • my code is working but i want output like ms word if one value is selected other value will not change in if condition compiler always search a new value and delete the previous font style .
    – Mohit
    Dec 27, 2013 at 6:56
  • Sorry I was thinking its websolution.
    – Naveen
    Dec 27, 2013 at 6:56
  • i am using window application
    – Mohit
    Dec 27, 2013 at 6:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.