Easy Newbie question

  • Thread starter Thread starter Andrew Mueller
  • Start date Start date
A

Andrew Mueller

Hello,

I am having issues (probably related to my VB background) using C# and
changing the properties of my form's controls from a separate class within
the solution. Here is some of my code:

namespace ArchiveDataExport{

public class Form1 : System.Windows.Forms.Form{
public System.Windows.Forms.GroupBox gp1;
public System.Windows.Forms.Textbox txtServerName;

Public Form1()
{
InitializeComponent();
}

......

All the stuff generated for cleaning up resources
......
All the code to 'do stuff' when I press buttons, etc..

-------- Separate Class Object within solution -----------

namespace ArchiveDataExport

Public Class Historian{
te
string strSQLConnection = "Provider=SQLOLEDB; Data Source = " +
ArchiveDataExport.Form1.txtServerName.Text
}


Of course, this is abbreviated... But the point is that I need to use the
Text property of the txtServerName textbox object for concatenation..

Thanks in advance,

Andrew Mueller
 
Yes indeed, in VB6 you could work like this, but in .NET (both in C# and
VB.NET) you need to get a hold of the reference of your Form, from which you
want to use a control. So make sure you keep somewhere a reference when you
instantiate the form. Also make sure the textbox is declared public (or at
least friend). A nicer way of working (in an OO way) would be to add a
public property that would get you the value of the textbox.
 
Do you mean by creating a variable to hold the property value? By doing a
Get? That seems so awkward compared to VB6. Is there no better way to do
this? For the first part, referencing... How would I do something like
that?

Thanks in advance,

Andrew Mueller
 
Andrew,

you are using Form1 The class and not form1 the object. In your inner
class, create a local variable of type Form1 and pass it in the constructor;

public class Class1
{

public Class1(Form1 paramForm1)
{
LocalForm1Variable = paramForm1;
}
}

this way, you'll have access to your form one using the LocalForm1Variable
variable.

You should have set/get properties for this, but this'l work.

hope it helps

Marco
 
This is the OO way, so I think it's not akward or something like that. I
would even say the VB6-way is somewhat "dirty".

Check out Marco's answer for an example.
 
Also check out this article, it does a great job on describing your problem
(although it's in vb.net, but in c# it's the same):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp
Describes how working with multiple forms has changed from previous editions
of Microsoft Visual Basic and illustrates several key techniques, including
displaying a second form, changing the appearance of another form, and using
a form as a dialog. (8 printed pages)

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Back
Top