none
C#-Changing the text of a label on another Form- C#

    Question

  • Hi everbody,
     I am making a program with 2 forms. The first form has a label on it. The second form has a textbox and a button on it. There is a string for the textbox which will allow the text to be changed. The string will then be displayed onto the label when the button is clicked.

    the code i am using is here:
    public string LabelString
    
    
    
    
    
    private void TextApplyButton_Click(object sender, EventArgs e)
    
    {
    
           //change the string text
    
           Txtbox1.Text = LabelString.ToString();
    
           // use the string to change txt of the label
    
           Form1.Label1.Text = LabelString.ToString();
    
    }
    
    


    I am getting an error on "Form1.Label.Text" ....."an object referenve is required for the non-static feild,method or property 'windowsFormApplication1.Form1.Label1' '

    I have tried adding an object for this on the Form2.cs (no change) on the Form1.cs(no change) and all of the other components of the program. Do you guys/girls have any way to fix this?
    Wednesday, August 19, 2009 12:29 AM

Answers

  • Please see [This example is almost identical to what you asked]

    Subscribing to an Event


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Fuingurth Thursday, August 20, 2009 3:55 PM
    Wednesday, August 19, 2009 7:34 PM
  • Would it be possible to do this with multiple labels? possibly by adding changing the names for each one?

    sorry i didnt know where to do that until just befor eyou posted that Rudedog

    Add a new delegate or modify the existing one...

    public delegate void AdviseParentEventHandler(string text, Label label );

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Fuingurth Friday, August 21, 2009 2:21 PM
    Thursday, August 20, 2009 4:20 PM

All replies

  • Step 1 is that you need to make sure that your Label1 has adequate access modifiers.  Click on your Label1 in the designer and look for the Modifiers in the property window.  Change it to Internal (or something more visible -- do not choose Private or Protected).

    Step 2 is to use the access the Label1 through the Form's owner property.  Your application might have more than one instance of Form1 loaded.  For example, think of how a word processor loads multiple documents.  Therefore, just saying Form1 is not enough.

    In Form2:

                Form1 owningForm = (Form1)this.Owner;
                owningForm.Label1.Text = "Your text here.";
    Lastly, make sure that the owner is being set when you create your second form.  Do this by passing an instance of Form1 to Form2's Show or ShowDialog.  Typically the word "this" suffices.

    In Form1:

                Form2 f2 = new Form2();
                f2.Show(this);




    Wednesday, August 19, 2009 12:44 AM
  • Another way only that in this sample when u type in the textbox the label on form1 will update.


    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Sample2
    {
    	/// <summary>
    	/// Class with program entry point.
    	/// </summary>
    	internal sealed class Program
    	{
    		/// <summary>
    		/// Program entry point.
    		/// </summary>
    		[STAThread]
    		private static void Main(string[] args)
    		{
    			Application.EnableVisualStyles();
    			Application.SetCompatibleTextRenderingDefault(false);
    			Application.Run(new Form1());
    		}
    		
    	}
    	
    	public class Form1 : Form
    	{
    		internal Label label1 = new Label();
    		public Form1()
    		{
    			this.Text = "Form1";
    			this.label1.BackColor = Color.White;
    			this.label1.Location = new Point(100, 100);
    			this.Controls.Add(this.label1);
    		}
    		
    		protected override void OnLoad(EventArgs e)
    		{
    			base.OnLoad(e);
    			
    			Form2 form = new Form2();
    			form.Show(this);
    		}
    	}
    	
    	public class Form2 : Form
    	{
    		TextBox textBox1 = new TextBox();
    		
    		public Form2()
    		{
    			this.Text="Form2";
    			this.Controls.Add(this.textBox1);
    			this.textBox1.TextChanged += delegate { this.TextBoxTextChanged(); };
    		}
    		
    		void TextBoxTextChanged()
    		{
    			Form1 form = this.Owner as Form1;
    			form.label1.Text=this.textBox1.Text;
    		}
    	}
    }
    

    Isidro A. López G.
    Wednesday, August 19, 2009 1:07 AM
  • @ Isidro
    That seemed to work, however, when i run it the textbox does not show, i have it set to enabled, visible , etc....
    Wednesday, August 19, 2009 3:39 PM
  • Try another time with this changes:


    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Sample2
    {
    	/// <summary>
    	/// Class with program entry point.
    	/// </summary>
    	internal sealed class Program
    	{
    		/// <summary>
    		/// Program entry point.
    		/// </summary>
    		[STAThread]
    		private static void Main(string[] args)
    		{
    			Application.EnableVisualStyles();
    			Application.SetCompatibleTextRenderingDefault(false);
    			Application.Run(new Form1());
    		}
    		
    	}
    	
    	public class Form1 : Form
    	{
    		internal Label label1 = new Label();
    		public Form1()
    		{
    			this.Text = "Form1";
    			this.label1.BackColor = Color.White;
    			this.label1.Location = new Point(100, 100);
    			this.Controls.Add(this.label1);
    		}
    		
    		protected override void OnLoad(EventArgs e)
    		{
    			base.OnLoad(e);
    			
    			Form2 form = new Form2();
    			form.Show(this);
    		}
    	}
    	
    	public class Form2 : Form
    	{
    		TextBox textBox1 = new TextBox();
    		
    		public Form2()
    		{
    			this.textBox1.Location = new Point(100, 100);
    			this.textBox1.Visible = true;
    			this.Controls.Add(this.textBox1);
    			this.textBox1.TextChanged += delegate { this.TextBoxTextChanged(); };
    		}
    		
    		void TextBoxTextChanged()
    		{
    			Form1 form = this.Owner as Form1;
    			form.label1.Text=this.textBox1.Text;
    		}
    	}
    }

    Isidro A. López G.
    Wednesday, August 19, 2009 6:52 PM
  • Please see [This example is almost identical to what you asked]

    Subscribing to an Event


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Fuingurth Thursday, August 20, 2009 3:55 PM
    Wednesday, August 19, 2009 7:34 PM
  • JohnGrove, I must thank you. That was exactly what I needed.


    In that case give the Moderators a break, please close the thread by marking his reply as the answer.

    The Moderators have the task of finding each and every unanswered post and supplying an answer for it.

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, August 20, 2009 3:45 PM
  • Would it be possible to do this with multiple labels? possibly by adding changing the names for each one?

    sorry i didnt know where to do that until just befor eyou posted that Rudedog
    Thursday, August 20, 2009 3:56 PM
  • Would it be possible to do this with multiple labels? possibly by adding changing the names for each one?

    sorry i didnt know where to do that until just befor eyou posted that Rudedog

    Add a new delegate or modify the existing one...

    public delegate void AdviseParentEventHandler(string text, Label label );

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Fuingurth Friday, August 21, 2009 2:21 PM
    Thursday, August 20, 2009 4:20 PM
  • See this too Thread
    بن بست معنا ندارد یا راهی خواهیم یافت یا راهی خواهیم ساخت Pleasure can be silence , After a deafening din
    Tuesday, September 22, 2009 6:31 PM
  • In your "Form1.designer" file, there are the labels programaticly. You will find in the bottom something like this:

     private System.Windows.Forms.Label label1;

    Just change the private to public, and you will gain access to the lable from another form.

    Thursday, January 10, 2013 3:56 PM