Focus in RichTextBox
-
I have a dynamically created RichTextBox that is created in the forms "OnLoad" event. For some reason it will not set the focus of the cursor inside the Text area. Once the program is running and I use the same code again it creates a new one and automatically sets the cursor inside it. What is wrong?
Dim rtbTextArea As New RichTextBox Dim newTab As New TabPage Dim newTime As Date = Now.ToString 'Set newtime to system time and date Dim strNewTime As String = newTime 'convert newTime to string TabControl1.TabPages.Add(newTab) 'Add new Tab Page to tab control TabControl1.SelectedTab = newTab With newTab .Text = strNewTime 'Set Tab Page text to System date and Time .Controls.Add(rtbTextArea) 'Add the rich text Box to the new tab control With rtbTextArea .Dock = DockStyle.Fill 'Set Rich Text box area to fill tab page .ContextMenuStrip = ContextMenuStrip1 .AcceptsTab = True .Focus() End With AddHandler rtbTextArea.SelectionChanged, AddressOf SelFontChk 'Assign SelctionChange Event Handler End With
Thanks in advance Thanks, Taen Karth -
I have a dynamically created RichTextBox that is created in the forms "OnLoad" event. For some reason it will not set the focus of the cursor inside the Text area. Once the program is running and I use the same code again it creates a new one and automatically sets the cursor inside it. What is wrong?
Dim rtbTextArea As New RichTextBox Dim newTab As New TabPage Dim newTime As Date = Now.ToString 'Set newtime to system time and date Dim strNewTime As String = newTime 'convert newTime to string TabControl1.TabPages.Add(newTab) 'Add new Tab Page to tab control TabControl1.SelectedTab = newTab With newTab .Text = strNewTime 'Set Tab Page text to System date and Time .Controls.Add(rtbTextArea) 'Add the rich text Box to the new tab control With rtbTextArea .Dock = DockStyle.Fill 'Set Rich Text box area to fill tab page .ContextMenuStrip = ContextMenuStrip1 .AcceptsTab = True .Focus() End With AddHandler rtbTextArea.SelectionChanged, AddressOf SelFontChk 'Assign SelctionChange Event Handler End With
Thanks in advance Thanks, Taen KarthYou can't SetFocus to a control until the form is displayed (i.e. active). If you want the control to have the focus when the form is initially displayed, set the control's TabIndex property to 0 (zero). ...Steve