Results 1 to 4 of 4

Thread: Creating and using tabcontrols at runtime

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    6

    Creating and using tabcontrols at runtime

    Hi Guys,
    This is the first time I have wanted to use tabstrips and have a couple of questions .
    I can build the tabstrip fine and name it - that part is from what I can see the easy bit.
    What I want to do is select a tab then create a listbox on that tab, and then pupulate the list box with data.
    For this test I am using the event viewer.

    The system loades the logs into memory, and then creates however many system logs there are ie application, system security etc etc.

    But how the hell do I select one of these tabs then draw a listbox and then address the list box - Ideally the list boxes etc will be created at the time of the tab ?

    Here is my code for this (By the way its VB 2010)
    Code:
            Dim log As EventLog
            Dim lstname As String
    
            'Gets logs on the local machine
            remoteEventLogs = EventLog.GetEventLogs(System.Environment.MachineName)
    
            'Create the Tab's
            For Each log In remoteEventLogs
    
                TabControl1.Controls.Add(New TabPage(log.Log))
                Dim tp As New TabPage
                tp.Text = log.Log
                Me.TabControl1.TabPages.Add(tp)
                Me.TabControl1.SelectedTab = tp
    
            Next log
    
            'now select a tab and create a list box ??
            Me.TabControl1.SelectTab(1) ' Selected tab 1 just as its the first
            ' Create an instance of the ListBox.
            Dim listBox1 As New ListBox()
            ' Set the size and location of the ListBox.
            listBox1.Size = New System.Drawing.Size(1055, 537)
            listBox1.Location = New System.Drawing.Point(106, 7)
            ' Add the ListBox to the form.
            selectedtab.Controls.Add(listBox1)
    Thanks for your help in advance

    Jez
    Last edited by VBnewbie1001; Jan 23rd, 2011 at 11:01 AM. Reason: forgot to add version of VB

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Creating and using tabcontrols at runtime

    What is this:
    Code:
    TabControl1.Controls.Add(New TabPage(log.Log))
                Dim tp As New TabPage
                tp.Text = log.Log
                Me.TabControl1.TabPages.Add(tp)
    In the first line, you create a new tab page and add it to the control. You then create a new tab page and add that to the control. Did you really mean to add two pages there?

    Aside from that, you are running into problems because you declared tp inside the For loop, which means it is not in scope outside of the for loop. However, you probably want it in scope for the whole method, so declare it outside the loop, or, perhaps better, move the listbox creation into the loop. After all, it sounds like you want to put a listbox on each tab, so you might as well do that inside the loop. You are adding the listboxes to the selected tab, but what you really want to do is add a listbox to tp for every tp created in the loop. Other than that, you are pretty much there.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    6

    Re: Creating and using tabcontrols at runtime

    Hi Shaggy Hiker,

    Thanks that worked - It built a list box on each tab - which is fine - I need some more help though, how do I increment the name listbox1 to listbox2,3,etc ? and then address the list boxes on the tabs ? Ie if I wanted to load data into tab1's listbox ? Thanks in advance again !!

    Code:
            Dim log As EventLog
            Dim lstname As String
            Dim eventCntr As Integer = 1
    
    
            'Gets logs on the local machine, give remote machine name to get the logs on the remote machine
            remoteEventLogs = EventLog.GetEventLogs(System.Environment.MachineName)
    
            Console.WriteLine("Number of logs on computer: " & remoteEventLogs.Length)
    
            'Display the list of event logs
    
    
    
            Dim tp As New TabPage
    
            For Each log In remoteEventLogs
       
                'TabPage1.Name = (remoteEventLogs(1).ToString)
    
    
                'TabControl1.Controls.Add(New TabPage(log.Log))
    
                tp.Text = log.Log
                Me.TabControl1.TabPages.Add(tp)
                Me.TabControl1.SelectedTab = tp
    
                Dim listBox1 As New ListBox()
                ' Set the size and location of the ListBox.
                listBox1.Size = New System.Drawing.Size(1055, 537)
                listBox1.Location = New System.Drawing.Point(106, 7)
                ' Add the ListBox to the form.
                Controls.Add(listBox1)
                eventCntr = 1
                For Each eventLogEntrycop In eventLogcop.Entries
    
                    listBox1.Items.Add("Event Number:" & eventCntr)
    
                    listBox1.Items.Add(eventLogEntrycop.EntryType.ToString)
    
                    listBox1.Items.Add(eventLogEntrycop.TimeGenerated.ToString)
    
                    listBox1.Items.Add(eventLogEntrycop.Source.ToString)
    
                    listBox1.Items.Add(eventLogEntrycop.Category.ToString)
    
                    listBox1.Items.Add(eventLogEntrycop.EventID.ToString)
    
                    listBox1.Items.Add(eventLogEntrycop.MachineName.ToString)
    
                    listBox1.Items.Add(eventLogEntrycop.Message.ToString)
    
                    listBox1.Items.Add("-----------------------------------------------")
    
                    eventCntr = eventCntr + 1
    
                    listBox1.Refresh()
    
                Next

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Creating and using tabcontrols at runtime

    There are a couple ways you could accomplish this. The simplest would be to have a integer variable and keep adding one to it every time through the loop.

    Whoa! Just looked at that code. If it is working, it is likely to not be working quite the way you expect. You create tp outside the loop, rather than just declaring it outside the loop. What you actually want is this:
    Code:
    Dim tp as TabPage
    
    For Each log In remoteEventLogs
     tp = New TabPage
    because you want to be creating a new tab page each time through the loop. Otherwise, there is only one tab page, it is just being added to the collection multiple times, which will cause some really odd behavior.

    The next thing is that each new tab page could have a name. You have such code, but you commented it out. What I would suggest is that the listbox name could be related to the tab page name if each tab page had a unique name. For instance, if tab page one was called TP1, then you could call the listbox something like lbTP1. Since you were headed in the direction of naming each tab page based on the events log, would that still make sense? You couldn't use RemoteEventsLogs(1), as you had in the commented code, as that would give every tab page the same name, but is there something in the log (the loop control variable) that would suit?

    If not, then you might use an integer variable and increment it each time through the loop. Therefore, it would look like:

    Code:
    Dim tp as TabPage
    Dim sequenceVar as Integer
    For Each log In remoteEventLogs
     tp = New TabPage
     tp.Name = "TP" & sequenceVar.ToString
     sequenceVar += 1
     'etc
    My usual boring signature: Nothing

Posting Permissions

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



Click Here to Expand Forum to Full Width