Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2008

Tip: Looking for answers? Try searching our database.

Passing class references

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AMP - 18 Mar 2008 20:53 GMT
Hello,
Where can I find a GREAT explanation about keeping references to other
classes/forms between each other.
I'd like to see code that uses the different methods , public
properties, set-get.
Am I correct in saying one form or class has to instantiate the other
one for this to work?
Thanks
Mike
Morten Wennevik [C# MVP] - 19 Mar 2008 10:21 GMT
> Hello,
> Where can I find a GREAT explanation about keeping references to other
[quoted text clipped - 5 lines]
> Thanks
> Mike

Hi Mike,

GREAT examples pretty much depend on what you already know and what you
don't know, but below are some examples of using a windows form and a class
and how they may interact.  One example uses DataBinding as well, just to
show you can have a control and an object interact behind the scenes.  You
should however wait using DataBinding until you are more comfortable of how
winforms work as DataBinding errors can be very cryptic and difficult to
solve.

The examples assumes you have created a windows application project.  
Replace everything except the 'using' lines at the top of the code file.

public partial class Form1 : Form
{
   private MyObject instanceOfMyObject;
   public Form1()
   {
       Button button1 = new Button();
       Controls.Add(button1);
       button1.Click += new EventHandler(button1_Click);
   }

   protected override void OnLoad(EventArgs e)
   {
       instanceOfMyObject = new MyObject();
       instanceOfMyObject.InstanceString = "Hello World";
       MyObject.StaticString = "Static stuff";
   }

   private void button1_Click(object sender, EventArgs e)
   {
       using (MyDialog dialog = new MyDialog(instanceOfMyObject))
       {
           dialog.ShowDialog();

           MessageBox.Show(instanceOfMyObject.InstanceString);
       }

       using (MyDialog dialog = new MyDialog())
       {
           dialog.DisplayText = MyObject.StaticString;
           dialog.ShowDialog();

           MessageBox.Show(dialog.DisplayText);
       }

   }

   public class MyDialog : Form
   {
       private TextBox textBox1;

       public MyDialog()
       {
           InitializeComponent();
       }

       public MyDialog(MyObject myObject)
       {
           InitializeComponent();

           textBox1.DataBindings.Add("Text", myObject, "InstanceString",
false, DataSourceUpdateMode.OnPropertyChanged);
       }

       private void InitializeComponent()
       {
           textBox1 = new TextBox();
           Controls.Add(textBox1);
       }

       public string DisplayText
       {
           get { return textBox1.Text; }
           set { textBox1.Text = value; }
       }
   }

   public class MyObject
   {
       private string _instanceString;
       private static string _staticString;

       public static string StaticString
       {
           get { return _staticString; }
           set { _staticString = value; }
       }

       public string InstanceString
       {
           get { return _instanceString; }
           set { _instanceString = value; }
       }
   }
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.