Home>

Text1 and button1 are placed on Form1, label1 is placed on Form2,
When you enter a character in textBox1 of Form1 and press button1,
I want to display the characters entered in the label of Form2.
Currently on the line "form2.label1.Text = textBox1.Text;"
"&Apos;Form2.label1'has inaccessible protection level"
I'm getting an error, but I can't understand it even if I search on the internet.
Would you please? Thank you.

// Fom1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace form_test3 {
   public partial class Form1: Form {
      public Form1 () {
         InitializeComponent ();
      }
      private void button1_Click (object sender, EventArgs e) {
         using (var form2 = new Form2 ()) {
            form2.label1.Text = textBox1.Text;
            form2.Show ();
         }
      }
   }
}

// Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace form_test3 {
   public partial class Form2: Form {
      public Form2 () {
         InitializeComponent ();
      }
   }
}
c#
  • Answer # 1

    Labels placed in the form are usually private and cannot be accessed from outside.
    To manage this, either make it public, or create a property and access it from there

    -Form2--

    public string LabelText {set {Label1.Text = value;} get {return Label1.Text;}}