vb and vc#

ditoskas

Newcomer
Joined
Dec 13, 2006
Messages
8
hello i am vb user and now i try to learn the vc#.
i want to learn how can i change the properties from one form to other.
for example in vb.
if i write this code in the form2
Visual Basic:
 Form1.Label1.Text = "My Text"
then the label from form1 will change the text. how can i make the same in VC#?
 
It will take you some time to get used to the C# syntax, but once you get past all the curly braces and semi-colons, you will realize that it is basically the same thing as VB. A good place to get started would be C# to/from VB.Net Recipes. The biggest changes you will notice are that every statement in C# ends with a semicolon, comments use // instead of ', and that instead of using this type of syntax:
Code:
[COLOR=Blue]Construct[/COLOR]
    [Color=Green]' Code
[/Color][COLOR=Blue]End Construct[/COLOR]
it uses this type of syntax:
Code:
[COLOR=Blue]Construct [/COLOR]{
    [Color=Green]// Code
[/Color]}
 
Control encapsulation

If you wish to access a Form's controls from outside the Form - from another type of Form, for instance - the the Form must expose the controls via properties. So if you want to allow access to Label1 from outside Form1 then you must expose it like so:

C#:
public Label Label1 {
    get {
        return this.label1;
    }
}

However, depending on your application it is probably better practice to not expose the controls themselves, but provide properties and methods which access the controls while keeping them encapsulated within the Form.

Good luck :cool:
 
nothing

nothing :-(
i try more things but i can't find nothing i am ready to brake up my pc
the same problem when i try to make this
C#:
        public ToolStripLabel Changelbl
        {
            get
            {
                return this.tlbmsg;
            }
        }
i can't call this method from other form.
but i can find the methods if i declare a new form
frmmain main = new frmmain();
then i can use the methods
main.tlbmsg.text = "My text";
but i want to change my already shown form.
please help!!! :) :)
 
If the method or property is an instance method (i.e. not static - shared in vb) then you need to create a valid instance of the form before you can access it.

If Form1 is a variable then it will work - if Form1 is a class you will need to create an instance of Form2 and use that.
 
PlausiblyDamp said:
If the method or property is an instance method (i.e. not static - shared in vb) then you need to create a valid instance of the form before you can access it.

If Form1 is a variable then it will work - if Form1 is a class you will need to create an instance of Form2 and use that.

how can i make it?
 
Where are you calling this code from?
C#:
frmmain main = new frmmain();
//then i can use the methods
main.tlbmsg.text = "My text";
 
Actually I think the biggest or most jarring change from VB to C# was the way C# handles strings as regular expressions.

If you're using 2005, you're gold. If you're using 2002/2003, prepare for a ton of compiler "quirks" that you're not used to with VB. You fix one error and 50 more pop up. Then you fix another and you're down to 20 errors. Then you fix 2 errors and nothing changes. Then you recompile the app and you have 3 errors left.

C# 2005 is amazing. They really listened and got a lot of helpful UI fixes. Improved intellisense, error tracking & bracket highlighting. Just talking about it makes me want to use C# again, but I'm in a vb.net project :P
 
first step has solved
if i try with static method i can see the method but i can't use the expression "this" what i mean?
if i write in Form1 this method
static public ChangeMsg(string msg)
{
this.Label1.Text = "My text";
}

then in Form2 i can write
Form1.ChangeMsg("My message");
but the compiler give me an error in Form1 - ChangeMsg method
"this is not a member of static...."
why?
 
i have to make my project in VC# 2005 i prefer VB6 or VB.net from this but my teacher want VC# 2005 please help!!!
 
ditoskas said:
first step has solved
if i try with static method i can see the method but i can't use the expression "this" what i mean?
if i write in Form1 this method
static public ChangeMsg(string msg)
{
this.Label1.Text = "My text";
}

then in Form2 i can write
Form1.ChangeMsg("My message");
but the compiler give me an error in Form1 - ChangeMsg method
"this is not a member of static...."
why?

This isn't a C# problem, but a .Net problem. Same will happen in vb.Net if you did this.

You're can't access a non-static/non-shared variable/property from a static(C#)/shared(VB) function.

If you need to change something in that instance of the form, you're going to need to pass a reference of Form1 to Form2 and set the property on that reference. Or possibly raise an event on Form2 that Form1 can handle, which might be "better", but is a little more complicated.
 
and how can i make something like this???
can you give me a tutorial or an example to learn how can i make it?
 
ditoskas said:
and how can i make something like this???
can you give me a tutorial or an example to learn how can i make it?

Easiest way is to pass it over in the constructor.

On Form2 in the constructor, have a paramater for a form and pass that reference to a private variable on Form2. From there you can set the text using that primary variable because it has a reference to Form1.

On Form1, when you create the new instance, pass a reference of itself (this) to Form2 in it's constructor.
 
Back
Top