2013-02-17 97 views
1

我正在創建聊天應用程序,我想在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()保留該信息

上面代碼的問題是,它只顯示粗體的第一個名字,但其他行不受影響。看到圖片enter image description here
有人可以告訴我爲什麼代碼不工作的原因。我無法弄清楚錯誤在哪裏。
謝謝

+0

什麼框架? WPF或Winforms編輯:沒關係,我看到它的winforms – 2013-02-17 22:30:35

回答

5

您在這裏遇到的問題是richTextBox1.SelectionStart始終爲零,這意味着格式將只應用於文本框中的第一行。

嘗試將其設置爲richTextBox1.SelectionStart = richTextBox1.TextLength

編輯:

嘗試將其設置爲

richTextBox1.SelectionStart = richTextBox1.TextLength - text.Length; 

編輯2:

我認爲無效的參數的是通過使用Environment.NewLine造成的。如果我使用"\n"代碼,則代碼正常工作。問題是Environment.NewLine在Windows上當然是\r\n,但richtextBox1似乎忽略了\r。這導致richTextBox1.TextLength - text.Length在第一次迭代中爲-1。

+0

它不起作用。除了正常字體的第一行外,我用粗體顯示了所有文本! – 2013-02-17 22:32:45

+0

@ShikataGaNai看我的編輯。 – 2013-02-17 22:36:58

+0

無效的參數值-1對'SelectionStart'無效參數名稱:SelectionStart – 2013-02-17 22:44:56

0

我無法測試,但它可能是因爲SelectionStart指數始終爲0,嘗試將其設置到行的開始,你剛纔添加

 while (dr.Read()) 
     { 
      string text = dr.GetValue(1).ToString() + ": " + dr.GetValue(6) + Environment.NewLine; 
      richTextBox1.AppendText(text); 
      richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexOfCurrentLine(); 
      richTextBox1.SelectionLength = dr.GetValue(1).ToString().Length; 
      richTextBox1.SelectionFont = new Font(richTextBox1.Font,FontStyle.Bold); 
     } 
+0

也行不通:(我除了第一個以外所有行都以粗體顯示 – 2013-02-17 22:43:11