Solved

Change the font of an object inside a foreach loop

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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