.NETGURU
Adding Items to ListView through another form
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vb.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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

Reply to this message...
 
    
Imran Koradia (VIP)
[Original message clipped]

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.

> Is it at all possible?

Reply to this message...
 
    
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

"Imran Koradia" <Click here to reveal e-mail address> wrote in message
news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Imran Koradia (VIP)
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.

"Ash Phillips" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Imran Koradia (VIP)
> 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.

Reply to this message...
 
    
Tom Shelton
On Sun, 12 Sep 2004 12:42:00 +0100, Ash Phillips wrote:

[Original message clipped]

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
--
Tom Shelton [MVP]
Reply to this message...
 
    
Supra
i still have same problem. but i'm using irc chat similar to mirc

Ash Phillips wrote:

[Original message clipped]

Reply to this message...
 
 
System.EventArgs
System.Object
System.Web.UI.WebControls.ListItem
System.Windows.Forms.DialogResult
System.Windows.Forms.ListView
System.Windows.Forms.ListViewItem




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification