2014-01-18 111 views
0

我想從我的C#Winforms應用程序中將一些行從MS Excel複製並粘貼到richTextBox。用戶將在鍵盤上按下CTRL + V並顯示Excel網格線。 ?可我怎麼確保粘貼的內容將只顯示爲文本#在富文本框中刪除電子表格格式

這似乎並不工作:

private void button1_Click(object sender, EventArgs e) 
{ 
    richTextBox2.Clear(); 
    richTextBox2.Focus(); 

    string strValues; 
    strValues = richTextBox1.Text; 

    var textInEachLine = richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); 
    string whereClause = string.Join("', '", textInEachLine).ToString(); 
    richTextBox2.AppendText(" IN ('" + whereClause + "')"); 
} 

private void richTextBox1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Control == true && e.KeyCode == Keys.V) 
    { 
     e.Handled = true; 
     string st = Clipboard.GetText(); 
     richTextBox1.Text = st; 
    } 
} 

爲我的代碼看起來是這樣的,我不能用一個文本框

+1

一個解決方案可能是使用'textbox'並將'multiline'選項設置爲true而不是'richtextbox' – Marek

+0

用我的代碼更新了我的問題。我無法使用文本框,因爲我使用的是字符串數組。除非你可以重寫它並告訴我 – PriceCheaperton

+0

對於我來說你提供的代碼正在工作,這個問題一定在別的地方。這個'richtextbox'上還有更多事件嗎? – Marek

回答

0

試試這個代碼

private void richTextBox1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control) 
      { 
       e.Handled = true; 
       string st = Clipboard.GetText(); 
       richTextBox1.Text = st; 
      } 
      base.OnKeyDown(e); 
     } 



private void richTextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
    { 
     if (e.Control == true && e.KeyCode == Keys.V) { e.IsInputKey = true; } 
    } 
+1

不工作...我仍然從Excel中獲取粘貼的網格 – PriceCheaperton

1

一個更好的解決辦法是在RichTextBox轉換爲多行文本框。

0

我已經四處尋找更好的答案。我已經提出了自己的解決方案,因爲我無法擺脫我支持的應用程序中的RichTextBox。我寫了一個表單應用程序來查看原始RTF數據,並從Excel中粘貼了一些單元格。 空白文本框產生...

{\ RTF1 \ ANSI \ ansicpg1252 \ deff0 \ deflang1033 {\ fonttbl {\ F0 \ fnil宋體;}} \ viewkind4 \ UC1 \ PARD \ F0 \ FS22 \帕 }

當少數細胞與它表現出一定的彩色文本粘貼...

{\ RTF1 \ ANSI \ ansicpg1252 \ deff0 \ deflang1033 {\ fonttbl {\ F0 \ fnil宋體; }} {\ colortbl; \ red255 \ green0 \ blue0; \ red0 \ green0 \ blue0;} \ viewkind4 \ uc1 \ trowd \ trgaph30 \ trleft-30 \ cellx1002 \ cellx2034 \ pard \ intbl \ cf1 \ f0 \ fs22 good text \ cell \ cf2 good text \ cell \ row \ intbl good text \ cell \ cf1 good text \細胞\排 \ PARD \ CF0 \相提並論 }

我發現,去掉「\細胞」或\ cellx ####」並返回文本到其它的RichTextBox導致所有相關格式被刪除。看來RichTextBox會去掉無效的格式。 有了這個,我在將文本粘貼到RichTextBox之前編寫了以下方法。

private void CleanClipboardText() 
    { 
     string cleaned = Clipboard.GetText(TextDataFormat.Rtf); 
     if (cleaned != null & cleaned != String.Empty) 
     { 
      Regex regex = new Regex(@"\\cellx\d{4}?"); 
      cleaned = regex.Replace(cleaned, " "); 
      regex = new Regex(@"\\cell"); 
      cleaned = regex.Replace(cleaned, " "); 
      Clipboard.SetText(cleaned, TextDataFormat.Rtf); 
     } 
    } 

而此事件爲「KeyDown」事件。

private void RTB_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Control && e.KeyCode == Keys.V) 
     { 
      RichTextBox rtb = sender as RichTextBox; 
      e.Handled = true; 
      CleanClipboardText(); 
      rtb.Paste(); 
     } 
    } 

我以前.Paste();,因爲我不希望覆蓋整個框的文本爲richTextBox1.Text = st;意志。但是,如果您願意,您可能希望CleanClipboardText方法返回一個rtf數據字符串,然後設置RichTextBox.Rtf