在多行文本框上逐行显示文本

时间:2016-10-29 11:16:51

标签: c# c#-4.0 c#-3.0 facebook-c#-sdk c#-2.0

大家好,当我将数据从访问数据库检索到我的文本框多行文本框时,数据显示在一旁,我希望任何行都在其行中,例如我在访问字段中有5行,但在我的文本框中在同一行显示所有数据我应该帮忙什么?

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\db\\it.accdb");

con.Open();

OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from data where [ID] like(" + textBox9.Text + ")";
cmd.Connection = con;

var reader = cmd.ExecuteReader();
while (reader.Read())
{
    textBox1.Text = reader["Name"].ToString();
    textBox20.Text = reader["Description"].ToString();

    // ----------------------------------------------
    // These doesn't work with me :
    // ----------------------------------------------
    //textBox2.Text = Environment.NewLine;
    //textBox28.Text = textBox28.Text + Environment.NewLine;
    //textBox2.Text = textBox28.Text + Environment.NewLine;

}

con.Close();

2 个答案:

答案 0 :(得分:1)

试试这个,

textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;//Other settings is Horizontal and Both
textBox1.AcceptsReturn = true;
textBox1.WordWrap = true;

textBox20.Multiline = true;
textBox20.ScrollBars = ScrollBars.Vertical;//Other settings is Horizontal and Both
textBox20.AcceptsReturn = true;
textBox20.WordWrap = true;

while (reader.Read())
{
    textBox1.Text += reader["Name"].ToString() + Environment.NewLine;
    textBox20.Text += reader["Description"].ToString() + Environment.NewLine;
}

答案 1 :(得分:0)

使用字符串'换行符'“\ n”来创建新行。像这样:

textBox1.Text = reader["Name"].ToString() + "\n";
textBox20.Text = reader["Description"].ToString() + "\n";