Adding Items to ListView through another form

A

Ash Phillips

Hi Everyone,

I have this program I wrote in VB6 for family use. It's a DVD Database just
for me to keep track of them cause I have so many lol.

In VB6, I could add items to the ListView in 'frmMain' from 'frmAdd' with
the following code:

Private Function AddEntry(Title As String, Rating As String, Genre As
String, OnLoan As Boolean, ToWho As String)
Dim x As ListItem

Set x = frmMain.lvwDVD.ListItems.Add(, , Title, , 1)
x.SubItems(1) = Rating
x.SubItems(2) = Genre
If OnLoan = True Then
x.SubItems(3) = "Yes"
Else
x.SubItems(3) = "No"
End If
x.SubItems(4) = ToWho

frmMain.lvwDVD.SelectedItem = x

End Function

The ListView I use in VB6 is from COMCTL.OCX

Now in VB .NET I have the following:

Private Function AddEntry(ByVal Title As String, ByVal Rating As String,
ByVal Genre As String, ByVal OnLoan As Boolean, ByVal ToWho As String)
Dim frmMain As New frmMain
Dim X As ListViewItem

X = frmmain.lvwDVD.Items.Add(Title)
X.SubItems(1).Text = Rating
X.SubItems(2).Text = Genre
If OnLoan = True Then
X.SubItems(3).Text = "Yes"
Else
X.SubItems(3).Text = "No"
End If
X.SubItems(4).Text = ToWho

End Function

The ListView I use in VB .NET is the System.Windows.Forms.ListView (I
believe)

In VB6, 'frmAdd' would be shown modal above 'frmMain' and the function would
add the item succesfully to the ListView.

For the life of me I can't make this happen in VB .NET :(

Is it at all possible?

Thanks in advance,
Ash
 
I

Imran Koradia

Private Function AddEntry(ByVal Title As String, ByVal Rating As String,
ByVal Genre As String, ByVal OnLoan As Boolean, ByVal ToWho As String)
Dim frmMain As New frmMain
For the life of me I can't make this happen in VB .NET :(

That's because in the above line of code you are creating a new instance of
frmMain and you're adding the listview item to this new form - not to the
instance of frmMain that you have opened already. What you need is a
reference to the original frmMain that is opened. There are several ways you
can do that. One way is you can pass a reference to frmMain to frmAdd
through frmAdd's constructor. Either use the same constructor or just add
another one which takes in a reference to frmMain. Here's some code:

' This is in frmMain
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Here we pass the frmMain reference to frmAdd
Dim ofrmAdd As New frmAdd(Me)
ofrmAdd.ShowDialog()
End Sub


' This is in frmAdd
Private ofrmMain As frmMain

' Either change the exisiting Sub New or just add this one.
' Both ways will work just fine.
Public Sub New(ByRef oMain As Form1)
MyBase.New()
InitializeComponent()
ofrmMain = oMain
End Sub

' Now add your listview items using this instance of frmMain
Private Function AddEntry(ByVal Title As String, _
ByVal Rating As String, ByVal Genre As String, _
ByVal OnLoan As Boolean, ByVal ToWho As String)

Dim X As ListViewItem

X = ofrmMain.lvwDVD.Items.Add(Title)
X.SubItems(1).Text = Rating
X.SubItems(2).Text = Genre
If OnLoan = True Then
X.SubItems(3).Text = "Yes"
Else
X.SubItems(3).Text = "No"
End If
X.SubItems(4).Text = ToWho

End Function

I hope that helps a bit..
Imran.
 
I

Imran Koradia

Public Sub New(ByRef oMain As Form1)

Sorry - the above line should be:
Public Sub New(ByRef oMain As frmMain)

you'd have figured that out though :)

Imran.
 
A

Ash Phillips

Hi Imran, thanks for your help.

I have done all that you said, and triple checked it lol

When I press the Add button in frmAdd, it still brings up a new instance of
frmMain but for some reason the Item hasnt even been added there?

Any other suggestions?

Ash
 
I

Imran Koradia

Strange. I tried the same thing on my machine and it works fine. Can you
post the exact code that you now have? Maybe that'll help us figure out what
exactly is going on.

Imran.
 
T

Tom Shelton

Hi Everyone,

I have this program I wrote in VB6 for family use. It's a DVD Database just
for me to keep track of them cause I have so many lol.

In VB6, I could add items to the ListView in 'frmMain' from 'frmAdd' with
the following code:

Private Function AddEntry(Title As String, Rating As String, Genre As
String, OnLoan As Boolean, ToWho As String)
Dim x As ListItem

Set x = frmMain.lvwDVD.ListItems.Add(, , Title, , 1)
x.SubItems(1) = Rating
x.SubItems(2) = Genre
If OnLoan = True Then
x.SubItems(3) = "Yes"
Else
x.SubItems(3) = "No"
End If
x.SubItems(4) = ToWho

frmMain.lvwDVD.SelectedItem = x

End Function

The ListView I use in VB6 is from COMCTL.OCX

Now in VB .NET I have the following:

Private Function AddEntry(ByVal Title As String, ByVal Rating As String,
ByVal Genre As String, ByVal OnLoan As Boolean, ByVal ToWho As String)
Dim frmMain As New frmMain
Dim X As ListViewItem

X = frmmain.lvwDVD.Items.Add(Title)
X.SubItems(1).Text = Rating
X.SubItems(2).Text = Genre
If OnLoan = True Then
X.SubItems(3).Text = "Yes"
Else
X.SubItems(3).Text = "No"
End If
X.SubItems(4).Text = ToWho

End Function

The ListView I use in VB .NET is the System.Windows.Forms.ListView (I
believe)

In VB6, 'frmAdd' would be shown modal above 'frmMain' and the function would
add the item succesfully to the ListView.

For the life of me I can't make this happen in VB .NET :(

Is it at all possible?

Thanks in advance,
Ash

You have a couple of choices, make a propertie on the second form that
retuns a new listviewitem that can be added to your form... You would
probably have a button that returns DialogResult.Ok - so that in the main
form you could do:

Dim dialogForm As New FormB
If dialogForm.ShowDialog (Me) = DialogResult.Ok Then
Me.ListView1.Items.Add (dialogForm.NewItem)
End If


so in the dialog form, you would just add a property that looked like
Private NewListViewItem As New ListViewItem

....

' populate the listviewitem

Public ReadOnly Property NewItem As ListViewItem
Get
Return Me.NewListViewItem
End Get
End Property

Or, you could just have a second constructor on your dialog form that takes
a reference to the listview control that you want to add to...

Sub New (ByVal listview1 as ListView)
me.listview1 = listview1
end sub

' populate the list view..

HTH
 
A

Ash Phillips

Sorry for the delayed response

In frmAdd, I have:
Public Sub New(ByRef oMain As frmMain)

MyBase.New()

InitializeComponent()

ofrmMain = oMain

End Sub

------------------

Private Function AddEntry(ByVal Title As String, ByVal Rating As String,
ByVal Genre As String, ByVal OnLoan As Boolean, ByVal ToWho As String)

Dim frmMain As New frmMain

Dim X As ListViewItem

X = frmMain.lvwDVD.Items.Add(Title)

X.SubItems(1).Text = Rating

X.SubItems(2).Text = Genre

If OnLoan = True Then

X.SubItems(3).Text = "Yes"

Else

X.SubItems(3).Text = "No"

End If

X.SubItems(4).Text = ToWho

End Function

------------------------

Private Sub butAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butAdd.Click

On Error Resume Next

AddEntry(txtTitle.Text, cboRating.Text, cboGenre.Text, optYes.Checked,
txtLoan.Text)



End Sub

In frmMain I have liek you told me:
 
A

Ash Phillips

Here's what I have in frmAdd:

Public Sub New(ByRef oMain As frmMain)
MyBase.New()
InitializeComponent()
ofrmMain = oMain
End Sub

Private Function AddEntry(ByVal Title As String, ByVal Rating As String,
ByVal Genre As String, ByVal OnLoan As Boolean, ByVal ToWho As String)
Dim frmMain As New frmMain
Dim X As ListViewItem

X = frmMain.lvwDVD.Items.Add(Title)
'X = frmMain.lvwDVD.ListItems.Add(, , Title, , 1)
X.SubItems(1).ForeColor = System.Drawing.Color.Red
X.SubItems(1).Text = Rating
X.SubItems(2).Text = Genre
If OnLoan = True Then
X.SubItems(3).Text = "Yes"
Else
X.SubItems(3).Text = "No"
End If
X.SubItems(4).Text = ToWho

' frmmain.lvwDVD.Items.Item =

End Function

Private Sub butAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butAdd.Click
AddEntry(txtTitle.Text, cboRating.Text, cboGenre.Text,
optYes.Checked, txtLoan.Text)
End Sub

And in frmMain I have the following like you told me:

Private Sub mnu_Add_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnu_Add.Click
Dim ofrmAdd As New frmAdd(Me)
ofrmAdd.ShowDialog()
End Sub

Sorry for the delayed response!
 
I

Imran Koradia

Private Function AddEntry(ByVal Title As String, ByVal Rating As
String,
ByVal Genre As String, ByVal OnLoan As Boolean, ByVal ToWho As String)

-----> Dim frmMain As New frmMain
This is the problem. You're still creating a new instance of frmMain and
using this instance in the code below. You should use the existing instance
of frmMain that you passed in the constructor of this form. Replace
'frmMain' wherever its being used in the code below with 'ofrmMain' since
that is the existing instance of your main form. You should be fine then.
Dim X As ListViewItem

X = frmMain.lvwDVD.Items.Add(Title)
'X = frmMain.lvwDVD.ListItems.Add(, , Title, , 1)
X.SubItems(1).ForeColor = System.Drawing.Color.Red
X.SubItems(1).Text = Rating
X.SubItems(2).Text = Genre
If OnLoan = True Then
X.SubItems(3).Text = "Yes"
Else
X.SubItems(3).Text = "No"
End If
X.SubItems(4).Text = ToWho

' frmmain.lvwDVD.Items.Item =

End Function

Sorry for the delayed response!
Not a problem :) Actually, I'm a little late too ;-)

hope that helps..
Imran.
 
A

Ash Phillips

THanks for the reply - I am getting somewhere now!!!

However in my AddEntry function I get the error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid
values.

and it highlights the line:

X.SubItems(1).Text = Rating

Any ideas?

Thanks again,
Ash
 
I

Imran Koradia

and it highlights the line:
X.SubItems(1).Text = Rating

you haven't added the subitem yet which is why you get the error. You need
to do SubItems.Add just like you did Items.Add. This is how your code should
look like:
(watch for typos - i'm writing the code directly in this message)

Private Function AddEntry(ByVal Title As String, ByVal Rating As String,
ByVal Genre As String, ByVal OnLoan As Boolean, ByVal ToWho As String)
Dim X As ListViewItem

X = ofrmMain.lvwDVD.Items.Add(Title)
With X
With .SubItems.Add(Rating)
.ForeColor = System.Drawing.Color.Red
End With
.SubItems.Add(Genre)
If OnLoan Then
.SubItems.Add("Yes")
Else
.SubItems.Add("No")
End If
.SubItems.Add(ToWho)
End With

End Function

That should pretty much solve the error you mentioned. Ofcourse, once you've
added the subitems, you can reference them with the index - for instance,
the Rating subitem with index 1 and so on..

hope that helps..
Imran.
 
A

Ash Phillips

ahhh thank you very much - I will try it now and let you know how it goes!!

I saw the add property but didnt think that'd be it - so used to VB6 :'(

Ash
 
G

Guest

I saw the add property but didnt think that'd be it - so used to VB6 :'(
...same here. But you'll get used to it to the extent that you'll never
want to go back to VB6 since VB .NET is so much better. And VB 2005 is
even better :)

Imran.
 
G

Guest

hmmmmmm the forecolor doesnt work on the subitem for me... does it work for
you?

set the 'UseItemStyleForSubItems' of the ListItem to false before adding any
of the subitems.

With X
' add this so that style of the listitem is NOT
' used for all the subitems.
.UseItemStyleForSubItems = False
With .SubItems.Add(Rating)
.ForeColor = System.Drawing.Color.Red
End With
.SubItems.Add(Genre)
If OnLoan Then
.SubItems.Add("Yes")
Else
.SubItems.Add("No")
End If
.SubItems.Add(ToWho)
End With
 
A

Ash Phillips

Thanks - that worked great!

Ash

Imran Koradia said:
set the 'UseItemStyleForSubItems' of the ListItem to false before adding any
of the subitems.

With X
' add this so that style of the listitem is NOT
' used for all the subitems.
.UseItemStyleForSubItems = False
With .SubItems.Add(Rating)
.ForeColor = System.Drawing.Color.Red
End With
.SubItems.Add(Genre)
If OnLoan Then
.SubItems.Add("Yes")
Else
.SubItems.Add("No")
End If
.SubItems.Add(ToWho)
End With
 
A

Ash Phillips

Ok, I have another problem :(

With ofrmMain.lvwDVD.Items
For Y = 0 To .Count

If UCase(.Item(Y).Text) = UCase(txtTitle.Text) Then

MsgBox("The item '" & txtTitle.Text & "' is already added to the Database."
& vbCrLf & "To allow Duplicate Entries to the Database, please go to
Options.", vbCritical + vbOKOnly, "Database - Duplicate Entry")

txtTitle.Focus()

txtTitle.SelectionStart = 0

txtTitle.SelectionLength = Len(txtTitle.Text)

Exit Sub

End If

Next Y

End With

Basically, I want it to search through all items in the listview to check
for duplicate entries, but for some reason it higlights the line

If UCase(.Item(Y).Text) = UCase(txtTitle.Text) Then

And gives the following error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid
values.


Ash
 
I

Imran Koradia

hey Ash,

Sorry for the late reply - I thought that was the end of this thread :)

Anyway - you need to have your counter from 0 to .Count - 1 because although
the number of items is .Count, the index ranges from 0 to .Count - 1.

hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top