|
-
Sep 8th, 2007, 03:16 PM
#1
Thread Starter
Member
[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.
-
Sep 8th, 2007, 04:01 PM
#2
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:
Public Class TabPageEx
Inherits TabPage
Public lv as ListView 'define the list view member here
Public Sub New()
lv = New ListView 'then instance the listview member here
Dim clmcode As New ColumnHeader
Dim symbol As New ColumnHeader
symbol.Text = "Symbol"
clmcode.Text = "Code"
With lv
.Name = "hello" <--- the name did not display
.BorderStyle = System.Windows.Forms.BorderStyle.None
.MultiSelect = True
.Columns.AddRange(New ColumnHeader() {clmcode, symbol})
.Dock = System.Windows.Forms.DockStyle.Fill
.FullRowSelect = True
.GridLines = True
.View = System.Windows.Forms.View.Details
End With
Me.Controls.Add(lv)
End Sub
End Class
then you should be able to get to the listview using something like
vb Code:
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
-
Sep 8th, 2007, 08:25 PM
#3
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:
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:
Private _listView As ListView
Public ReadOnly Property ListView() As ListView
Get
Return Me._listView
End Get
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:
Private _listView As ListView
Public ReadOnly Property Items() As ListViewItemCollection
Get
Return Me._listView.Items
End Get
End Property
Now no aspect of the ListView can be affected from outside other than the items.
-
Sep 11th, 2007, 03:42 AM
#4
Thread Starter
Member
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.
-
Sep 11th, 2007, 06:11 AM
#5
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.
-
Sep 11th, 2007, 06:33 AM
#6
Thread Starter
Member
Re: [RESOLVED] [2005] insert item into a custom listview in tabpage
 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.
-
Sep 11th, 2007, 07:17 AM
#7
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:
Dim selectedListView As ListView = DirectCast(myTabControl.SelectedTab, TabPageEx).ListView
where TabPageEx is your extended TabPage class that displays its own ListView.
-
Sep 12th, 2007, 01:11 AM
#8
Thread Starter
Member
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:
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:
Private Sub StreamStockDataWorker_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles StreamStockDataWorker.DoWork
If TabControl1.InvokeRequired = True Then
Dim Invoker As New MethodInvoker(AddressOf SelectTab)
TabControl1.Invoke(Invoker)
End If
End Sub
Private Sub SelectTab()
Dim selectedListView As ListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
Dim i As Integer
Dim EmptyData As Integer
Dim stockdata As StockData
Dim datastock As String()
For i = 0 To selectedListView.Items.Count - 1
datastock = client1.DownloadString("link" & selectedListView.Items(i).Text).Split("|")
stockdata.name = datastock(3)
stockdata.open = datastock(6)
With selectedListView.Items(i).SubItems
.Add(stockdata.name)
.Add(stockdata.open)
End With
Next
End Sub
any solution for this? if can, it is better not for me to put invoke... anyway to put
vb.net Code:
Dim selectedListView As ListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
in backgroundworker and does not have cross-thread problem?
-
Sep 12th, 2007, 01:51 AM
#9
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:
Private Sub InitiateBackgroundTask()
'This can be any object you want, so it can contain any and as much data as you like.
Dim argument As Object
'Populate argument here.
Me.BackgroundWorker1.RunWorkerAsync(argument)
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Get the starting data back here.
Dim argument As Object = e.Argument
'This can be any object you want, so it can contain any and as much data as you like.
Dim result As Object
'Use argument and populate result here.
e.Result = result
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
'Get the result data back here.
Dim result As Object = e.Result
'Use result here.
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.
-
Sep 12th, 2007, 05:36 AM
#10
Thread Starter
Member
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:
Private Sub InitiateBackgroundTask()
Dim selectedListView As ListView
selectedListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
StreamStockDataWorker.RunWorkerAsync(selectedListView)
End Sub
Private Sub StreamStockDataWorker_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles StreamStockDataWorker.DoWork
If TabControl1.TabPages.Count = 0 Then
Else
Dim i As Integer
Dim WholeListview As ListView = e.Argument
Dim EmptyData As Integer
Dim stockdata As StockData
Dim datastock As String()
For i = 0 To WholeListview.Items.Count - 1
Dim abc As String
abc = WholeListview.Items(i).Text <-----cross-thread error
datastock = datastock = client1.DownloadString("link" & selectedListView.Items(i).Text).Split("|")
stockdata.name = datastock(3)
stockdata.open = datastock(6)
With selectedListView.Items(i).SubItems
.Add(stockdata.name)
.Add(stockdata.open)
End With
Next
End If
End Sub
-
Sep 12th, 2007, 05:38 AM
#11
Re: [RESOLVED] [2005] insert item into a custom listview in tabpage
I have already explained what you need to do.
-
Sep 12th, 2007, 05:56 AM
#12
Thread Starter
Member
Re: [RESOLVED] [2005] insert item into a custom listview in tabpage
I had try another way like this:
vb Code:
Private Sub InitiateBackgroundTask()
Dim selectedListView As ListView = DirectCast(TabControl1.SelectedTab, TabPageEx).lv
For i = 0 To selectedListView.Items.Count - 1
Dim name As String = selectedListView.Items(i).Text
StreamStockDataWorker.RunWorkerAsync(name)
Next
Private Sub StreamStockDataWorker_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles StreamStockDataWorker.DoWork
Dim name As String = e.Argument
Dim datastock As String()
If TabControl1.TabPages.Count = 0 Then
Else
datastock = client1.DownloadString("link" & name.Tostring).Split("|")
End If
End Sub
but obviously, this will occured "backgroundworker cannot handle concurren task" problem...
-
Sep 12th, 2007, 08:14 AM
#13
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?
 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.
-
Sep 18th, 2007, 12:04 PM
#14
Thread Starter
Member
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?
-
Sep 18th, 2007, 05:57 PM
#15
Re: [RESOLVED] [2005] insert item into a custom listview in tabpage
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|