Listview struggles

durilai

Member
Joined
Oct 17, 2005
Messages
16
Programming Experience
1-3
I have a simple form that has a listview that is populated by a table in access. There is a button that opens another form using showdialog so that the first form stays in the background. The second form is used to add a value to that same access table. When I click the add button I want the value to be added, the current form to close and the new value to show in the listview. Everything works except the new value does not show, if I stop and then start the project the value appears. So I just need to refresh the listview which is being populated on formload. I have tried to use the refresh and update command, but to no avail. Any help is greatly appreciated.

Thanks
 
Hey,

I'm not entirely sure of the situation so I'm going to mention some things that may seem patronising!! Don't be offended if this is the case. Anything in the form load event will only be run when the form is first loaded, that is, until it's closed and opened again. When I refresh a listview I usually have a seperate sub say RefreshListview() then call this from form load. I'd then call the same sub again (Make sure the Sub begins with ListView1.Items.Clear) after your input dialog form has closed.

Hope this helps. (Say if I'm going down the wrong track!)
 
Thank you for your input I am going to paste my code here, if you could see whats wrong.


#Region " .... dbconn .... "
Dim strSQL As String
Dim con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "/TEST.mdb; Persist Security Info=False;"
Dim oledbcon As New OleDbConnection(con)
Dim cmd As OleDbCommand
Dim objRead As OleDbDataReader
Dim lvItem As ListViewItem = New ListViewItem
#
End Region

#Region " .... getdata .... "
Private Sub fetching()
Me.ListView1.Items.Clear()
Try
strSQL = "SELECT * FROM class"
cmd =
New OleDbCommand(strSQL, oledbcon)
objRead = cmd.ExecuteReader
While objRead.Read
lvItem =
Me.ListView1.Items.Add(objRead("ClassID") & "")
lvItem.SubItems.Add(objRead("ClassDescription") & "")
lvItem.SubItems.Add(objRead("ClassDate") & "")
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
#End Region

#Region " .... load event .... "
Public Sub frmClass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oledbcon.Open()
fetching()
oledbcon.Close()
End Sub
#End Region

Public Sub btnAddClass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddClass.Click
Dim frmPOP As New frmAddClass
frmPOP.ShowDialog()
End Sub
Public Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Public Sub btnViewRoster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewRoster.Click
If ListView1.SelectedIndices.Count = 0 Then
MsgBox("Please Select a Class Before Moving On.", MsgBoxStyle.OKOnly, "Select Class")
Else
Dim classvalue As Long = Me.ListView1.SelectedItems(0).Text
Dim frmRos As New frmRoster
frmRos.ShowDialog()
End If
End Sub

I am also trying to pass the data in "classvalue" to a new form, if you have any insight on that it would be very much appreciated.

Thanks alot
 
Hey again,

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] Sub[/COLOR][/SIZE][SIZE=2] btnAddClass_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnAddClass.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] frmPOP [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] New[/COLOR][/SIZE][SIZE=2] frmAddClass

If frmPOP.ShowDialog() = DialogResult.OK then
[/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2] oledbcon.Open()[/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2]fetching()
[/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2] oledbcon.Close()[/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2]End If

[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] Sub[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE]

Of course make sure that frmAddClass closes with Me.DialogResult = DialogResult.OK

I'd Also suggest you open and close the db connection inside the fetching sub for ease of use.

If you want to pass a value to another form, the simplest way is to make it a public property on the new form ie.

In new form
VB.NET:
dim ClassValue as integer '(Or whichever datatype you're using)
Public Property MyClassValue() As Integer
        Get
           return ClassValue
        End Get
        Set(ByVal Value As Integer)
           ClassValue = Value
        End Set
    End Property

You can then use this ClassValue inside the new form and set it from outside. So in your main form you would do:

VB.NET:
Dim frmNewForm as New NewFormType
frmNewForm.MyClassValue = 10
frmNewForm.ShowDialog(Me)

Hope this helps..
 
Thanks for the help the pass value item worked, i cant believe I was having such a problem with that. I guess the first thing I need to do is to not relate VB 6.0 to VB.NET. As for the refresh a listview, I am not sure if that is what I m looking for. I need the listview to be refreshed when the "frmAddClass.btnAddClass" is clicked. I think the problem is because the "fetching" is done on form load, and it cannot be reloaded. I would like to keep it that way, but if not I am open to suggestions.

Thanks again
 
Hey,

Glad you got the variable passing sorted. With the listview, because you are populating it yourself to begin with you will have to populate it every time you need to refresh, which sucks I know but I do this a in lot of apps. Unfortunately the built in listview doesn't have great support for databinding which would solve your problem. You could look into using the Virtual Listview (google around, can't remember where to get it) or god forbid a datagrid, though my utter hatred for the built in datagrid always deters me.
 
Hang on a sec!!! Just wondering are you saying that you want the Add Class form to stay open and still have the Main Forms listview refresh whien the add class button is pressed?, in which case you can do that by setting up a public event in the Add Class form. ie

VB.NET:
'in AddClass 
Public event NewClassAdded()

'in btnAddClass click event
RaiseEvent NewClassAdded

'Then in your main form when you declare the AddClass form:-

AddHandler frmAddClass.NewClassAdded, AddressOf fetching
'but again make sure you open and close the connection at the start and end of the fetching sub respectively.

Still not sure if this is what you're after, just an idea..
 
That refresh issue worked very well, except in one instance. I have created a txtfield that can be submitted on clicking enter or when a button is pushed, then a form pops up with a listview populated of all the name that match the txtField. I then can select one row, and click another button that adds that record to the firstlist view. I tried the method that was posted above and it worked on a form that pops up another form with a text field that adds that text to a table and it repopulated my listview fine. I would assume it has something to do with the listview, but I am not sure so any help would be great.

Thanks
 
Back
Top