How can I get the instance of a winform class?

  • Thread starter Thread starter Jeff Yang
  • Start date Start date
J

Jeff Yang

How can I get the instance of a winform class?
namespace DZ
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

.......

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}

public class A
{
public void GetInstance()
{
//How can I get the instance of Form1?
}
}
}

regards,
Jeff Yang
 
Jeff Yang said:
How can I get the instance of a winform class?

You tell your other class about it somehow, just as you would any other
class.

For some reason, many people seem to think that GUI classes are
completely different from all other classes - they ask how you can pass
parameters out of them, etc. They're really not different at all, in
terms of the language. (They tend to have specific threading rules etc,
but that's not magic either.)

So, think about how you'd do it if it were a normal non-GUI class, and
do it the same way.
 
everything in C# is a type, handle them the same way you would any other
type.

People dont understand that conept fully and they just balk when u mention
attributes.

How to cut down the java FUD, just mention "Custom Attributes" :D something
they dont have :D
 
news.microsoft.com said:
How to cut down the java FUD, just mention "Custom Attributes" :D something
they dont have :D

What Java FUD is that? I haven't seen any around here for a long time.

(Custom attributes in some form or other are coming in Java 1.5, I
believe, along with generics and enumerations.)
 
In fact I don't known how to tell other class about it.Below is a .net
remoting example,can you help me to complete the mission?thanks!(If it's
needed,you can modify my class)

namespace DZ
{
public class Form1 : System.Windows.Forms.Form
{
public int aaa;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(A),"Hi",WellKnownO
bjectMode.SingleCall);
}

.......

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}

public class A:System.MarshalByRefObject
{
public void GetInstance()
{
//I want to get aaa's value in class Form1,so I should get the instance of
Form1.But how can I get the instance of Form1?and there is no code like"new
A" in class Form1
}
}
}
 
Jeff Yang said:
In fact I don't known how to tell other class about it.

Well, how are they related? Will you only ever have one instance of
Form1? If so, make it a singleton. If not, what (in human terms)
relation is there between the instance of A and the form? If there were
two forms up, which would you want to be fetched by GetInstance?
 
then the following is a full server and client,the client want to use method
Getaaa() to get aaa's value in Form1,then how can I fill public int
Getaaa()?

namespace DZ
{
public class Form1 : System.Windows.Forms.Form
{
public int aaa;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(A),"Hi",WellKnownO
bjectMode.Singletone);
}

.......

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}

public class A:System.MarshalByRefObject
{
public int Getaaa()
{
//the client use this method to get aaa's value.How can I implement this
method?
}
}
}

the client maybe like this:
namespace Client
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel());
A obj=(A)Activator.GetObject(typeof(A),"tcp://localhost:8086/Hi");
if(obj==null)
{
Console.WriteLine("could not locate server");
Console.ReadLine();
return;
}
int value=obj.Getaaa();
Console.ReadLine();
}
}
}

reguards,
Jeff Yang
 
Jeff Yang said:
then the following is a full server and client,the client want to use method
Getaaa() to get aaa's value in Form1,then how can I fill public int
Getaaa()?

You haven't answered my previous question: is the *form* a singleton or
not?
 
Oh,sorry,Form1 is not a Singletone.I want to use a seperate class to deal
with the remoting part,and leave Form1 to do other works.Can I realize it?

regards,
Jeff Yang
 
Jeff Yang said:
Oh,sorry,Form1 is not a Singletone.I want to use a seperate class to deal
with the remoting part,and leave Form1 to do other works.Can I realize it?

So if Form1 isn't a singleton, which instance of Form1 do you want your
remoting part to fetch?
 
I have read some articles after received your reply.and I find some of your
conceptions(such as "make a class singletone or whether a class is
singletone") is for Java,and i don't known if there has such conceptions in
C# either.I do not known java,and i also do not know what means is make a
class singletone or how can i tell if a class is singletone.

I do not know if my expression is not right.In Class A,I mean A is a
class,and in A a,then I mean a is an instance of A;
So i think the system will make an instance of Class Form1 when the program
is started,and when the client access the remote object,then the system will
make an instance of class A.

English is not my mother tongue.If there are any misunderstandings,i am
sorry.

reguards,
Jeff Yang
 
Jeff Yang said:
I have read some articles after received your reply.and I find some of your
conceptions(such as "make a class singletone or whether a class is
singletone") is for Java,and i don't known if there has such conceptions in
C# either.

No, the concept of a singleton isn't Java-specific. It's a general
concept, namely "is there only ever going to be one instance of this
class? If so, have a common way of getting at it, which (usually)
creates it if it doesn't already exist).
I do not known java,and i also do not know what means is make a
class singletone or how can i tell if a class is singletone.

I do not know if my expression is not right.In Class A,I mean A is a
class,and in A a,then I mean a is an instance of A;

Yes, and that's find.
So i think the system will make an instance of Class Form1 when the program
is started,and when the client access the remote object,then the system will
make an instance of class A.

English is not my mother tongue.If there are any misunderstandings,i am
sorry.

If there is only ever going to be one instance of Form1, then you can
put that in a static member somewhere, eg Form1.instance which may be
backed by a read-only property of Form1.Instance. However, you need to
consider what happens if another instance of Form1 is constructed, and
possibly safe-guard against that.
 
Back
Top