首页 > 解决方案 > 如何在动态文本框中写入一些文本

问题描述

我必须做一个填字游戏,它的文本框是动态创建的。在游戏的某个时刻,我需要用数据库中的正确字符(数据)填充它。

这是我到目前为止所做的。

public void GenerateCrosswordAdmin()
{
    string crossWordName = cb_cw.Text; // the name of the table

    string dbPath = Application.StartupPath + @"\CrossWord.mdf";

    SqlConnection con = new SqlConnection();
    con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + dbPath + ";Integrated Security=True;User Instance=True";
    con.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "select * from CrossWords where name='" + crossWordName + "'";
    SqlDataReader reader = cmd.ExecuteReader();

    int ROWS = 0;
    int COLS = 0;
    while (reader.Read())
    {
        ROWS = Convert.ToInt32(reader[2]);
        COLS = Convert.ToInt32(reader[3]);
    }
    cmd.Dispose();
    reader.Dispose();

    MyTextBox mtb = new MyTextBox(ROWS, COLS, tabControl1); // triggers the constructor to work with cols and rows

    cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "select * from Solves where Id='" + idCrossWord + "'";
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        mtb.col = Convert.ToInt32(reader[1]);
        mtb.row = Convert.ToInt32(reader[2]);

        string solution = reader[4].ToString();
        foreach (char c in solution)
        {
            mtb.Text = c.ToString().ToUpper();
            mtb.TextAlign = HorizontalAlignment.Center;
            if (reader[3].ToString() == "horizontal")
            {
                mtb.col++;
            }
            else mtb.row++;
        }
    }
}

public class MyTextBox : TextBox
{
    public TabControl tabControl1;
    public static List<List<MyTextBox>> tbs { get; set; }
    public static List<MyTextBox> tbslist { get; set; }
    public int row { get; set; }
    public int col { get; set; }
    public MyTextBox() { }
    public MyTextBox(int rows, int cols, TabControl tabControl1)
    {
        tbs = new List<List<MyTextBox>>();
        tbslist = new List<MyTextBox>();
        this.tabControl1 = tabControl1;

        Point p = new Point(20, 30);
        for (int row = 1; row <= rows; ++row)
        {
            List<MyTextBox> newRow = new List<MyTextBox>();
            tbs.Add(newRow);
            for (int col = 1; col <= cols; ++col)
            {
                MyTextBox tb = new MyTextBox();
                tb.Name = "tb_CWAdmin";
                tb.Size = new Size(25, 25);
                tb.Font = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold);
                tb.row = row;
                tb.col = col;
                tb.Location = p;
                newRow.Add(tb);
                tbslist.Add(tb);
                //tb.TextChanged += new EventHandler(tb_TextChanged); // don't need it for this problem
                tabControl1.TabPages[0].Controls.Add(tb);
                p.X += 25;
            }
            p.X = 20;
            p.Y += 20;
        }
    }

    /*public void tb_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;
        string upLetter = tb.Text[0].ToString().ToUpper();
        tb.Text = upLetter;
        tb.TextAlign = HorizontalAlignment.Center;
    }*/
}

打开应用程序时,组合框会填充一些填字游戏名称,存储在数据库中。一旦选择了一个,带有所有正确数据的游戏应该出现在屏幕上。

我的表格已正确生成,但文本框没有填满文本。

图片

标签: c#

解决方案


推荐阅读