Solved

Change the font of an object inside a foreach loop

Posted on 2013-05-10
1
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 36

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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