2014-05-12 22 views
1

下面是我的代碼,當它調試時,文檔將在Rich TextBox中打開,但它可以編輯,因此我需要它爲ReadOnly。你能幫我解決嗎?如何在Visual Studio中使用RichTextBox使documents.docx只讀?

void ImportWord() 
{ 
    Microsoft.Office.Interop.Word.Application wordObject = new Microsoft.Office.Interop.Word.Application(); 
    object File = @"C:\Users\czu_ecu\Desktop\OAG-WORK\Management_Letter_Template1.docx" ; //this is the path 
    object nullobject = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application(); 
    wordobject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; Microsoft.Office.Interop.Word._Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject); docs.ActiveWindow.Selection.WholeStory(); 
    docs.ActiveWindow.Selection.Copy(); 
    this.richTextBox2.Paste(); 
    docs.Close(ref nullobject, ref nullobject, ref nullobject); 
    wordobject.Quit(ref nullobject,ref nullobject, ref nullobject); 
} 

private void btnTemML_Click(object sender, EventArgs e) //Handles the actions of the template button 
{ 
    btnAdd_Finding.Visible = false; 
    listView2.Visible = false; 
    label8.Visible = false; 
    richTextBox1.Visible = false; 
    listView1.Visible = false; 
    listView3.Visible = false; 
    richTextBox2.Visible = true; 

    ImportWord(); 

    listView1.Items.Clear(); 
} 
+0

我認爲這是winforms並添加到標籤,如果不刪除它。 –

+1

這似乎有點明顯,但你有沒有嘗試過:'richTextBox1.ReadOnly = true;' –

+0

你已經試過,但不工作 – user3587567

回答

1

您可以嘗試下面的代碼。

richTextBox2.Visible = true; 
richTextBox2.IsReadOnly = true;