473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Apologies : Exposing form controls to a class instance / object

Hi all,

First of all I'd like to apologise. There are many many posts about this around the net, however I just can't for the life of me get mine to work.

Simply, I was to be able to expose myTextBox or myListBox to a child class.

Expand|Select|Wrap|Line Numbers
  1. namespace MyApp
  2. {
  3.     public partial class MyNewApp : Form
  4.     {
  5.         public void doSomething(string s)
  6.         {
  7.             myFormListbox.items.add(s);
  8.         }
  9.  
  10.         // Custom Class
  11.         public static myClass myClassObject = new myClass();
  12.         public class myClass
  13.         {
  14.             private string myVar;
  15.  
  16.             //Constructor
  17.             public myClass()
  18.             {
  19.             }
  20.             //Destuctor
  21.             ~myClass()
  22.             {
  23.             }
  24.             //Some things
  25.             public string getKey()
  26.             {
  27.                 return this.myVar;
  28.             }
  29.             public void setName(string sValue)
  30.             {
  31.                 this.myVar = sValue;
  32.             }
  33.  
  34.         public void addToListBox(string s)
  35.         {
  36.             doSomething("test");
  37.         }
  38.  
  39.         }    
  40.     }
  41. }
  42.  
Any pointers to anything obvious I'm doing wrong? I've tried using 'base' or 'form' and even tried passing the base form as a reference to the constructor.

Thanks for any help.
James
Jul 23 '12 #1
1 1375
Aimee Bailey
197 Expert 100+
I know it is possible to make a class definition within another class (nesting), but id strongly suggest against it as it then becomes very easy to get tangled in knots. Instead define the class below the form class, here is an example that will hopefully answer some of your questions.

Expand|Select|Wrap|Line Numbers
  1. public partial class MyNewApp : Form
  2. {
  3.     public string ParentVar;
  4.     public MyClass instance1;
  5.  
  6.     public MyNewApp()
  7.     {
  8.         InitializeComponent();
  9.         instance1 = new MyClass(this);
  10.     }
  11.  
  12.  
  13.     public void SetVar(string s)
  14.     {
  15.         instance1.MyVar = s;
  16.     }
  17. }
  18.  
  19. public class MyClass
  20. {
  21.     public string MyVar;
  22.     public Form Owner;
  23.  
  24.     public MyClass(Form owner)
  25.     {
  26.         this.Owner = owner;
  27.     }
  28.  
  29.     public void SetParentVar(string s)
  30.     {
  31.         Owner.ParentVar = s;
  32.     }
  33. }
  34.  
All the best!

Aimee Bailey
Jul 24 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
3
by: Robert Oschler | last post by:
Hello, I am a Python newbie (by experience, not chronologically :) ), so if any of this doesn't make sense my apologies in advance. I am reading the chapter in The Python Cookbook on databases...
3
by: Chris | last post by:
Hi, I'm trying to append text from another class to a generic richTextBox that I've added to a Windows form. I can't seem to figure out how to expose the richTextBox to append text to it. ...
4
by: Tony W | last post by:
Hi, I am trying to write a simple application to retrieve data from the Windows registry and insert it into textboxs on a windows form. So far I have one namespace containing two classess. ...
1
by: Mamatha | last post by:
Hi I have a small application in VB.NET,it consists of one form,one class and one module.I have declared one thread globally in module to execute the function in the form.I started that thread...
4
by: Dries De Rudder | last post by:
Hi, I am trying to bind an object to form controls. I've got an object MyObject which has some properties e.g. ID. I've got a form, Form1, that contains a textbox ,TextBox1, now I want to...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.