1

My problem is simple. I want to click a panel in Form1, this will cause label1 in a userControl1 which is placed upon form2 to change to "Text".

Clicking this panel would also change the background color of said userControl1. I receive the error "'TileInterFaceTest.Usercontrol1.label1' due to its protection level" which frankly baffles me.

Even running the color change code separately, it simply doesn't achieve the desired result.

To be clear, I'm quite a novice when it comes to C# and programming in general. I've been working with Visual Basic until now so the concept of classes, methods and objects are slightly confusing to me.

Here is my code:

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 TileInterFaceTest
{
    public partial class Form1 : Form
    {
        public UserControl1 userControl1 = new UserControl1();
        public Form2 form2 = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_DoubleClick(object sender, EventArgs e)
        {

        }



        private void panel1_Click(object sender, EventArgs e)
        {
            form2.Show();
            userControl1.BackColor = System.Drawing.Color.Red;
            userControl1.label1 = "Text";
        }


    }
}
BradleyDotNET
  • 57,599
  • 10
  • 90
  • 109

1 Answers1

1

First off, your code as shown doesn't even show the usercontrol on the first form! Thats probably why your color change code didn't do as you expected. Simply writing:

public UserControl1 userControl1 = new UserControl1();

Just declares a public field (bad style!) and sets it to a new instance of your user control. It doesn't put it on the rendered UI.

To fix that part, you need to add it to your form somewhere. Say... in your constructor or the Loaded event:

Controls.Add(userControl1);

Note also that this puts it on Form1. If you want it on Form2, then that form needs to create the control and add it to its Controls collection, and expose it as a property (see below). The other problem is here:

userControl1.label1 = "Text";

Presumably label1 is a label control on that user control. First off, controls are private members of the user control, you can't just access them from somewhere else without exposing them first! You need to add something like this to your user control:

public Label Label1 { get { return label1; } }

Note the use of a property (correct style!). Now you can write this:

userControl1.Label1.Text = "Text";

Note I fixed the final mistake in that line, as you can't set a Label object to a string, it just doesn't make sense. You need to modify its Text property instead.

BradleyDotNET
  • 57,599
  • 10
  • 90
  • 109
  • I greatly appreciate this, the syntax errors have been solved. However, when launching the program, the panel on form1 has disappeared. After adding a button to the form with the same code, the button appears but is visually cut in half, presses it causes the error "An unhandled exception of type 'System.NullReferenceException' occurred in TileInterFaceTest.exe Additional information: Object reference not set to an instance of an object." – ShreddedWheat Mar 13 '15 at 18:13
  • 1
    @ShreddedWheat You'll have to debug that one yourself, as I can't see the code. If I had to guess, the "cut in half" is because your user control is overlaying the panel and buttons. As far as the NRE, read this: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – BradleyDotNET Mar 13 '15 at 18:16