473,461 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing Components of One Form From Another

16
So have two forms under the same namespace and I need to be able to access a richtextbox attached to the form "Home" from the form "Editor". But whenever I try to access it it doesn't come up, I'm new to C# but I'm assuming this is from the public/private modifiers. Also, "Home" is the MdiParent of "Editor" if that makes a difference. So what I'm trying to do is like.
Expand|Select|Wrap|Line Numbers
  1. Home.richtextbox.Text = "Editor Running"; 
  2.  
Any help would be greatly appreciated.
Oct 10 '09 #1
9 6297
tlhintoq
3,525 Expert 2GB
Just a suggestion...

Don't access controls directly like that. It's much better to raise an event that the other form is subscribed to.

If your "Editor" raises an event like CurrentModeEvent(new ModeArgs("Running"));

Then you could have 1 or a dozen tools, forms, controls subscribed to that event. They all react at the same time. Yes your Editor form is not tightly tied to the other forms.

Right now it might be only one form that listens to Editor so it doesn't seem like a big deal. But later you add a toolboxpallet... then you add another form...
Oct 10 '09 #2
l3mon
16
I'm sorry... I'm really not that good with this. I have multiple forms that will be children of the "Home" form (such as the editor form) and the only part of the "Home" form I want to affect is a richtextbox similar to the output box under View->Output (in Visual c#). So it's showing the collective output/debug infostuffs of all the forms. I'm not really following you. Just how is it possible to get to the form or at least a method on it which I could use to update the status box.
Oct 11 '09 #3
tlhintoq
3,525 Expert 2GB
In that case you REALLY don't want to be accessing your 'Output" richtextform directly from a bunch of other forms. It just makes it a mess to maintain.

You want to make a custom event that your Output window listens for. Whenever it hears the event it reacts by putting the text of the eventarg into the richtext box.

Think of your program as a bingo parlor. The events are the man picking the numbers. He just yells out N-15 and all the players hear him and respond by checking the bingo cards. The players all subscribe to the event of:
Caller.yells(PickedNumber);

The caller doesn't know anything about the players or their game cards. He has one job, yell the numbers. It doesn't matter if 1 person is playing or 100.

I suggest you first make a generic form with all your custom but commonly shared code it in. This form will have a custom event something like "LogThis" or "ReportThis"

Now when you make all of your other children forms, make them inherit from that common form that already has the event. This way *they* inherit that event.

When your Mainform makes newforms it can do something like this...
Expand|Select|Wrap|Line Numbers
  1. MyCustomForm child1 = new MyCustomForm();
  2. child1.LogThis += new LogThis(LogThisMethodHandler);
  3.  
  4. MyCustomForm child2 = new MyCustomForm();
  5. child2.LogThis += new LogThis(LogThisMethodHandler);
  6.  
  7. MyCustomForm child3 = new MyCustomForm();
  8. child3.LogThis += new LogThis(LogThisMethodHandler);
  9.  
Now you have 3 new child forms, all inherited from the custom form with the LogThis Event. Your MainForm is subscribed to each child's LogThis event.

When a child yells "LogThis" the parent responds in the LogThisMethodHandler method.

The children only know they need to yell "LogThis". They don't know *how* the parent is going to log it. They don't have to. They are children. Its not their place to tell the parent how to react.

The parent has one reaction to all the children: It logs when they yell.
If you later change your logging to include writing to file in addition to displaying in the richtextbox you only have to make the update in the one method of the parent - not in all the child forms.
Oct 11 '09 #4
l3mon
16
Thank you, now I know what to do just not how.
This is one of the childforms being created.
Expand|Select|Wrap|Line Numbers
  1. private void hashCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
  2.  {
  3.       Form HashCalc = new HashCalc();
  4.       HashCalc.MdiParent = this;
  5.       HashCalc.Show();
  6. }
  7.  
Here is the method I want to use on the mainform to update the richtextbox
Expand|Select|Wrap|Line Numbers
  1.  public void addStatus(string status)
  2.  {
  3.       RtDebug.Text += "\n" + status;
  4.       RtDebug.Select(RtDebug.Text.Length + 1, 2);
  5.       RtDebug.ScrollToCaret();
  6.  }
  7.  
So how would I define the custom event and have it pass the "status" argument? And how do I "shout" from a child form?
Oct 11 '09 #5
tlhintoq
3,525 Expert 2GB
I was actually thinking about writing an "Insights" article on the subject. Much better to have that much info in an article than a single post.

In the mean time Google for "C# custom event" and you will find a ton of articles and how-to blogs on the topic.
Oct 12 '09 #6
l3mon
16
I tried that before posting... but, whatever. Thanks for pointing me in the right direction.
Oct 12 '09 #7
l3mon
16
Wait, I got it. I was defining the delegate and the event in the wrong class/form But thanks again! :)
Oct 12 '09 #8
tlhintoq
3,525 Expert 2GB
I was checking our own site before starting an article on events and guess what? Frinavale already did one.

Amazing what you can find when you look
http://bytes.com/topic/net/insights/...use-events-net
Oct 12 '09 #9
tlhintoq
3,525 Expert 2GB
@l3mon
Yah know... you can eliminate that whole delegate thing too. EventHandler already has its own delegate, so why re-create it? I tend to do my events short and sweet.

I have a namespace I use in all my projects that keeps all my common stuff, like eventargs, registry reading classes and so on. So that's where I keep things like a text args etc.

Expand|Select|Wrap|Line Numbers
  1. namespace tlhintoq
  2. {
  3.     public class TextArgs : EventArgs
  4.     {
  5.         private string szMessage;
  6.         public string Message
  7.         {
  8.             get { return szMessage; }
  9.             set { szMessage = value; }
  10.         }
  11.  
  12.         public TextArgs(string TextMessage)
  13.         {
  14.             szMessage = TextMessage;
  15.         }
  16.     }
  17. }
  18.  


Making the event is then a single line of code.
I use a method to raise the event for safety and convenience sake, and it is only 5 lines if you count the curly braces.
Expand|Select|Wrap|Line Numbers
  1.     public partial class MyForm1 : Form
  2.     {
  3.         public event EventHandler<tlhintoq.TextArgs> LogMessage;
  4.         public void RaiseLog(string Message)
  5.         {
  6.             EventHandler<tlhintoq.TextArgs> temp = LogMessage;
  7.             if (temp != null) temp(null, new tlhintoq.TextArgs(Message));
  8.         }
  9.  
  10.  
  11.         void SomeOtherMethod()
  12.         {
  13.             //  Do a bunch of stuff
  14.  
  15.  
  16.            // Raise the event that will create a log entry
  17.            RaiseLog("SomeOtherMethod completed successfully");
  18.         }
  19.    }
  20.  
Oct 12 '09 #10

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

Similar topics

1
by: Hal Vaughan | last post by:
I have a panel in a GUI that has three components: A JTextArea, a JCheckBox and a JButton. When the button is pressed, I want to check the value of the checkbox and get the text of the textarea...
4
by: Jaydeep | last post by:
Hello, I am facing a strange problem. Problem accessing remote database from ASP using COM+ server application having VB components (ActiveX DLL) installed. Tier 1 : ASP front End (IIS 5.0) Tire...
4
by: Duncan | last post by:
It seems that controls that are in a form's ComponentTray (e.g. ToolTip, ErrorProvider) don't appear on its list of controls (FormName.Controls). Is there any way I can get hold of them?
0
by: jj | last post by:
Try something like this, Assuming I wanted to access all my controls that were of type Button (I don't know what your types called "Thumbs" are, or I'd used those as an example): private void...
1
by: J F | last post by:
Hi all I'm totally new to Visual Studio, coming from Delphi. My question, I'm sure is pretty trivial. I'm using C# I have a component class (Project/Add Class/Component Class). In this...
5
by: RSH | last post by:
I havent been able to set a property from another class with out getting some sort of error. Can someone please tell me what I'm doing wrong here? Public Class Form1
1
by: Miku | last post by:
Hi Guies, I am new to vb.net. In my project I am using vb.net & MySql 4.0.17 as a backend. For database connectivity i have downloaded ByteFX - Mysql .net native provider. I have written the...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
4
by: Lilith | last post by:
This was easy in C++ (well, relatively.) Now I'm in the C# environment and everything I try doesn't work. I'm running a thread in which I have two accesses I need to components on a form. In...
3
by: harvindersingh | last post by:
Hello guys, I am trying to access components of object created suring runtime. Basically the object is a new form and I call it like this: QuickUpdate qu = new QuickUpdate(); ...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.