通过另一个表单向ListView添加项目 [英] Adding Items to ListView through another form

查看:55
本文介绍了通过另一个表单向ListView添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有这个程序,我在VB6中写了供家庭使用。这是一个DVD数据库只是

让我跟踪它们因为我有这么多大声笑。


在VB6中,我可以添加项目到列表视图''frmMain''来自''frmAdd''和

以下代码:


私有函数AddEntry(标题为字符串,等级为字符串,类型为

String,OnLoan As Boolean,ToWho As String)

Dim x As ListItem


设置x = frmMain.lvwDVD .ListItems.Add(,, Title ,, 1)

x.SubItems(1)=评级

x.SubItems(2)=类型

如果OnLoan = True则

x.SubItems(3)=" Yes"

Else

x.SubItems(3)= 否

结束如果

x.SubItems(4)= ToWho


frmMain.lvwDVD.SelectedItem = x


结束功能


我在VB6中使用的ListView来自COMCTL.OCX


现在进入VB .NET我有以下内容:


私有函数AddEntry(ByVal Title As String, ByVal评级为字符串,

ByVal类型为字符串,ByVal OnLoan为布尔,ByVal ToWho为字符串)

Dim frmMain为新frmMain

Dim X作为ListViewItem


X = frmmain.lvwDVD.Items.Add(标题)

X.SubItems(1).Text =评级

X.SubItems(2).Text = Genre

如果OnLoan = True那么

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

Else

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

结束如果

X.SubItems(4 ).Text = ToWho


结束函数

我在VB .NET中使用的ListView是System.Windows.Forms.ListView(I

相信)


在VB6中,''frmAdd''将在''frmMain''上方显示模态,并且函数将

成功地将项目添加到ListView。


对于我的生活,我不能在VB .NET中实现这一点:(


可能吗?


提前致谢,

Ash

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

推荐答案

>私有函数AddEntry(ByVal Title As String,ByVal Rating As String,
> 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
对于我的生活,我不能在VB .NET中实现这一点:(


那是因为在上面的代码行中你创建了一个新的实例

frmMain你正在将listview项添加到这个新表单 - 而不是你已经打开过的frmMain的

实例。你需要的是一个

对打开的原始frmMain的引用。有几种方法你可以用b
$ b来传递对frmMain的引用

通过frmAdd'的构造函数。要么使用相同的构造函数,要么添加

另一个接受frmMain的引用。这里有一些代码:


''这是在frmMain

Private Sub Button1_Click(ByVal sender As System .Object,_

ByVal e As System.EventArgs)处理Button1.Click

''这里我们将frmMain引用传递给frmAdd

Dim ofrmAdd As New frmAdd(Me)

ofrmAdd.ShowDialog()

End Sub

''这是在frmAdd

私人ofrmMain作为frmMain


''要么改变现有的Sub New,要么只添加这个。

''两种方式都可以正常工作。 br />
Public Sub New(ByRef oMain As Form1)

MyBase.New()

InitializeComponent()

ofrmMain = oMain

结束子


''现在使用这个frmMain实例添加你的listview项目

私函数AddEntry(ByVal Title As字符串,_

ByVal评级为字符串,ByVal类型为字符串,_

ByVal OnLoan为布尔值,ByVal ToWho为字符串)


Dim X作为ListViewItem

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

X.SubItems(1).Text = Rating

XS ubItems(2).Text = Genre

如果OnLoan = True则

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

否则

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

结束如果

X.SubItems(4).Text = ToWho


结束功能


我希望有所帮助..

Imran。

是否可能?
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.
Is it at all possible?



> Public Sub New(ByRef oMain As Form1)


对不起 - 以上行应该是:

Public Sub New(ByRef oMain as frmMain)


你会想到这一点虽然:)


Imran。
> 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.


Hi Imran ,谢谢你的帮助。


我已经完成了你所说的一切,并且三重检查了它lol


当我按下Add按钮时frmAdd,它仍然会带来一个新的实例

frmMain但由于某种原因,物品还没有添加到那里?


还有其他任何建议吗?

Ash


" Imran Koradia" <无**** @ microsoft.com>在消息中写道

news:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
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" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
私有函数AddEntry(ByVal Title As String,ByVal Rating As String,ByVal Onre As String,ByVal OnLoan As Boolean,ByVal ToWho As String)
Dim frmMain As new 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 frmMain As New frmMain
对于我的生活,我不能在VB .NET中实现这一点:(
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



的frmMain你正在将listview项添加到这个新的表单 - 而不是你已经打开的frmMain的
实例。你是什么人need是对打开的原始frmMain的引用。有几种方法你可以用
来做。有一种方法是你可以通过frmAdd'将frmMain的引用传递给frmAdd
构造函数。要么使用相同的构造函数,要么只添加另一个引用的构造函数frmMain。这里有一些代码:

''这是在frmMain
Private Sub Button1_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs)处理Button1.Click
''这里我们将frmMain引用传递给frmAdd
Dim ofrmAdd As New frmAdd(Me)
ofrmAdd.ShowDialog()
End Sub

''这是在frmAdd
私人ofrmMain作为frmMain

''要么改变现有的Sub New,要么只是添加这个。
''两种方式都可行很好。
Public Sub New(ByRef oMain As Form1)
MyBase.New()
InitializeComponent()
ofrmMain = oMain
End Sub

''现在使用这个frmMain实例添加你的listview项目
私有函数AddEntry(ByVal Title As String,_
ByVal Rating As String,ByVal Genre As String,_
ByVal OnLoan As Boolean,ByVal ToWho As String)

Dim X作为ListViewItem

X = ofrmMain.lvwDVD.Items.Add(Title)
X.SubItems(1).Text = Rating
X.SubItems(2).Text = Genre
如果OnLoan = True那么
X.SubItems(3).Text =" Yes"
Else
X.SubItems(3).Text =" No"
结束如果 X.SubItems( 4).Text = ToWho

结束功能

我希望有点帮助..
Imran。


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.

它是否可能?




这篇关于通过另一个表单向ListView添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆