Communication with controls between classes
-
Hey guys... first I just want to praise The Code Project and everyone on here for basically teaching me the basics of C#. I have a web background and this is the first language I have learned coming off my javascript and vbscript web knowledge. I am sure this question has a very simple answer, but that is an answer I cannot figure out! :) Here it is... I have dummied down my problem into the following code examples. I have a very basic program that takes one richtext field and copies the content into another. I am doing so by calling a method in a separate class that accomplishes the copying of text. Form1.cs:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace TestApp { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; public System.Windows.Forms.RichTextBox txtFromSecondClass; public System.Windows.Forms.RichTextBox txtToSecondClass; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.txtFromSecondClass = new System.Windows.Forms.RichTextBox(); this.label1 = new System.Windows.Forms.Label(); this.txtToSecondClass = new System.Windows.Forms.RichTextBox(); this.label2 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // txtFromSecondClass // this.txtFromSecondClass.Location = new System.Drawing.Point(8, 200); this.txtFromSecondClass.Name = "txtFromSecondClass"; this.t
-
Hey guys... first I just want to praise The Code Project and everyone on here for basically teaching me the basics of C#. I have a web background and this is the first language I have learned coming off my javascript and vbscript web knowledge. I am sure this question has a very simple answer, but that is an answer I cannot figure out! :) Here it is... I have dummied down my problem into the following code examples. I have a very basic program that takes one richtext field and copies the content into another. I am doing so by calling a method in a separate class that accomplishes the copying of text. Form1.cs:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace TestApp { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; public System.Windows.Forms.RichTextBox txtFromSecondClass; public System.Windows.Forms.RichTextBox txtToSecondClass; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.txtFromSecondClass = new System.Windows.Forms.RichTextBox(); this.label1 = new System.Windows.Forms.Label(); this.txtToSecondClass = new System.Windows.Forms.RichTextBox(); this.label2 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // txtFromSecondClass // this.txtFromSecondClass.Location = new System.Drawing.Point(8, 200); this.txtFromSecondClass.Name = "txtFromSecondClass"; this.t
your problem is that you are creating a new instance of form1. You need to pass in the existing instance of form1 transfertext to take the existing instance public static string transfertext(System.Windows.Forms.Form x) { return x.txtFromSecondClass.Text; } usage string value = SecondClass.transfertext(this); best of luck Forever Developing
-
your problem is that you are creating a new instance of form1. You need to pass in the existing instance of form1 transfertext to take the existing instance public static string transfertext(System.Windows.Forms.Form x) { return x.txtFromSecondClass.Text; } usage string value = SecondClass.transfertext(this); best of luck Forever Developing
Hey thanks for the reply! ... and here is the but hehehe ... Can you treat me like a moron, and show me the rewritten methods for each class? I just can't wrap my mind around this... :((
-
your problem is that you are creating a new instance of form1. You need to pass in the existing instance of form1 transfertext to take the existing instance public static string transfertext(System.Windows.Forms.Form x) { return x.txtFromSecondClass.Text; } usage string value = SecondClass.transfertext(this); best of luck Forever Developing
..\..\..\SecondClass.cs(12): 'System.Windows.Forms.Form' does not contain a definition for 'textFromSecondClass' That is what I get when I try to use your code. I guess I have some reading to do. Thanks anyway.