PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Help with tabcontrol-VBForums
Results 1 to 9 of 9

Thread: Help with tabcontrol

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    69

    Help with tabcontrol

    Hello,

    I have a chat program I'm working on, and the main form contains a list of active users in a listbox control. When the selected user is double clicked, I have a message window open with a tab control.
    My intention is to have a tab for each separate user on this form. So if I double click user a the message form pops up with a new tab for user A. If that window is still open and I double click user B, a new tab should be formed on the message window.
    I am passing the username to the message form the main form on the doubleclick event and it appears to work fine for the first tab to be created. However, when I double click the second user, the message window regains focus, with the same original tab (created for the first user) selected. This is what I have for code:

    message window load event:
    Code:
    tabcontrolUsers.TabPages.Add(strUser)
    
            Dim rtbHistory As New RichTextBox()
    
            tabcontrolUsers.SelectedTab.Controls.Add(rtbHistory)
    
            rtbHistory.Dock = DockStyle.Fill
    
            rtbHistory.ReadOnly = True
    
            rtbHistory.BackColor = Color.White
    and then this sub for the double click event:

    Code:
    Public Sub lbUsers_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbUsers.DoubleClick
    
            frmMessages.User = lbUsers.SelectedItem
    
            frmMessages.Show()
    
        End Sub
    Can anyone point me in the right direction on this? In addition, if possible, I would like to add some functionality where if the message window is open, and there is an existing tab for a particular user name when double clicked, that tab is brought to focus instead of another tab created. In other words, I want to prevent multiple tabs from being created on the form, just one per. Thanks in advance to anyone that can help out.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,126

    Re: Help with tabcontrol

    here's my solution. listbox1 contains userNames:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim frm As Form2
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         ListBox1.DataSource = New String() {"name1", "name2", "name3", "name4", "name5"}
    7.     End Sub
    8.  
    9.     Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
    10.         If ListBox1.IndexFromPoint(e.Location) <> -1 Then
    11.             If Not My.Application.OpenForms.Cast(Of Form).Any(Function(f) TypeOf f Is Form2) Then
    12.                 frm = New Form2(ListBox1.Text)
    13.                 frm.Show()
    14.             Else
    15.                 frm.addTab(ListBox1.Text)
    16.             End If
    17.         End If
    18.     End Sub
    19.  
    20. End Class

    tabcontrol1 contains just 1 tabpage at design time:

    vb Code:
    1. Public Class Form2
    2.  
    3.     Public Sub New(ByVal userName As String)
    4.         InitializeComponent()
    5.  
    6.         TabControl1.TabPages(0).Text = userName
    7.  
    8.     End Sub
    9.  
    10.     Public Sub addTab(ByVal userName As String)
    11.         TabControl1.TabPages.Add(userName)
    12.         TabControl1.SelectedIndex = TabControl1.TabPages.Count - 1
    13.     End Sub
    14.  
    15. End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,126

    Re: Help with tabcontrol

    i just noticed the last part:

    vb Code:
    1. Public Sub addTab(ByVal userName As String)
    2.     If Not TabControl1.TabPages.Cast(Of TabPage).Any(Function(tp) tp.Text = userName) Then
    3.         TabControl1.TabPages.Add(userName)
    4.         TabControl1.SelectedIndex = TabControl1.TabPages.Count - 1
    5.     Else
    6.         TabControl1.SelectedTab = TabControl1.TabPages.Cast(Of TabPage).First(Function(tp) tp.Text = userName)
    7.     End If
    8. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    69

    Re: Help with tabcontrol

    Sweet, it's working perfectly. I had to make a few modifications, but I have now exactly what I wanted - thank you!

    This is what I ended up with (if interested):

    Code:
    Public Sub lbUsers_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbUsers.DoubleClick
    
            frmMessages.addTab(lbUsers.SelectedItem)
    
            frmMessages.Show()
    
        End Sub
    Code:
      Public Sub addTab(ByVal userName As String)
    
            If Not tabcontrolUsers.TabPages.Cast(Of TabPage).Any(Function(tp) tp.Text = userName) Then
    
                tabcontrolUsers.TabPages.Add(userName)
    
                tabcontrolUsers.SelectedIndex = tabcontrolUsers.TabPages.Count - 1
    
            Else
    
                tabcontrolUsers.SelectedTab = tabcontrolUsers.TabPages.Cast(Of TabPage).First(Function(tp) tp.Text = userName)
    
            End If
    
        End Sub
    Everything else I omitted for one reason or another, for example in the first block you had Dim frm as Form2 - but I could not use that, what is Form2? Also it seemed that functionality of the addtab sub makes the if then statement in the doubleclick even obsolete, so I left it out. It is working great though, thanks again!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,126

    Re: Help with tabcontrol

    Form2 was the test form i used. in your case it'd be frmMessages.
    it got you started though.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,126

    Re: Help with tabcontrol

    i would recommend:

    vb Code:
    1. Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
    2.     If ListBox1.IndexFromPoint(e.Location) <> -1 Then
    3.         frmMessages.addTab(lbUsers.SelectedItem)
    4.     End If
    5.     frmMessages.Show()
    6. End Sub

    if your user doubleclicks whitespace when there is no selecteditem, your code will open a new tab with no text

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    69

    Re: Help with tabcontrol

    I tried to incorporate that line but it had a problem with e.location so I left it out.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    69

    Re: Help with tabcontrol

    never mind, I see why - it's the mouseeventargs I didn't have in there...

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,126

    Re: Help with tabcontrol

    it's a different eventhandler. you were using doubleclick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width