|
-
Oct 1st, 2003, 07:54 PM
#1
Thread Starter
New Member
Object Reference and Casting
Hi everyone I'm starting on VB.net and have encounter some problems:
1._ I have two Forms (Form1 and Form2), on Form1 I have a public variable; Form1 calls Form2. My problem is that I need to change that public variable from Form2. I have (in Form2) a variable declared as Form1, but I don't know how to make that variable point to the existing instance of Form1.
2._ Is there a legal way to cast from an object to a button. 'Cause I have something like this:
Dim btn as button;
btn=sender '(which is an object);
When I use strict off I can do it fine but I get an error if I turn strict on.
Thanks for the help.
-
Oct 1st, 2003, 07:58 PM
#2
VB Code:
Dim btn as button = DirectCast(Sender , Button)
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Oct 1st, 2003, 08:01 PM
#3
Sleep mode
For the variable part why don't you put it in a module ?
-
Oct 1st, 2003, 08:41 PM
#4
Thread Starter
New Member
I guess I'm going to declare it in a module, but just for knowing do you know how to do it, 'cause there's got to be a way...
-
Oct 1st, 2003, 08:47 PM
#5
Sleep mode
By passing it in the constructor I think .
-
Oct 1st, 2003, 09:30 PM
#6
Lively Member
1. Do not use a module. That sure sounds like the old (wrong) way of just making everything global. That is bad programming. Here is how you do it the right way, via a reference to Form1.
Declare a variable, call it FormOne in form2 at the class level.
Private FormOne as Form1
In form2s constructor (the New method, expand the generated code section to see it), you need to add something like this
public sub New(frm1 as form1)
' put this at bottom of method
FormOne = frm1
In Form1 you create a Form2 like this:
Dim frm2 as new Form2(Me)
Frm2.show
Now you have a reference to your Form1 object that instantiated Form2, so you can reference controls like this:
FormOne.someControl.someProperty = someValue
or
FormOne.someVariable = someValue
2. If you are talking about the sender object in events, just say something like this, assuming the sender is a button of course
dim btn as button = sender
now you can call all methods and use properties of a button via your btn object. I believe VB will cast it for you using that syntax, but the other way works just as well
-
Oct 1st, 2003, 09:41 PM
#7
I wonder how many charact
1) Make a public property in Form1 that changes the variable.
Then in form2, you can simply change that property by coding:
VB Code:
Form1.someProperty = someValue
-
Oct 2nd, 2003, 05:14 AM
#8
Sleep mode
-
Oct 2nd, 2003, 07:20 AM
#9
Lively Member
Originally posted by nemaroller
1) Make a public property in Form1 that changes the variable.
Then in form2, you can simply change that property by coding:
VB Code:
Form1.someProperty = someValue
This will not work, ever, without a reference to form1. .NET is not VB6. In VB6 this is how it was done, but not any more.
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
|