Results 1 to 15 of 15

Thread: [RESOLVED] [2005] insert item into a custom listview in tabpage

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    37

    Resolved [RESOLVED] [2005] insert item into a custom listview in tabpage

    thx jmcilhinney on this thread Question in create tabpage with same component
    ok... now i had found another problem base on the thread above and hope got someone to help.

    After declare class and inherit the TabPage class, and in the constructor create a ListView. How do i add an item into a specific listview?
    for example i wan to add an item "ABC" into tabpage(2)---->listview.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: [2005] insert item into a custom listview in tabpage

    one way would be to make the listview a public member of the tab page then refence it throught the tab page....

    vb Code:
    1. Public Class TabPageEx
    2.     Inherits TabPage
    3.     Public lv as ListView 'define the list view member here
    4.     Public Sub New()
    5.         lv = New ListView 'then instance the listview member here
    6.         Dim clmcode As New ColumnHeader
    7.         Dim symbol As New ColumnHeader
    8.  
    9.         symbol.Text = "Symbol"
    10.         clmcode.Text = "Code"
    11.         With lv
    12.             .Name = "hello"    <--- the name did not display
    13.             .BorderStyle = System.Windows.Forms.BorderStyle.None
    14.             .MultiSelect = True
    15.             .Columns.AddRange(New ColumnHeader() {clmcode, symbol})
    16.             .Dock = System.Windows.Forms.DockStyle.Fill
    17.             .FullRowSelect = True
    18.             .GridLines = True
    19.             .View = System.Windows.Forms.View.Details
    20.         End With
    21.         Me.Controls.Add(lv)
    22.     End Sub
    23. End Class

    then you should be able to get to the listview using something like

    vb Code:
    1. tabpage(2).lv.add(myListViewItem)
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] insert item into a custom listview in tabpage

    While what kebo suggested will work I'd actually suggest modifying it a bit. First of all, at most you should expose the ListView via a ReadOnly field:
    vb.net Code:
    1. Public ReadOnly ListView As ListView
    That way a new ListView object cannot be assign to the field from outside, or even inside other than in the constructor. I'd consider it better to use a ReadOnly property:
    vb.net Code:
    1. Private _listView As ListView
    2.  
    3. Public ReadOnly Property ListView() As ListView
    4.     Get
    5.         Return Me._listView
    6.     End Get
    7. End Property
    That said, I would think that the only thing you'd need to access from outside the TabPage would be the Items collection of the ListView. In that case that is the only thing you should expose:
    vb.net Code:
    1. Private _listView As ListView
    2.  
    3. Public ReadOnly Property Items() As ListViewItemCollection
    4.     Get
    5.         Return Me._listView.Items
    6.     End Get
    7. End Property
    Now no aspect of the ListView can be affected from outside other than the items.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    37

    Re: [2005] insert item into a custom listview in tabpage

    thx kebo... i try ur method and and it works. anywhere thx jmcilhinney's suggestion also.
    now i have face another new problem:

    how i know which listview i am controlling? i had a timer + backgroundworker to keep updating the data.
    And if i change another tab, only the listview in selectedtab will keep updating the data?
    Last edited by kahwai1984; Sep 11th, 2007 at 03:55 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    If only the ListView on the selected tab is being updated then that's because you're only updating the ListView on the selected tab. If you want the ListView on all the tabs to update then obviously you have to update the ListView's on all the tabs. If you have N tabs then you have to run your update code N times, once For Each tab in the TabControl.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    37

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    Quote Originally Posted by jmcilhinney
    If only the ListView on the selected tab is being updated then that's because you're only updating the ListView on the selected tab. If you want the ListView on all the tabs to update then obviously you have to update the ListView's on all the tabs. If you have N tabs then you have to run your update code N times, once For Each tab in the TabControl.
    No No... i think you had misunderstood..., i just wan to update the specific listview in the selectedtab. Your method also a good idea... if i use ur method, how to update the listview? how do i identify that the listview is the listview in that tab? Because the listview i update not during creating time, but in a backgroundworker event...

    THE MOST IMPORTANT IS HOW I GET THE LISTVIEW OBJECCT
    if can get the object i maybe will have idea on the next step.
    Sorry that i am new to VB.net. Hope someone can help me
    Last edited by kahwai1984; Sep 11th, 2007 at 06:40 AM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    The SelectedTab is the TabPage that's selected. If you want the ListView on the selected TabPage then you get the SelectedTab first, then get the ListView on that page. Assuming that you chose to use the second code snippet post #3 that would look like this:
    vb.net Code:
    1. Dim selectedListView As ListView = DirectCast(myTabControl.SelectedTab, TabPageEx).ListView
    where TabPageEx is your extended TabPage class that displays its own ListView.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    37

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    As i not quite sure about second code snippet post #3, so i had use kobe method on the post #2, with your solution:
    vb.net Code:
    1. Dim selectedListView As ListView = DirectCast(myTabControl.SelectedTab, TabPageEx).ListView
    but if i put this in background worker will have a cross-thread problem, and if i invoke it, the whole system will slow responds... below are the code:

    vb.net Code:
    1. Private Sub StreamStockDataWorker_DoWork(ByVal sender As System.Object, _
    2.     ByVal e As System.ComponentModel.DoWorkEventArgs) Handles StreamStockDataWorker.DoWork
    3.        If TabControl1.InvokeRequired = True Then
    4.             Dim Invoker As New MethodInvoker(AddressOf SelectTab)
    5.            TabControl1.Invoke(Invoker)
    6.         End If
    7.     End Sub
    8.  
    9.     Private Sub SelectTab()
    10.         Dim selectedListView As ListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
    11.         Dim i As Integer
    12.         Dim EmptyData As Integer
    13.         Dim stockdata As StockData
    14.         Dim datastock As String()
    15.         For i = 0 To selectedListView.Items.Count - 1
    16.             datastock = client1.DownloadString("link" & selectedListView.Items(i).Text).Split("|")
    17.             stockdata.name = datastock(3)
    18.             stockdata.open = datastock(6)
    19.             With selectedListView.Items(i).SubItems
    20.                    .Add(stockdata.name)
    21.                    .Add(stockdata.open)
    22.             End With
    23.         Next
    24.      End Sub

    any solution for this? if can, it is better not for me to put invoke... anyway to put
    vb.net Code:
    1. Dim selectedListView As ListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
    in backgroundworker and does not have cross-thread problem?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    There's no need for you to be marshaling back to the UI thread at all. You can get all the information you need before you start the BackgroundWorker, do all the work in the background thread and collect the results, then update the UI after the worker completes:
    vb.net Code:
    1. Private Sub InitiateBackgroundTask()
    2.     'This can be any object you want, so it can contain any and as much data as you like.
    3.     Dim argument As Object
    4.  
    5.     'Populate argument here.
    6.  
    7.     Me.BackgroundWorker1.RunWorkerAsync(argument)
    8. End Sub
    9.  
    10.  
    11. Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
    12.                                      ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    13.     'Get the starting data back here.
    14.     Dim argument As Object = e.Argument
    15.  
    16.     'This can be any object you want, so it can contain any and as much data as you like.
    17.     Dim result As Object
    18.  
    19.     'Use argument and populate result here.
    20.  
    21.     e.Result = result
    22. End Sub
    23.  
    24. Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
    25.                                                  ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    26.     'Get the result data back here.
    27.     Dim result As Object = e.Result
    28.  
    29.     'Use result here.
    30. End Sub
    So you see, you get all the data you need from the ListView BEFORE you ever start the background thread and you don't update the UI until AFTER the background thread has completed, so there's never any delegation required. If you really want to update the UI as you go then you can call the BGW's ReportProgress method and update the UI in the ProgressChanged event handler. See here for more info.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    37

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    i think i need to explain what am i trying to do:
    i am doing a stock quote monitoring function.
    When form load, there will be a list of data from database display to a listbox. Then when user double click the item, a tabpage will be generated and a list of data will also generated into the listview. Base on the item in the listview, data will be downloaded and added into listview.subitem. i plan to do all this in background. The first problem i face was cross thread problem like below code:

    vb Code:
    1. Private Sub InitiateBackgroundTask()
    2.         Dim selectedListView As ListView
    3.  
    4.         selectedListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
    5.  
    6.         StreamStockDataWorker.RunWorkerAsync(selectedListView)
    7.     End Sub
    8.  
    9.     Private Sub StreamStockDataWorker_DoWork(ByVal sender As System.Object, _
    10.     ByVal e As System.ComponentModel.DoWorkEventArgs) Handles StreamStockDataWorker.DoWork
    11.         If TabControl1.TabPages.Count = 0 Then
    12.         Else
    13.             Dim i As Integer
    14.             Dim WholeListview As ListView = e.Argument
    15.             Dim EmptyData As Integer
    16.             Dim stockdata As StockData
    17.             Dim datastock As String()
    18.             For i = 0 To WholeListview.Items.Count - 1
    19.                 Dim abc As String
    20.                 abc = WholeListview.Items(i).Text        <-----cross-thread error
    21.                 datastock = datastock = client1.DownloadString("link" & selectedListView.Items(i).Text).Split("|")
    22.             stockdata.name = datastock(3)
    23.             stockdata.open = datastock(6)
    24.             With selectedListView.Items(i).SubItems
    25.                    .Add(stockdata.name)
    26.                    .Add(stockdata.open)
    27.             End With
    28.             Next
    29.         End If
    30.     End Sub

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    I have already explained what you need to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    37

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    I had try another way like this:
    vb Code:
    1. Private Sub InitiateBackgroundTask()
    2.         Dim selectedListView As ListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
    3.  
    4. For i = 0 To selectedListView.Items.Count - 1
    5.         Dim name As String = selectedListView.Items(i).Text
    6.         StreamStockDataWorker.RunWorkerAsync(name)
    7. Next
    8.  
    9. Private Sub StreamStockDataWorker_DoWork(ByVal sender As System.Object, _
    10.     ByVal e As System.ComponentModel.DoWorkEventArgs) Handles StreamStockDataWorker.DoWork
    11.         Dim name As String = e.Argument
    12.         Dim datastock As String()
    13.         If TabControl1.TabPages.Count = 0 Then
    14.  
    15.         Else
    16.             datastock = client1.DownloadString("link" & name.Tostring).Split("|")
    17.         End If
    18.     End Sub

    but obviously, this will occured "backgroundworker cannot handle concurren task" problem...

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    Have I got any loop in my InitiateBackgroundTask method? No, I don't. What did I say in my last post?
    Quote Originally Posted by jmcilhinney
    you get all the data you need from the ListView BEFORE you ever start the background thread
    If you have 100 items in your ListView and you need a value from each of them then you get a avlue from each item first, THEN you start the background thread and pass in ALL the data. You process ALL the data in your DoWork event handler, collecting ALL the data you need to ouput and passing it ALL out. Finally you update the UI with ALL the data you passed out at the end of the DoWork event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    37

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    thanks jmcilhinney. Now i had another problem base on the listview above. How do i handle double click event for a specific listview?

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] insert item into a custom listview in tabpage

    Quote Originally Posted by kahwai1984
    thanks jmcilhinney. Now i had another problem base on the listview above. How do i handle double click event for a specific listview?
    This is a completely different topic and should be discussed in its own thread. One thread per topic and one topic per thread please.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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