2016-03-08 45 views
0

你能帮我把焦点设置到RichTextBox吗?我正在尝试创建多个RichTextBox。当我OpenFile文本文件所有RichTextBox1,RichTextBox2和RichTextBox3被使用。RichTextBox1活动,然后RichTextBox2不活动

我希望当RichTextBox1是活动的,那么其他的RichTextBox无效,

我试图设置,则在焦点没有用,但我不知道如何使用它。

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click 
    Try 
     With OpenFileDialog1 
      .Filter = "Text files(*.txt)|*.txt|" & "All files|*.*" 
      If Not focussedrtb Is Nothing Then 
       focussedrtb = rtb1 
       .ShowDialog() = DialogResult.OK 
       filename = .FileName 
       sr = New StreamReader(.OpenFile) 
       rtb1.Text = sr.ReadToEnd() 
       rtb1.Focus() 
       rtb1.Enabled = True 
       rtb2.Enabled = False 
      Else 
       rtb2.Text = sr.ReadToEnd() 
       rtb1.Enabled = False 
       rtb2.Enabled = True 
      End If 
     End With 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    Finally 
     If Not (sr Is Nothing) Then 
      sr.Close() 
     End If 
    End Try 
End Sub 

感谢您编辑我的问题。

回答

0

如果你试图将焦点设置到rtb1然后重新排列这些:

rtb1.Enabled = True 
rtb2.Enabled = False 
rtb1.Focus() 

或实际上这些

me.ActiveControl = rtb1 

rtb1.Select(

**

之一

更新:

**

If Not focussedrtb Is Nothing Then 
     focussedrtb = rtb1 
     .ShowDialog() = DialogResult.OK 
     filename = .FileName 
     sr = New StreamReader(.OpenFile) 
     rtb1.Text = sr.ReadToEnd() 
     rtb1.Enabled = True 
     rtb2.Enabled = False 
     rtb1.Focus() 
    Else 
     rtb1.Enabled = False 
     rtb2.Enabled = True 
     rtb2.Text = sr.ReadToEnd() 
    End If 

解释你需要启用之前,你可以把价值的文本框

+0

我只想问,如果我要使用Else语句? 如果你不介意你能编辑我的代码吗? 我尝试使用你的答案 rtb1.Enabled =真 rtb2.Enabled =假 rtb1.Focus()这些 me.ActiveControl = RTB1 或 rtb1.Select的 或实际上为( )但它没有工作 –

+0

所以什么不工作? – Claudius

+0

rtb1仅在运行。 当我打开文件在rtb1它的作品 但当我打开文件在rtb2它没有工作 –