|
-
Jun 13th, 2002, 03:45 PM
#1
-
Jun 13th, 2002, 04:58 PM
#2
Hyperactive Member
The best way to do this imho is to send a reference to the listview to your frmAdd form. Do this by changing the Public Sub New part of the frmAdd form:-
Public Sub New(ByRef lv As ListView)
This sub is the first procedure in the Windows Form Designer Generated code.
Now from your form with the list view you do the following in the procedure to open the frmAdd form:-
dim frm as new frmAdd
frm.Show(ListView1)
This assumes your listview is called ListView1 so you would change that to the correct name.
Then when the user clicks the o.k. button on the frmAdd form you just use the passed reference:-
lv.Items.Add "New Item"
....
..
.
etc.
Should work but I haven't tried it - thats up to you
-
Jun 13th, 2002, 05:04 PM
#3
I agree, I have been passing references as arguments to the constructor of the class for a while now. Works great. Plus, if you use the same form for different things, you can overload the constructor and allow yourself to pass it what you want depending on what you want the form/class to do.
-
Jun 13th, 2002, 09:45 PM
#4
Wow! never thought of editing the default constructor!!! tnx guys 
Edit:
Doesnt work 
VB Code:
Private newItems() As DB.Item
Public Sub New(ByRef newItems() As DB.Item)
' ...
Me.newItems = newItems
End Sub
when the form is closed, the newItems array that was sent as ByRef isnt changed. the newItems() is changed before closing this form, but when it is closed it will be set to Nothing
I'm passing it as ByRef!!! why doesnt it work?
NOTE: DB is a class and DB.Item is a Structure inside it
Last edited by MrPolite; Jun 13th, 2002 at 10:01 PM.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 14th, 2002, 08:06 AM
#5
you are using the same variable name as a decalred private and a parameter. 20 bucks thats the problem.
-
Jun 14th, 2002, 03:47 PM
#6
Originally posted by Cander
you are using the same variable name as a decalred private and a parameter. 20 bucks thats the problem.
give me 20 bucks that's not a problem at all becuase I'm useing the ME keyword to access the private variable thingie. Just to make sure, I changed the variable name, but that didnt change anything.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 14th, 2002, 04:13 PM
#7
Addicted Member
You could try setting the objects through a public property instead of a constructor, but that shouldn't make a difference.
It also shouldn't make a difference if you pass it ByVal or ByRef.
Post more code. I don't think the problem is with anything you have posted so far.
That which does not kill us, only makes us stronger. 
-
Jun 14th, 2002, 09:01 PM
#8
My fault, but still..
OH WELL!!! Maybe I'm still confusing C++ and VB together 
When the form starts, the object IS equal to nothing. Let's change the example this way:
VB Code:
Private newItems() As DB.Item
Public Sub New(ByRef SaveValues() As DB.Item)
' I wish this could work like a reference
saveValues = newItems
End Sub
Private doStuff()
' Suppose that newItems() is chaning to another array
newItems = .......
End Sub
I am changing newItems(), so it wont be equal to NOTHING. What I want is that whenever newItems changes, the saveValues array would change to the same value as newItems. And when the form is closing, the saveValues array would be keep alive (because it's declared on the main form)Exactly like setting a reference to a variable in C++.
kinda confusing
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 14th, 2002, 09:31 PM
#9
Ok, if I understand you correctly, I think I have your answer. Follow these steps.
Create two forms: form1 and form2
On form1, add a button and a label with default names. In the buttons click event, add this code:
Code:
Dim frmTemp As Form2
frmTemp = New Form2(Me)
frmTemp.ShowDialog()
Also add a user created sub to that form1 class like this:
Code:
Public Sub MySub()
Label1.BackColor = System.Drawing.Color.AliceBlue
End Sub
Now, on form2, add just one button, keeping the default name.
Here is where things get a little tricky, so follow closely...
First, edit the constructor to take a object variable into it and assign it to a variable we will add in next:
Code:
Public Sub New(ByVal sender As System.Object)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
original = sender
End Sub
Now, add a private "CLASS" variable to the form2 class like so (I am showing you two statements, the first is put there by VS, the second is mine. I just wanted to show you where I put it)
Code:
Private components As System.ComponentModel.IContainer
'This one is the one I added:
Private original As System.Object
Now, in the buttons click event, put this code:
Code:
' This will call the MySub on form 1, which created form2.
Dim frmTemp As Form1
frmTemp = DirectCast(original, Form1)
frmTemp.MySub()
Now, when you start and run the program, form one will show up. You push the button to bring up form2. When you push form2's button, it will call a public sub from form1. If you watch, you will see the labels color change to prove it works.
-
Jun 14th, 2002, 09:33 PM
#10
And, just in case I wasn't clear on anything, here is all the code for both forms, starting with form1.
Code:
'Form1's code:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(72, 128)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(32, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(160, 23)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frmTemp As Form2
frmTemp = New Form2(Me)
frmTemp.ShowDialog()
End Sub
Public Sub MySub()
Label1.BackColor = System.Drawing.Color.AliceBlue
End Sub
End Class
Code:
'Form2's code:
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New(ByVal sender As System.Object)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
original = sender
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Private original As System.Object
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(88, 104)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' This will call the MySub on form 1, which created form2.
Dim frmTemp As Form1
frmTemp = DirectCast(original, Form1)
frmTemp.MySub()
End Sub
End Class
-
Jun 14th, 2002, 09:41 PM
#11
tried your fist post, workz perfect!!! tnx 
EDITED:
I'm just wondering why do you send the form as system.Object? I tried the same thing and I also tried to send the form like this
VB Code:
Private Original As Form1
Public Sub New(ByVal frmOriginal As Form1)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
original = frmOriginal
End Sub
this works too, but why would you use system.object and then cast from it? is that any better?
Last edited by MrPolite; Jun 14th, 2002 at 09:44 PM.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 14th, 2002, 10:48 PM
#12
The reason I did the cast is only a matter of preference thing. I was needing to pass in different object types to the constructor, so I ended up using System.Object. Your way will work, as long as form1 is the only one creating that form2.
Glad it worked for you.
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
|