c# - 如何在动态文本框中写入一些文本
问题描述
我必须做一个填字游戏,它的文本框是动态创建的。在游戏的某个时刻,我需要用数据库中的正确字符(数据)填充它。
这是我到目前为止所做的。
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# - 缩放级别上的 OxyPlot 更改间隔
- angular - Angular 6 - 使用 ng-content 时样式不起作用
- c# - asp mvc 核心 url 本地化
- ios - iOS 的 Firebase 集成示例 (FriendlyPix)
- reactjs - 不变违规:在 es6 导入中使用 React-Dom renderToString() 时出错
- python - 为什么 Python linecache 会影响 traceback 模块,但不会影响常规 traceback?
- oozie - 是否可以在不指定 inputDir 和 outputDir 的情况下运行 Oozie Spark Action
- linux - 当我在命令行中时,如何判断 Docker 映像正在运行什么 Linux?
- r - 删除文档中的文本并将其替换为空格
- asp.net - ASP.NET - 如何使用实体框架进行内部连接