Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 13th, 2007, 09:51 PM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam
Default control properties and methods access problem

Hey Experts
I wanna know how to access control properties and methods in another different window.
for example,I created a window application containing more than just one window.In the Main Window,I click an item of StripMenu and another window will pop up. In this window,how can I access control properties and methods in the main window?
I serched online but got no ideal solvement.I need to declare public property in the main window?
great appreiciation,kinda urgent.

ERIC
__________________
ERIC
 
Old April 14th, 2007, 08:32 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

In order to access control properties from another form, you need to provide a public property on the form containing the control that exposes the desired property or the whole control. For example, if you had a login form with a user name textbox, you could expose just the text of the textbox:

public class MyForm : Form{
   ...
   public string UserName{
      get{ return txtUserName.Text; }
      set{ txtUserName.Text = value; }
   }
   ...
}

-Peter
 
Old April 14th, 2007, 11:40 PM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam
Default

What if I want to access control methods from another form?thanks for helping me!

ERIC
 
Old April 15th, 2007, 09:55 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You could expose the control itself publicly on the form and do whatever you like. However, this is not the best design practice. What would happen if you wanted to change the control type within the form? Instead of exposing the control itself, expose the properties and/or methods you need to use. This is a safer way of doing it by encapsulating the functionality. Obviously, this will result in more individual property accessors and method wrappers, but in the end it's safer.

-Peter
 
Old April 16th, 2007, 03:07 AM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam
Default

I'm sorry,but could you please be more specific? How can I expose control method in a form and then access the method in another form? and you mentioned that I can expose the control itself publicly on the form,how to make it?
GREAT GREAT APPRECITION!


ERIC
 
Old April 16th, 2007, 07:45 AM
Authorized User
 
Join Date: Feb 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I understand what you are asking, then you could do it something like this.

Form1.Username = "abc";


There are 10 types of people - Those who understand binary, and those who don't.
http://bschleusner.googlepages.com/home
 
Old April 16th, 2007, 08:29 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Create a public property of the type of the control you want to expose. In the case of exposing a control, you should use a readonly property as it's unlikely you'll ever want to support re-assigning the control.
Code:
public class MyForm : Form{
   ...
   public TextBox UserNameControl{
      get{ return txtUserName; }
   }
   ...
}
Then you can use that control from outside the form:
Code:
MyForm myFormInstance = new MyForm();

myFormInstance.UserNameControl.Text = "name";
However, like I suggested before, it's better practice to expose just the properties of the control you need to expose instead of the control itself. This is safer and often easier from the perspective of the consuming code.

-Peter

(Edit - Added formatting)
 
Old April 16th, 2007, 09:34 AM
Authorized User
 
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam
Default

Thanks for this detailed explanation.You already gave me tips how to create public property of a whole control.But how about creating public property of a specific method of a control?any way to realise it? This is really embarrassing and sorry for constant questions...
great appreciation.

ERIC
 
Old April 16th, 2007, 02:23 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If you want to expose a control method on the containing form, just write a method and call it internally:
Code:
public class MyForm : Form{
   ...
   public void DoSomething(){
      innerControl.DoSomething();
   }
   ...
}
-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create methods in Windows Control Library Seduccion VB.NET 2002/2003 Basics 0 April 19th, 2007 11:02 PM
methods and properties reference rmoguel Crystal Reports 0 January 31st, 2006 07:38 PM
Access properties & methods from a WebService sloesch VB.NET 8 January 24th, 2006 02:02 PM
Properties/Methods Not in Object Browser ritag VS.NET 2002/2003 0 January 13th, 2005 12:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.