using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        string s = "你的爱好是:";
        public Form1()
        {
            
            InitializeComponent();
        }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int style = 0, k = 1;            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (listBox1.GetSelected(i))
                    style = style | k;
                else style = style & (~k);

                k = k * 2;            }
            FontStyle m = new FontStyle();
            m = (FontStyle)style;
            label1.Font = new Font(label1.Font.Name, label1.Font.Size, m);
        }         
    }
}
希望大家帮我解释一下红色的语句

解决方案 »

  1.   

    上面代码的作用和下面的一样            int style = 0,temp;
                for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
                {
                    temp = 1;
                    for (int j = 0; j < listBox1.SelectedIndices[i]; j++)
                    {
                        temp *= 2;
                    }
                    style += temp;
                }
                FontStyle m = new FontStyle();
                m = (FontStyle)style;
                label1.Font = new Font(label1.Font.Name, label1.Font.Size, m);当选中项下标
    为0,则style=1
    为1,则style=2
    为2,则style=4
    为3,则style=8正好对应于FontStyle 类型的
     Regular 普通文本。 
     Bold 加粗文本。 
     Italic 倾斜文本。 
     Underline 带下划线的文本当选中0和2则style=5
    对应于FontStyle 的普通文本+倾斜文本