Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 Form1.Label1.Text = "My Text" 

then the label from form1 will change the text. how can i make the same in VC#?

  • Leaders
Posted

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:

[color=Blue]Construct[/color]
   [Color=Green]' Code
[/Color][color=Blue]End Construct[/color]

it uses this type of syntax:

[color=Blue]Construct [/color]{
[Color=Green]// Code
[/Color]}
[/Code]

[sIGPIC]e[/sIGPIC]
Posted

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:

 

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:

Never trouble another for what you can do for yourself.
Posted

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

       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!!! :) :)

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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?

Posted

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

Posted

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?

Posted
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.

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...