I had no problems in vb6 :confused:
How can I do it in .net?
I'd like to access and change values of properties of the objects on another form.
ie: function T ( ByRef myForm as System.Windows.Forms.Form)
myForm.txtName.Text = "Joe"
end function
Printable View
I had no problems in vb6 :confused:
How can I do it in .net?
I'd like to access and change values of properties of the objects on another form.
ie: function T ( ByRef myForm as System.Windows.Forms.Form)
myForm.txtName.Text = "Joe"
end function
In your example above you are only passing a reference to a generic form.Quote:
Originally posted by wordracr
I had no problems in vb6 :confused:
How can I do it in .net?
I'd like to access and change values of properties of the objects on another form.
ie: function T ( ByRef myForm as System.Windows.Forms.Form)
myForm.txtName.Text = "Joe"
end function
If you need to access txtName you need to reference the form that contains the txtName.
And btw, Forms are a reference type so it doesn't matter if you use ByRef or ByVal.
ie: Function T (ByVal myForm As Form1)
This is a sub I use to set forecolor=black to all objects on a specified form, excepted some object types
To call it, from a specified Form, for the objects of which you want to set forecolor=black, you only need to write (GIN is the project's name. The function is in a module):
GIN.PblSubForeColorNero(Me)
VB Code:
Public Sub PblSubForeColorNero(ByVal ThisForm As System.Windows.Forms.Form) Dim ThisControl As System.Windows.Forms.Control For Each ThisControl In ThisForm.Controls If Not TypeOf ThisControl Is Button Then 'Or TypeOf ThisControl Is RadioButton Or TypeOf ThisControl Is Label Or TypeOf ThisControl Is CheckBox Then ThisControl.ForeColor = System.Drawing.Color.Black End If Next ThisControl End Sub
As Pax said, it should be correct to use byref, but it doesn't matter in this case!
Good job:)
The simple answer is:Quote:
Originally posted by wordracr
I had no problems in vb6 :confused:
How can I do it in .net?
I'd like to access and change values of properties of the objects on another form.
ie: function T ( ByRef myForm as System.Windows.Forms.Form)
myForm.txtName.Text = "Joe"
end function
The calling event in myFormVB Code:
Public Function T(ByVal obj1 As Object) As Object obj1.text ="Joe" End Function
VB Code:
T(txt.Name)
I have assumed the Function is in the Module. If the function is in another form, you will have to make sure that form is properly instanced and accessible.
I have also assumed Option Strict is OFF so that any object can be passed to the function. If you want to limit the function to handling a specific type of object then you can use early binding and Option Strict ON
Thanks for the help, but it doesn't answer my specific question.
I'm passing a FORM because there are repetitive form controls being modified. I'd prefer not to pass every control that I'm working with.
How can this be done?
the answers are up posted.
THese dont solve my problem. I dont see how you can say that answers it.
I have build errors right now.
quick sample. there's 2 form [let's say form1 and form2]. in form1, there's a button which shows the form2. like this.
and in form2, we could change the constructor, like this.VB Code:
Dim f As New Form2(Me) f.ShowDialog()
and on load of form2VB Code:
Dim f As Form Public Sub New(ByRef form) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() f = form 'Add any initialization after the InitializeComponent() call End Sub
hope this helps. pax, taxes and alex here has this idea though. credit for them... thanx...VB Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load f.BackColor = Color.Red End Sub
Then you haven't looked hard enough. My post clearly answers you question...:ehh:Quote:
Originally posted by wordracr
THese dont solve my problem. I dont see how you can say that answers it.
I have build errors right now.
I would also add my assurance that the codings you have been given work. If you are getting build errors, then you have probably not created the correct instances of the forms.Quote:
Originally posted by wordracr
THese dont solve my problem. I dont see how you can say that answers it.
I have build errors right now.
I would also emphasise the remarks in my previous post
"I have assumed the Function is in the Module. If the function is in another form, you will have to make sure that form is properly instanced and accessible.
I have also assumed Option Strict is OFF so that any object can be passed to the function. If you want to limit the function to handling a specific type of object then you can use early binding and Option Strict ON"
Please answer the following:
1. Where is the Function?
2. What is your startup object?
3. What are your build errors?
The problem is probably that I didn't change my project setup yet, so the STARTUP obj is "form1". I have heard if you use multiple forms, you have to use a sub main() in a module. I will change that soon.
IN a module, the public function I'd like to use can modify objects of form1, form2, or form3.
function T(ByVal theForm as System.Windows.Forms.Form)
theForm.A_Form_Obj.top = 10
End Function
ERROR: "A_Form_Obj is not a member of System.Windows.Forms.Form"
Another possible problem: Option strict. It's off right now. Is there a way in tools to have it on by default?
Thanks a lot for the continuing help!
Yes , go to Tools menu , Options , Projects item , there you go .Quote:
Originally posted by wordracr
Another possible problem: Option strict. It's off right now. Is there a way in tools to have it on by default?
Hi,
You can use the form1 as the startup if you want that form to remain open throughout the project, which seems to be what you are doing but I prefer to ALWAYS use the Sub Main of a Module. What you require is the following;
In the Module
VB Code:
Public frm1 As New Form1 Public frm2 As New Form2 Public Sub Main() Application.Run(frm1) End Sub
When you want to open frm2: In the appropriate event of form1
VB Code:
frm2.ShowDialog()
Now you can put the following in either form2 or the module
VB Code:
Public Sub T(ByVal obj1 As Object, strText as String) As Object obj1.text strText End Sub
Now, you can call this Sub from form1 passing the parameters of the control and the value of the string as required. Please note:
1. You use this to alter the Text property of any appropriate object to the chosen characters. You will have to call the sub for each object one at a time.
2. If you want to make the Text of all the objects then you can call the sub from a loop through all controls in form1. (You can set the loop to select only those controls of the same type or you can even select controls of different types omitting some of those types)
3. You will find that you can now access any control of either form from form1 or form2
Hope that helps.
Quote:
Originally posted by Pirate
Yes , go to Tools menu , Options , Projects item , there you go .
Nope. At least not in VB.NET 2003
Right click on project name in solution explorer.
Choose Properties from context menu (sometimes - I don't know
why - the wrong
Properties page turns up.
If so repeat)
Click Build item
Open the Option Strict dropdown list
If the poster is still confused...
A form is now just another object. So you are not loading "Form2" per se... you're loading an instance of Form2 (just like any other class object).
So, if you have a "frmMain" and "frmChild" and want to show frmChild by pressing a button, you first need to declare it as an object.
VB Code:
Dim child1 as New frmChild 'A new object from which to work with child1.Show 'Show the instance of the form
So, in the sub procedure in which you want to pass a frmChild as a parameter, set it up like this:
VB Code:
Private Sub DoThis (ByVal child as frmChild) 'code... End Sub
Hope this helps!
HI,Quote:
Originally posted by wordracr
IN a module, the public function I'd like to use can modify objects of form1, form2, or form3.
function T(ByVal theForm as System.Windows.Forms.Form)
theForm.A_Form_Obj.top = 10
End Function
ERROR: "A_Form_Obj is not a member of System.Windows.Forms.Form"
Just one point about the above section of your post.
You can use
function T(ByVal theForm as System.Windows.Forms.Form)
theForm.A_Form_Obj.top = 10
End Function
provided you set the Modifier property of A_Form_Obj to Public.
Sorry, I did not notice that part of your post, sooner.