2011-06-21 30 views
0

我想輸入一個字符串轉換成一個RichTextBox,雖然我得到以下錯誤:如何插入一個字符串到一個RichTextBox

Cannot implicitly convert type 'string[]' to 'string' 

代碼如下:

private void testing_Click(object sender, EventArgs e) 
{ 
    // Get a ScheduledTasks object for the computer named "DALLAS" 
    string machineName = (@"\\" + System.Environment.MachineName); 
    ScheduledTasks st = new ScheduledTasks(machineName); 
    // Get an array of all the task names 
    string[] taskNames = st.GetTaskNames(); 
    richTextBox6.Text = taskNames; 

    // Dispose the ScheduledTasks object to release COM resources. 
    st.Dispose(); 

} 

回答

3

嘗試string.Join

string[] taskNames = st.GetTaskNames(); 
richTextBox6.Text = string.Join (Environment.NewLine, taskNames); 
1

您不能將數組添加到變量。使用foreach。

foreach (string item in taskNames) 
      richTextBox6.Text += item; 
2

當你的代碼中的註釋如下:
//得到所有你所創建的字符串數組的任務名稱
的數組。
然後您嘗試將其分配給字符串屬性。
這裏沒有自動轉換功能。
您應該將數組中的所有(少數)字符串合併爲一個字符串或僅選擇一個要分配的元素。

相關問題