日期:2014-05-17  浏览次数:20894 次

如何让label自适应窗体大小?
怎么样能实现这样的效果?
有好几个Label,里面的内容可以根据页面调整自动换行,应该是吧AutoSize设false,然后Anchor设置成top,right和left,这样可以实现遂改变换行,可是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));