ditoskas Posted December 13, 2006 Posted December 13, 2006 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#? Quote
IceAzul Posted December 13, 2006 Posted December 13, 2006 Same thing, just with a semi-colon at the end: Form1.Label1.Text = "My Text"; Quote
Leaders snarfblam Posted December 14, 2006 Leaders Posted December 14, 2006 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] Quote [sIGPIC]e[/sIGPIC]
MrPaul Posted December 14, 2006 Posted December 14, 2006 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: Quote Never trouble another for what you can do for yourself.
ditoskas Posted December 19, 2006 Author Posted December 19, 2006 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!!! :) :) Quote
Administrators PlausiblyDamp Posted December 19, 2006 Administrators Posted December 19, 2006 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ditoskas Posted December 19, 2006 Author Posted December 19, 2006 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? Quote
mskeel Posted December 19, 2006 Posted December 19, 2006 Where are you calling this code from? frmmain main = new frmmain(); //then i can use the methods main.tlbmsg.text = "My text"; Quote
Denaes Posted December 19, 2006 Posted December 19, 2006 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 Quote
ditoskas Posted December 19, 2006 Author Posted December 19, 2006 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? Quote
ditoskas Posted December 19, 2006 Author Posted December 19, 2006 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!!! Quote
Denaes Posted December 19, 2006 Posted December 19, 2006 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. Quote
ditoskas Posted December 19, 2006 Author Posted December 19, 2006 and how can i make something like this??? can you give me a tutorial or an example to learn how can i make it? Quote
Denaes Posted December 19, 2006 Posted December 19, 2006 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. Quote
Administrators PlausiblyDamp Posted December 19, 2006 Administrators Posted December 19, 2006 http://www.xtremedotnettalk.com/showthread.php?t=83092 contains examples of how to do this kind of thing in VB while http://www.xtremedotnettalk.com/showthread.php?t=97057 offers tips on how to port vb code to C#. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ditoskas Posted December 19, 2006 Author Posted December 19, 2006 ok thks for help i will try it Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.