我正在創建聊天應用程序,我想在Bold中顯示姓名。當窗體第一次加載,我顯示從數據庫上使用這些代碼行一個RichTextBox
控制的歷史對話,我想以粗體顯示的名稱:爲什麼這些代碼行不按預期運行?
這裏是所有的代碼,以使這成爲可能:
string strProvider = "Data Source=" + srv_host + ";Database=" + srv_db + ";User ID=" + srv_user + ";Password=" + srv_pass;
MySqlConnection myConn = new MySqlConnection(strProvider);
try
{
myConn.Open();
string strCmd = "SELECT * FROM comments WHERE [email protected]_id AND (([email protected] AND [email protected]) OR ([email protected] AND [email protected])) ORDER BY at_time ASC";
MySqlCommand myCmd = new MySqlCommand(strCmd, myConn);
myCmd.Parameters.AddWithValue("from", frm_usr);
myCmd.Parameters.AddWithValue("to", to_usr);
myCmd.Parameters.AddWithValue("task_id", tid);
myCmd.ExecuteNonQuery(); // execute now
MySqlDataReader dr = myCmd.ExecuteReader();
while (dr.Read())
{
string text = dr.GetValue(1).ToString() + ": " + dr.GetValue(6) + Environment.NewLine;
richTextBox1.AppendText(text);
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = dr.GetValue(1).ToString().Length;
richTextBox1.SelectionFont = new Font(richTextBox1.Font,FontStyle.Bold);
}
myConn.Dispose();
}
catch (Exception E) { MessageBox.Show(E.Message); }
而這些代碼並不如預期的工作線:
while (dr.Read())
{
string text = dr.GetValue(1).ToString() + ": " + dr.GetValue(6) + Environment.NewLine;
richTextBox1.AppendText(text);
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = dr.GetValue(1).ToString().Length;
richTextBox1.SelectionFont = new Font(richTextBox1.Font,FontStyle.Bold);
}
編輯:
dr.GetValue(1).ToString()
持有用戶全名dr.GetValue(6).ToString()
保留該信息
上面代碼的問題是,它只顯示粗體的第一個名字,但其他行不受影響。看到圖片
有人可以告訴我爲什麼代碼不工作的原因。我無法弄清楚錯誤在哪裏。
謝謝
什麼框架? WPF或Winforms編輯:沒關係,我看到它的winforms – 2013-02-17 22:30:35