473,320 Members | 1,876 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,320 software developers and data experts.

Exposing form controls to other classes

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.

Thanks in advance,

Chris
Nov 13 '05 #1
3 14120
Hi Nicholas,

Thanks for your reply. I had realized this early and actually tried writing
a public function to perform the actions as you had mentioned.
public void PostToBox(string msg)

{

richTextBox1.AppendText(msg);

}

However, being new to C#, I'm not sure how to call this function from my
other class (in the same namespace). Am I missing a uses statement or
something like that? It seems that I don't have access to the PostToBox
function when I attempted to call it: Form1.PostToBox("hello");

Thanks for your time and patience,

Chris

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:ek**************@tk2msftngp13.phx.gbl...
Chris,

You can set the access modifier of the rich text box to public, and then it will expose that member to anything that is holding a reference to your
form. However, I generally think that this is bad practice, and you should expose methods on your form that will perform the actions for you.
Something like a SetText method, or something of that nature.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

Thanks in advance,

Chris


Nov 13 '05 #2
Would you happen to have an example of how to make the object available
through a static property on a type?
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:uJ**************@TK2MSFTNGP10.phx.gbl...
Chris,

In VB6, an instance of the form with the same name as the type of the
form was always available. This is not the case in .NET. In .NET, a form
is just like any other object. If you want your objects to have access to
your form, then you have to pass it to them, or make it available through
something like a static property on a type.
"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Nicholas,

Thanks for your reply. I had realized this early and actually tried

writing
a public function to perform the actions as you had mentioned.
public void PostToBox(string msg)

{

richTextBox1.AppendText(msg);

}

However, being new to C#, I'm not sure how to call this function from my
other class (in the same namespace). Am I missing a uses statement or
something like that? It seems that I don't have access to the PostToBox
function when I attempted to call it: Form1.PostToBox("hello");

Thanks for your time and patience,

Chris

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com>

wrote
in message news:ek**************@tk2msftngp13.phx.gbl...
Chris,

You can set the access modifier of the rich text box to public,
and
then
it will expose that member to anything that is holding a reference to your form. However, I generally think that this is bad practice, and you

should
expose methods on your form that will perform the actions for you.
Something like a SetText method, or something of that nature.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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.
>
> Thanks in advance,
>
> Chris
>
>



Nov 13 '05 #3
As others have said, once you see that a form is just a class, then anything
is possible and you will see many ways to do these kind of things. For
example, to have bidirectional links between two forms (say the MainForm and
a LogForm) can be done a couple ways.
1) MainForm - As you probably want to keep the log form open but hidden all
the time, you can add a private var to your declaration section in MainForm.
Then in the load method, create an instance of the LogForm for the rest of
MainForm (or others) to use. Now add a Public property (or public method)
to LogForm that returns a ref (i.e. getter) to the richtextbox on the
LogForm. Now MainForm has everything it needs. It has a ref to LogForm and
can "see" the richtextbox to append some text. It is probably better to use
a Public method in LogForm to do this as you "contain" more logic in LogForm
that way and handle errors, returns and don't expose your raw control to
MainForm which could help reduce problems along the way.
2) So now you can start LogForm from MainFrom and append text as you need.
But what if LogForm needs to check a public field or public property in
MainForm to see if something is set or not (i.e. an AllowClearLog flag or
something else set in an options dialog.) Or maybe it too needs to append
some text to a richtextbox in main after it does something usefull. LogForm
is constructed and running, but it does not now anything about MainForm and
has no reference to it. For this, standard class constructors can help. As
a Form is first a class like any other, you can instantiate a form using a
constructor and passing in values or ref types. This is what we need and
how LogForm will know how to ref back to MainForm to see Public Properties
or public fields or public methods. So in MainForm, when you load the
instance of the LogForm, do it this way "logForm = new LogForm(this);".
This passes the ref to MainForm to the LogForm(MainForm mainForm)
constructor. You need to add/change the LogForm(MainForm mainForm)
constructor in LogForm. Remember to use "MainForm" type and not "Form" or
"Object" as you will have to down-cast Form to MainForm anyway in your code
to "see" MainForm's public properties.

Now MainForm can see LogForm and visa-versa. When you see the linkages, you
can use this for many kinds of inter-form/class communications. Here is an
example:

MainForm
======
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ReferenceBetweenForms
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnShowLog;
private System.Windows.Forms.RichTextBox richTextBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private LogForm logForm; //private var to hold common reference to
LogForm.

public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnShowLog = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// btnShowLog
//
this.btnShowLog.Location = new System.Drawing.Point(224, 232);
this.btnShowLog.Name = "btnShowLog";
this.btnShowLog.TabIndex = 0;
this.btnShowLog.Text = "Show Log";
this.btnShowLog.Click += new System.EventHandler(this.btnShowLog_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(24, 32);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 152);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(320, 273);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.btnShowLog);
this.Name = "MainForm";
this.Text = "Main";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

private void MainForm_Load(object sender, System.EventArgs e)
{
logForm = new LogForm(this);
}

private void btnShowLog_Click(object sender, System.EventArgs e)
{
logForm.LogFormRichText.AppendText("Main Form Showed LogForm and appended
this text.\n");
logForm.LogThis("Main Form appended this text using Public Method.\n");
logForm.Show();
}

public RichTextBox MainFormRichText
{
get { return this.richTextBox1; }
}
} //End MainForm Class
}

LogForm
===========
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ReferenceBetweenForms
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class LogForm : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox tbParent;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnOK;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnClearLog;
private MainForm mainForm;

public LogForm(MainForm parentForm)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
mainForm = parentForm; //Set private var "mainForm" so logger always has
reference to mainform.
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.tbParent = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.btnClearLog = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 40);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(272, 168);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// tbParent
//
this.tbParent.Location = new System.Drawing.Point(64, 8);
this.tbParent.Name = "tbParent";
this.tbParent.TabIndex = 1;
this.tbParent.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 2;
this.label1.Text = "Parent:";
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(200, 248);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 3;
this.btnOK.Text = "OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnClearLog
//
this.btnClearLog.Location = new System.Drawing.Point(200, 216);
this.btnClearLog.Name = "btnClearLog";
this.btnClearLog.TabIndex = 4;
this.btnClearLog.Text = "Clear";
this.btnClearLog.Click += new
System.EventHandler(this.btnClearLog_Click);
//
// LogForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 277);
this.Controls.Add(this.btnClearLog);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.label1);
this.Controls.Add(this.tbParent);
this.Controls.Add(this.richTextBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "LogForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParen t;
this.Text = "Log";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.LogF orm_Closing);
this.Load += new System.EventHandler(this.LogForm_Load);
this.ResumeLayout(false);

}
#endregion

private void btnOK_Click(object sender, System.EventArgs e)
{
this.Hide();
}

private void LogForm_Load(object sender, System.EventArgs e)
{
mainForm.MainFormRichText.AppendText("LogForm was loaded and appended
this text to MainForm using reference passed to LogForm() constructor.");
this.tbParent.Text = mainForm.Name;
}

public void LogThis(string logText)
{
//One option is to use a public method to log text.
//This is probably preferred as you "contain" all append and error
//logic in the class where it belongs (i.e. the LogForm.)
this.richTextBox1.AppendText(logText);
}

private void btnClearLog_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Clear();
}

private void LogForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}

public RichTextBox LogFormRichText
{
//Another option is to use a public Property to return a ref. to
//the RichTextBox and let MainForm logic append.
get { return this.richTextBox1; }
}
}
}

HTH
--
William Stacey, DNS MVP
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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. ...
4
by: Adam Clauss | last post by:
This may be more of a Visual Studio question than a C# question, but it came up within the context of a C# app, so here it is. In a Windows Form or a Web Form application, you can drag various...
8
by: Tim Geiges | last post by:
Since I am being challenged with learning c# I figured I could pass some of the pain on to you guys :-) I have another question(this one is important for me to fix before I can get my app to Beta)...
3
by: Dave | last post by:
Please - anyone that can help. I am getting confusing results while trying to expose a collection from a web service. I have a webservice in which a web method accepts a collection as a...
2
by: Yuk Tang | last post by:
I've satisfactorily got an axwebbrowser control on a form within a panel, suitably positioned and sized, and now I want to display a webpage on it. This is not normally a problem when I have the...
9
by: Dimsion | last post by:
Hi, How do i expose all my forms and it controls to other form in the project? I want to be able to add a form and some control on it, this then be available to all other forms. form1 click...
2
by: Rich | last post by:
Greetings, I observed that in VB2005 the designer generated code is hidden. The initialize code appears to be stored in a different module than the Form's class module. But the form's class...
4
by: Toze | last post by:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to list all forms in my apllication and list all controls (ex: txtname;btnname) inside of each form.
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.