471,627 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Var/Object Access

Cant access a object that is in the same class it seems...
With a new c# win. app. project in vis studio 2k5, if I want to access a
richTextBox that was defined in Form1.Designer.cs. (richTextBox1).

I write a simple line in program.cs
richTextBox1.Text="Test 1.1";

Essentially richTextBox1 is unknown to program.cs ?
so I figured there was a proper name to this textbox

Form1.richTextBox1 ?

So my question is how to access objects from the partial class Form1 ?
Jan 5 '06 #1
2 1529
Firstly, they aren't the same class: one is Form1, and the other is Program

Secondly, the control is (by default) private. And rightly so. You could
make this "internal" or "public", but I wouldn't recommed it. Better to
expose this via a custom property on Form1:

public string RichText {
set { richTextBox1.Text = value; }
get { return richTextBox1.Text; }
}
Thirdly, in C# (and .Net generally) there is no automatic default instance
of (for instance) a form, so you need to work with an instantiated instance
of the form - something like (in Program.cs)
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
using (Form1 formInstance = new Form1()) {
formInstance.RichText = "Test 1.1";
Application.Run(formInstance);
}
}

Would you be moving over from VB6 by any chance? I guess this from the
Form1.richTextBox1 line.

The code above works fine for me.

Marc

"Christopher" <Ch*********@discussions.microsoft.com> wrote in message
news:C8**********************************@microsof t.com...
Cant access a object that is in the same class it seems...
With a new c# win. app. project in vis studio 2k5, if I want to access a
richTextBox that was defined in Form1.Designer.cs. (richTextBox1).

I write a simple line in program.cs
richTextBox1.Text="Test 1.1";

Essentially richTextBox1 is unknown to program.cs ?
so I figured there was a proper name to this textbox

Form1.richTextBox1 ?

So my question is how to access objects from the partial class Form1 ?

Jan 5 '06 #2
Thanks for the reply..

"> static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
using (Form1 formInstance = new Form1()) {
formInstance.RichText = "Test 1.1";
Application.Run(formInstance);
}
}

Would you be moving over from VB6 by any chance? I guess this from the
Form1.richTextBox1 line.


Well that was just silly of me , I was confused by the partial class
declarations.

Thanks
Jan 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave Kolb | last post: by
3 posts views Thread by Shawn | last post: by
4 posts views Thread by Luke Matuszewski | last post: by
4 posts views Thread by gg9h0st | last post: by
2 posts views Thread by Crimson* | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.