Windows Controls Problem

VBOfficer

Newcomer
Joined
Jan 22, 2009
Messages
24
Hi,
I have a TabControl on my form with 1 TextBox in EACH Tab, total 2 TextBox...
I fill each TextBox during a loop like this:
Visual Basic:
For i As Integer = 1 To 100
    ReportTextBoxX1.Text = ReportTextBoxX1.Text + i.ToString + vbNewLine
    ReportTextBoxX2.Text = ReportTextBoxX2.Text + i.ToString + vbNewLine
Next
However, I need to scroll down both TextBox in each Tab and have to use this code:
Visual Basic:
Private Sub ReportTextBoxX1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportTextBoxX1.TextChanged
    ReportTextBoxX1.SelectionLength = 0
    ReportTextBoxX1.SelectionStart = ReportTextBoxX1.Text.Length
    ReportTextBoxX1.ScrollToCaret()
End Sub
Private Sub ReportTextBoxX2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportTextBoxX2.TextChanged
    ReportTextBoxX2.SelectionLength = 0
    ReportTextBoxX2.SelectionStart = ReportTextBoxX2.Text.Length
    ReportTextBoxX2.ScrollToCaret()
End Sub
However, the problem is that it only works for the SELECTED TAB, no matter which tab is selected.
I do believe, this is because Windows Forms only shows one control at a time, and just update that...
I was hoping this code solved my problem but it didn't:
Visual Basic:
...Before adding items to TextBox

If Not ReportTextBoxX1.IsHandleCreated Then
    Dim handle As IntPtr = ReportTextBoxX1.Handle
End If
If Not ReportTextBoxX2.IsHandleCreated Then
    Dim handle As IntPtr = ReportTextBoxX2.Handle
End If
Can anyone help me please?! :confused:
 
How about handling the SelectedIndexChanged event on the tabcontrol to scroll the textbox to the charat?

Code:
Private Sub TabControl1_SelectedIndexChanged(ByVal Sender As Object, ByVal e As EventArgs) Handles TabControl1.SelectedIndexChanged
    Dim sel As Integer = TabControl1.SelectedIndex
    Dim txt As Textbox = CType(TabControl1.Controls("TextBox" & (sel + 1).ToString()), TextBox)
    txt.ScrollToChar()
End Sub
 
The problem is that controls on second tab were never displayed on screen so Windows Forms framework hasn't created Windows Handles for them.

You can ensure that is done if you for example execute this code before you start your thread:
Visual Basic:
If Not ReportTextBoxX2.IsHandleCreated Then
    Dim handle As IntPtr = ReportTextBoxX2.Handle
End If
 
Back
Top