?
Solved

Change the font of an object inside a foreach loop

Posted on 2013-05-10
1
Medium Priority
?
376 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 1600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 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