请问在文本框中输入行列,点击按钮怎么实现与输入相一致的行列label控件?

解决方案 »

  1.   

    可以参考一下我以前回答别人的一个例子
    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 WindowsFormsApplication21
    {
        public partial class Form1 : Form
        {
            int TotalCount = 0;        public Form1()
            {
                InitializeComponent();            TextBox TB = new TextBox();
                TB.Parent = this;
                TB.TextChanged += new EventHandler(TB_TextChanged);
            }        void TB_TextChanged(object sender, EventArgs e)
            {
                int Count;
                if (((TextBox)sender).Text == String.Empty)
                    Count = 0;
                else
                    if (!int.TryParse(((TextBox)sender).Text, out Count))
                        return;            int Temp = 1;            for (; Temp <= Count; Temp++)
                    if (this.Controls.Find("DyTB" + Temp.ToString(), true).Length == 0)
                    {
                        TextBox DyTB = new TextBox();
                        DyTB.Name = "DyTB" + Temp.ToString();
                        DyTB.Text = "DyTB" + Temp.ToString();
                        DyTB.Location = new Point(0, 20 + Temp * 20);
                        DyTB.Parent = this;
                    }            for (int i = Temp; i <= TotalCount; i++)
                    this.Controls.Remove(this.Controls.Find("DyTB" + i.ToString(), true)[0]);            TotalCount = Temp - 1;
            }
        }
    }
      

  2.   

     我写的代码指只输出了一个label控件,谁可以帮我改一下,谢谢楼上的回帖!
    private void btnRun_MouseClick(object sender, MouseEventArgs e)
            {
                int k = Convert.ToInt32(txtRows.Text);
                int y = Convert.ToInt32(txtCols.Text);
                for (int i = 1; i <= k; i++)
                {
                    for (int j = 1; j <= y; j++)
                    {
                        // Console.Write(i + "-" + j + "\t");
                        Label lbl = new Label();
                        lbl.Text = i + "-" + j;
                        lbl.Location = new Point(e.X, e.Y);
                        lbl.BackColor = Color.Yellow;
                        this.groupBox1.Controls.Add(lbl);
                    }
                }        }
      

  3.   

     lbl.Location = new Point(e.X, e.Y); //这句是位置??
    莫非都输出到一起了
      

  4.   

    你可以放断点查看groupBox1.Controls里是否创建成功。
      

  5.   

    this.groupBox1.Controls.Add(lbl); 
    Add后面的lbl每次都能循环赋值给它的text属性,可是lbl还是lbl,所以控件根本没有循环,还不知道怎么解决,??晕。
      

  6.   

     private void btnRun_MouseClick(object sender, MouseEventArgs e) 
            { 
                int k = Convert.ToInt32(txtRows.Text); 
                int y = Convert.ToInt32(txtCols.Text); 
                int px=5;    //加了两个改变labl坐标的变量,你是一下效果
                int py=10;
                for (int i = 1; i <= k; i++) 
                { 
                    py=10;
                    for (int j = 1; j <= y; j++) 
                    { 
                        // Console.Write(i + "-" + j + "\t"); 
                        Label lbl = new Label(); 
                        lbl.Text = i + "-" + j; 
                        lbl.Location = new Point(px, py);
                        py+=10; 
                        lbl.BackColor = Color.Yellow; 
                        this.groupBox1.Controls.Add(lbl); 
                    } 
                    px+=10;
                }         } 
      

  7.   

    你把px+=150;
    py+=60;
    就能看出效果了
      

  8.   

    好像不是location的问题,试过几次了。
    循环的过程中一直在改变Lbl的text值与位置,可是结果只输出了最后一个控件。
      

  9.   

    不好意思。我添加了一些代码,lbl.parent=this;然后把实例化lbl放在了全局的位置,
    没有看出效果,现在可以循环了。谢谢啊。
      

  10.   

    我觉得这些都不是好方法,你的思路就不好,这么出来的界面也很丑,应该考虑用datagridview
      

  11.   

    这句:private void btnRun_MouseClick(object sender, MouseEventArgs e) 
    那来的 e.X, e.Y
      

  12.   

    e是参数啊。上面有:MouseEventArgs e
      

  13.   

    我把this.groupBox1.Controls.Add(lbl);改成了this.Controls.Add(lbl);我没加groupbox
      

  14.   

    应该是一样效果吧。我加groupbox是因为按题目做的原因。还有,你帮我加的两个改变labl坐标的变量,在循环里最后写成这样,横纵坐标互换一下:
     for (int i = 1; i <= k; i++) 
                { 
                    px=10; 
                    for (int j = 1; j <= y; j++) 
                    { 
                       
                        Label lbl = new Label(); 
                        lbl.Text = i + "-" + j; 
                        lbl.Location = new Point(px, py); 
                        px+=10;                     lbl.BackColor = Color.Yellow; 
                        this.groupBox1.Controls.Add(lbl); 
                    } 
                    py+=10;             } 
      

  15.   

    哦。还有相加的值写大一点能看出效果,px+=30,py+=30就行了。
    可以在循环里加一句:lbl.Autosize=true;效果好一点。