|
-
Jan 21st, 2002, 09:22 PM
#1
Thread Starter
Junior Member
How to use another forms subs
Hello - I've got a sub called addprefstocombo:
Public Sub addprefstocombo()
Dim alpayment As ArrayList = New ArrayList()
Dim s As String
'Load payment options into an array
alpayment = readtextfile("payment.ini")
'Clear out the combobox
Me.cboPayment.Items.Clear()
'Add them
For Each s In alpayment
Me.cboPayment.Items.Add(s)
Next
End Sub
This sub works perfectly when used inside its own form, but doesn't work (and completes without errors) when called from outside its form. I've tried saying:
Dim frm as New frmData
frm.addprefstocombo()
When calling it from another form but this doesn't work. It just completes, doesn't break, show errors, it just simply doesn't work. How do I call another form's subroutine? Thanks!
Reid
-
Jan 22nd, 2002, 09:49 AM
#2
I know we have had discussions before on this but I dont know if anyone got a concrete answer. But I was thinking about it and maybe you can try this(I dont have access to .NET right now to test this) Try making the Sub Friend instead of Public.
I think they made some changes to how Public works.
The only other suggestio I can make is to Inherit the form?!?!
Im not 100% up on inhertiance yet, so this is just a guess. Still learning about it.
-
Jan 23rd, 2002, 11:19 AM
#3
Hyperactive Member
Try making the Sub Friend instead of Public.
Friend/Public refer to the scope of the procedure. Friend means that it is visible only by other classes (remember a form is a class) within the same assembly, so in this example it won't make a difference.
The only other suggestio I can make is to Inherit the form
Inheriting the form will just mean that the form that does the inheriting will start off with all the controls, properties, procedures etc. of the form it has inherited. So it won't solve the problem.
ok, so so far this hasn't been very helpful to you.
You could pass into the "addprefstocombo" sub the form object where the combo control exists.
for example
VB Code:
Public Sub addprefstocombo(byref oForm as frmData)
Then to use it you would pass in "Me" if it is being excecuted from the original form, and the form object if not. Note however you will need to have access to the original form object from your second form.
Difficult to give examples without knowing more.
I hope this has helped. (I'm not very good at explaining things)
-
Jan 23rd, 2002, 11:28 AM
#4
Hyperactive Member
Actually, thinking about it. I would probably make the Combo Box the passed paramater and not the form.
When you open the second form presumably from the first (frmData) you would need to pass it the reference to the combobox. (ideally using properties).
btw - the reason it seemed to work, but didn't update anything was because it updated a combo on a new instance of the form class (frmData) This form object was never displayed. If you added a frm.show after the other code you will see what I mean.
VB Code:
Dim frm as New frmData
frm.addprefstocombo()
frm.Show
You actually need to update the original instance of the frmData class and not create a new one.
-
Jan 23rd, 2002, 12:34 PM
#5
Ahh I didnt read the orignal post correctly. Bad! Bad Cander!
-
Jan 23rd, 2002, 12:36 PM
#6
Hyperactive Member
-
Jan 23rd, 2002, 02:27 PM
#7
Thread Starter
Junior Member
I think I've tried what you suggested before I'd read your reply. Here's all the code that pertains to this section. Basically, all I'm trying to do is populate a combobox with items from a file. I want this done on a button's click event on a form other than the form of the combo box.
Here's the sub:
Public Sub addtocombo(ByVal array As ArrayList, ByVal combo As ComboBox)
Dim s As Object
'Clear out the combo
combo.Items.Clear()
'For every piece of the array
For Each s In array
'Add the piece to the items collection
combo.Items.Add(s)
Next
End Sub
And here's where I'm calling this sub. (I placed this sub in a class, the form on which this event occurs is importing this class.)
Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
'Get an array to use
Dim al As New ArrayList()
'I want to update the combo box on frmData
'First, get a new instance of frmData
Dim frm As frmData = New frmData()
'Get a new instance of my class
Dim simple As New SimpleIO()
'Then clear our cboPayment
frm.cboPayment.Items.Clear()
'Get values
al = simple.readtextfile("payment.ini")
'Run the sub
simple.addtocombo(al, frm.cboPayment)
'Close the form
Me.Close()
End Sub
-
Jan 24th, 2002, 04:55 AM
#8
Hyperactive Member
Well, the only thing you are really missing from that code is to actually show the form. I thought you were trying to display the combo on a form that was already displayed elsewhere.
These are the comments I have made to your code
VB Code:
'Get an array to use
Dim al As New ArrayList()
'I want to update the combo box on frmData
'First, get a new instance of frmData
Dim frm As frmData = New frmData()
'Get a new instance of my class
Dim simple As New SimpleIO()
'Then clear our cboPayment
'-> This is not needed, The control will already be initialised as
'-> you have only jist declared a new instance of it.
frm.cboPayment.Items.Clear()
'Get values
al = simple.readtextfile("payment.ini")
'Run the sub
simple.addtocombo(al, frm.cboPayment)
'-> Now show the form with the combo on it.
frm.ShowDialog()
'-> When it comes back here - remember to tidy up the
'-> objects used.
frm = Nothing
simple = Nothing
The only other comment I would make is that combobox is actually being passed by reference and not ByVal. As it happens you can only pass an Object ByRef anyway - so even if you specify ByVal (as you did). It will be ByRef.
It would have been better to write it as :-
VB Code:
Public Sub addtocombo(ByVal array As ArrayList, ByRef combo As ComboBox)
Although in this case I would probably have considered writting it as a function that returned a ComboBox.
For Example
VB Code:
Public Function AddToCombo(ByVal array As ArrayList) As ComboBox)
-
Jan 24th, 2002, 11:46 PM
#9
Thread Starter
Junior Member
Ok, first of all, let me thank you for your continued support in this issue. Second of all, your code work except for one thing. The items are being added to the combo box; the correct items are being added. I know this because I step through it and watch the items of the combobox as they're being added. So far, so good. Everything works. However, when I actually go to view the form they're not there! I do not understand why this is so. I can watch the correct items being added into the items of this combobox, but I can't see them. Does it matter that this form is already open when it's items are being edited? I tried running the refresh method but this didn't do the trick either. Any more suggestions?
Thanks again!
Reid V. Plumbo
[email protected]
-
Jan 25th, 2002, 04:09 AM
#10
Hyperactive Member
It sounds to me like you have more than one instance of the form with the combo box.
The code example you posted showed that you were declaring a new instance of the form, and yet you say the form was already opened. The new instance would be shown if you do a frm.showdialog where I suggested in the last message. If you do this I suspect you will see another form displayed with the combo with all the values.
However without seeing code it's difficult to guess exactly what you are trying to do.
-
Jan 25th, 2002, 09:50 AM
#11
Thread Starter
Junior Member
Ok, I understand why this doesn't work now. This understanding begs the question, how do I change a combo box's items on a form that's already open from another form that's open as well? It vb6 I could just say frmData.cboPayment.Items....and that'd do it. I was under the impression that I had to get a new instance of the form to edit its properties. It makes sense that because this form is already open, I've already got one.
So here we go:
2 open forms. One form's list box controls the other forms combobox. When I make a change to the listbox I want it reflected in the combo box.
Thanks again.
Sincerely,
Reid V. Plumbo
-
Jan 25th, 2002, 10:05 AM
#12
Hyperactive Member
ok, so you have two forms open. one with the combobox on, one without.
How are they opened. Is the one without opened by the one with? or are the both opened by a third form? are they mdi children?
-
Jan 25th, 2002, 11:57 AM
#13
Thread Starter
Junior Member
It's a pretty simple setup. I've got a main form (frmData) and a preferences form (frmPrefs). To open the prefs form, I use a Main menu. Edit->Prefs.
Private Sub mnuEditPreferences_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditPreferences.Click
Dim frm As New frmPrefs()
frm.ShowDialog(Me)
End Sub
This shows frmPrefs, my preferences form, which contains a listbox. I'd like to click a button (cmdAdd) and add each item in the listbox to the combobox (cboPayment) on the main form (frmData).
I haven't started using MDI parent-child forms yet. I did VB6 for about 8 months, and I've been using VB.NET for the past 4. I'm relatively new.
Thanks again!
Reid
-
Jan 25th, 2002, 12:15 PM
#14
Hyperactive Member
ok, this is one way to do it.
a) in the frmPrefs class you will need to set a property to pass the control.
VB Code:
Private mcboCombo As ComboBox
Public Property Combo() As ComboBox
Get
Combo = mcboCombo
End Get
Set(ByVal Value As ComboBox)
mcboCombo = Value
End Set
End Property
Then when you wish to add to the combobox, you use the private variable mcboCombo. For exxample
VB Code:
mcboCombo.Items.Add("WhateverA")
mcboCombo.Items.Add("WhateverB")
b) Then when you open frmPrefs you pass the combobox in as a property
VB Code:
Dim frm As New frmPrefs()
frm.Combo = ComboBox1 ' or whatever you called it
frm.ShowDialog()
frm = Nothing
Hope this helps
-
Jan 25th, 2002, 03:06 PM
#15
Thread Starter
Junior Member
Success!
Thank you! That idea works perfectly! I've got full functionality. Strange that they would make it so complicated. Thanks again for your help, I really appreciate you taking the time to work with me.
Indebted,
Reid
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
|