hello.
i created a new form and added to my form few user controls and menu.
my menu contains numbers.
i created all my user controls in the forms constructor and changed their visibility to false.
my goal is to show the number of controls i picked in my menu.
i double clicked a number from the menu and by this created a function for the click in the form.

my problem is that the function does not recognize any of my controls when i try to make them visible.

Recommended Answers

All 7 Replies

Hi there emilio, could you post your code so we can try to help you a little easier?

yes please.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text = "Nim";
            col c1 = new col();
            c1.Location = new Point(10, 25);
            this.Controls.Add(c1);
            c1.Visible = false;
            col c2 = new col();
            c2.Location = new Point(120, 25);
            this.Controls.Add(c2);
            c2.Visible = false;
}

private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            
        }
}

i added user control named col to my form.
i also added menu to my form witch contains numbers between 2 and 7.

in the fuction i want to write something like : c1.Visible = true;

Have you added your custom controls to your project? If not, right click your project and select 'add existing item' then add your controls. hope this helps!

First off, your scoping is wrong.

You have declared this in your constructor:
col c1 = new col();

The milisecond that your constructor exits, c1 is out of scope and ready for the garbage collector.

Declare your components (not initialize them, just declare them) in the class but not inside the constructor or any other method:

public partial class Form1 : Form
    {
        private col c1 = null;

        public Form1()
        {
            InitializeComponent();
            this.Text = "Nim";
            c1 = new col();

// Jerry

commented: Nice work mate! +2

JerryShaw, nice one buddy! Have been trying to solve this one for emilio for a while now lol

ok i got it
thanks to everyone for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.