Solved

Change the font of an object inside a foreach loop

Posted on 2013-05-10
1
357 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
Allow users to select all checkboxes 6 60
Data Saving(2) 2 21
Performance  Html.BeginForm vs jQuery 3 11
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…
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now