首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么让label自适应窗体大小

2013-04-09 
如何让label自适应窗体大小?怎么样能实现这样的效果?有好几个Label,里面的内容可以根据页面调整自动换行,

如何让label自适应窗体大小?
怎么样能实现这样的效果?
有好几个Label,里面的内容可以根据页面调整自动换行,应该是吧AutoSize设false,然后Anchor设置成top,right和left,这样可以实现遂改变换行,可是label的高度该怎样控制呢,使它可以适应,也不会挡住下面另外一个label!
怎么让label自适应窗体大小
怎么让label自适应窗体大小
两个图片说明题目意思,效果没有达到!

写了一小段测试实验了一下,始终不能成功!


        private void Form1_Load(object sender, EventArgs e)
        {
            string sqlcon = "Data Source=PC20130329;Initial Catalog=Exam;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlcon);

            int x = this.ClientRectangle.Width;

            string sql="select * from Question";
            SqlCommand command = new SqlCommand(sql,con);

            con.Open();
            SqlDataReader dataReader = command.ExecuteReader();
            dataReader.Read();
            string ss = (string)dataReader["Question"];

            Label ques = new Label();
            ques.Parent = panel;
            ques.Font = new Font("宋体", 14);
            ques.Width = x;
            ques.Height = 100;
            ques.AutoSize = false;
            ques.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right|AnchorStyles.Bottom;
            ques.Location = new Point(0, 0);
            ques.BorderStyle = BorderStyle.FixedSingle;
            ques.Text = ss;

            Label ques0 = new Label();
            ques0.Parent = panel;
            ques0.Width = x;
            /*//////高度
            int meihang=x/20;
            int a = ss.Length;
            int hang =( a % meihang == 0 ? (a / meihang) : ((a / meihang)+1));
            ques0.Height = hang * 20;
            *//////////


            ques0.AutoSize = false;
            ques0.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
            int y = ques.Location.Y + ques.Size.Height + 20;
            ques0.Location = new Point(0,180);
            ques0.BorderStyle = BorderStyle.FixedSingle;
            ques0.Text = ss;
            ques0.Font = new Font("宋体", 14);
  
            dataReader.Close();
            con.Close();
            con.Dispose();

        }

C# Label 换行
[解决办法]
在SizeChanged事件里面写吧,没试过
[解决办法]
不想自己计算高度,就用TableLayout控件,谁用谁知道

热点排行