Solved

Change the font of an object inside a foreach loop

Posted on 2013-05-10
1
365 Views
Last Modified: 2013-05-13
Hello,

I have a checkbox list that contains 3 list items.  The 3 items are Bold, Italicize, and Underline.  I have a textbox, a button, and a label.  When I write in my textbox, I want to be able change the fonts when I select on my checkboxes.  I want to be able to do this in a FOREACH loop.  I'm struggling out the gate to initialize my listitem to even make it bold.  I work in MicrosoftVisualStudio 2005.  

Front end code
  <tr>
        <td>
            <asp:CheckBoxList ID="FontChange" runat="server">
                <asp:ListItem ID="Bold" Text="Bold" Value="1"></asp:ListItem>
                <asp:ListItem ID="Italicize" Text="Italicize" Value="2"></asp:ListItem>
                <asp:ListItem ID="Underline" Text="Underline" Value="3"></asp:ListItem>
            </asp:CheckBoxList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:TextBox ID= "FontBox" runat="server" Text="Insert"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
    <asp:Label ID="Important" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="ShowFont" runat="server" Text="ClickIt" OnClick="clk_OnClick" />
        </td>
    </tr>  

Back end
protected void clk_OnClick(object sender, EventArgs arg)
        {
            Important.Text = FontBox.Text;
           
            foreach (ListItem li in FontChange.Items)
            {
                if (li.Selected)
                {
                   
                }
            }
        }

I can display the text in the label, but changing the font and selecting each one correctly proves difficult for me.  How can this work?
0
Comment
Question by:valley025
1 Comment
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 400 total points
ID: 39159028
You need to modify the label's font properties as follows:
    protected void clk_OnClick(object sender, EventArgs arg)
    {
        Important.Text = FontBox.Text;

        foreach (ListItem li in FontChange.Items)
        {
            SetFont(li, li.Selected);
        }
    }

    private void SetFont(ListItem li, bool value)
    {
        if (li.Value == "1")
            Important.Font.Bold = value;
        else if (li.Value == "2")
            Important.Font.Italic = value;
        else if (li.Value == "3")
            Important.Font.Underline = value;
    }

Open in new window

Note: 1) This code assumes that user can pick more than one property.
2) Please add ASP.NET zone as well to have more experts looking at your question
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question