Hello,

I am training as a future software developper, so maybe my problem can be solved differently, but for education the following problem is the problem (and not some code with a specific aim).

Have have an application (a Mastermind clone) in Windows Forms mode.
Everything is inside of different UserControls wich contain a variety of components (as labels, buttons etc.)

I can navigate between the UserControls that all have the same MindMasterForm as parent. And now I finally have another form (MindMasterColourForm), from which I want to set the properties of any component on the active UserControlGame on the MindMasterForm.

I can not access the components of UserControlGame from within MindMasterColourForm.

UserControlGame

public partial class UserControlGame : UserControl
    {

        public UserControlGame()
        {
            InitializeComponent();

            for (int i = 0; i < GlobalVariables.configsetup[0]; i++)
            {
                SlotField[i] = new System.Windows.Forms.Panel();
                SlotContainer[i] = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
                SlotForm[i] = new Microsoft.VisualBasic.PowerPacks.OvalShape();

                flowLayoutPanel1.Controls.Add(SlotField[i]);


                SlotField[i].Controls.Add(SlotContainer[i]);
                SlotField[i].Margin = new System.Windows.Forms.Padding(5);
                SlotField[i].Name = "SlotField" + (i + 1).ToString();
                SlotField[i].Size = new System.Drawing.Size(50, 50);
                SlotField[i].TabIndex = 0;
                SlotField[i].TabStop = false;
                SlotContainer[i].Location = new System.Drawing.Point(0, 0);
                SlotContainer[i].Margin = new System.Windows.Forms.Padding(0);
                SlotContainer[i].Name = "SlotContainer" + (i + 1).ToString();
                SlotContainer[i].Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] { SlotForm[i] });
                SlotContainer[i].Size = new System.Drawing.Size(50, 50);
                SlotContainer[i].TabIndex = 0;
                SlotContainer[i].TabStop = false;
                SlotForm[i].BorderColor = System.Drawing.Color.Black;
                SlotForm[i].BorderWidth = 7;
                SlotForm[i].Enabled = false;
                SlotForm[i].FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
                SlotForm[i].FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;
                SlotForm[i].Location = new System.Drawing.Point(5, 5);
                SlotForm[i].Name = "SlotForm" + (i + 1).ToString();
                SlotForm[i].Size = new System.Drawing.Size(33, 33);
            }
           
        }
        MindMasterColourForm ColourForm = new MindMasterColourForm();

        System.Windows.Forms.Panel[] SlotField = new System.Windows.Forms.Panel[GlobalVariables.configsetup[0]];
        Microsoft.VisualBasic.PowerPacks.ShapeContainer[] SlotContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer[GlobalVariables.configsetup[0]];
        Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm = new Microsoft.VisualBasic.PowerPacks.OvalShape[GlobalVariables.configsetup[0]];

        private void buttonSetUp_Click(object sender, EventArgs e)
        {
            Hide();
            BasicFunctions.setuppage();
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            Hide();
            ColourForm.Close();
            BasicFunctions.startpage();
        }

        private void UserControlGame_Load(object sender, EventArgs e)
        {
            ColourForm.Location = new Point(this.Parent.Location.X + 521, this.Parent.Location.Y + 383);
            ColourForm.Show();


            GlobalVariables.position = new int[GlobalVariables.configsetup[2] + 1, GlobalVariables.configsetup[0] + 2];
            int[] colorvec;
            colorvec = new int[GlobalVariables.configsetup[1]];
            Random rand = new Random();



        }

    }

MindMasterColourForm

{
        public MindMasterColourForm()
        {
            InitializeComponent();
        }

        private void MindMasterColourForm_Load(object sender, EventArgs e)
        {

        }


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


    }

Using button1_Click I want to change the properties of; say, SlotForm.BorderColor?

How can I do that?

Recommended Answers

All 21 Replies

ok, first thing you need to do is set all the controls you need access to from the outside as properties.

example

public Panel thepanel
{
get
{
return mypanel;
}
set
{
mypanel = value;
}
}

this will let objects see your controls and access them easily.

then all you have to do is pass a reference to the usercontrol to the constructor of the color form like so

MindMasterColourForm ColourForm = new MindMasterColourForm(this);

then catch that in the constructor of the mindmastercolourform

UserControlGame ucg;
        public MindMasterColourForm(UserControlGame ucgParam)
        {
            InitializeComponent();

           ucg = ucgParam;
        }

then when you need a control from the colourform you can access it via its property like so

ucg.thepanel.Location.X;
//or whatever you want to do with it.

Okay, let me try to see what I may have understood correctly. :$

In my UserControlGame code I change line 41 to include this between the parentheses. And I'll add the code you've started with as, for example:

public Microsoft.VisualBasic.PowerPacks.OvalShape SlotForm[1]
{
     get
     {
          return myovalshape;
     }
     set
     {
          myovalshape = value;
     }
}

(Not sure about the red part, where does myovalshape - your mypanel - come from?)

And I'll add

UserControlGame ucg;
public MindMasterColourForm(UserControlGame ucgParam)
{
     InitializeComponent();
     ucg = ucgParam;
}

as well.

And in my MindMasterColourForm code I'd add eventually inside the button1_Click:

private void button1_Click(object sender, EventArgs e)
{
     ucg.SlotForm[1].FillColor = System.Drawing.Color.Red;
}

I can't try this right now (I did not copy the code to my stick), which gives you time to verify my code. I am absolutely not unsure in which class what code belongs (also because some test code used to explain this is likely incomplete, well, it doesn't function when I try to compile it; I am using Microsoft Visual Studio 2008, btw).

In parts I've already tried this solution, but since I just don't understand a) what the code public MindMasterColourForm(UserControlGame ucgParam) is supposed to do (i.e, to does he do), b) where each part of the code has to be placed, and c) what does the get do inside of public Microsoft.VisualBasic.PowerPacks.OvalShape SlotForm[1]? Return mypanel/myovalshape? What is it? :confused:

I hope I can get my tutor tomorrow to explain this, but I'd still appreciate any help I can get here!

Apparently up on terms of properties, constructors, and passing references...

you almost got it. the first code chuck i presented was an example of how to set a private control accesible by a public property, you would need to substitute in the type and private control you wanted to expose, in the example I used type of panel, and a non existant panel called mypanel.

the 2nd 2 were actually cut and paste working code.
since in your usergameControl you created the colorform, you tell it which instance of the usergamecontrol you want to access.

in the last codebit I created a variable of type usergamecontorl to hold the reference of the instance to the control. and the public MindMasterColourForm(UserControlGame ucgParam) part is where you capture that "(this)" reference and then its set to that variable we created.

now ugc is a variable that refers to that usergamecontrol instance in all of your color form, so you can access all the public properties using ugc.Proptertynamehere and you set the controls you need availabe using the first code block I posted about creating public properties...

sorry I can't explain better I am actually in a big hurry... best luck and happy coding.

Okay, my MindMasterColourForm class looks like this:

public partial class MindMasterColourForm : Form
    {
        UserControlGame ucg;
        public MindMasterColourForm(UserControlGame ucgParam)
        {
            InitializeComponent();
            ucg = ucgParam;
        }

        private void MindMasterColourForm_Load(object sender, EventArgs e)
        {

        }


        private void button1_Click(object sender, EventArgs e)
        {
            ucg.SlotForm[1].FillColor = System.Drawing.Color.Red;
        }


    }

And more or less I understand what it does (maybe not in exact terms):
UserControlGame ucg; will declare UserControlGame within this form. And then it will initialize the form with an parameter that will be defined in the UserControlGame class as MindMasterColourForm ColourForm = new MindMasterColourForm(this);; Unfortunately there we have the first problem. Visual Studio says that the keyword this can not be used in the context. (My VS2008 is in French, and the full error description is "Le mot clé 'this' n'est pas disponible dans le contexte actuel.")

Then I tried to use the public get set code snippet:

public Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm
        {
            get
            {
                return myovalshape;
            }
            set
            {
                myovalshape = value;
            }
        }

But then VS2008 states two errata.

First, there is an ambiguity as I have defined MindMasterGUI.UserControlGame.SlotForm twice. And myovalshape doesn't exist in this context, as it has never been declared/created.

Btw, I tried several different forms, even though they did seem wrong all along:
public Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm[]
public Microsoft.VisualBasic.PowerPacks.OvalShape SlotForm
public Microsoft.VisualBasic.PowerPacks.OvalShape SlotForm[]

and SlotForm, SlotForm[] and SlotForm[0] instead of myovalshape.
Useless to say that neither worked.


So, how can I join the two declaration of the SlotForm?
And what would be myovershape?

public partial class UserControlGame : UserControl
    {

        public UserControlGame()
        {
            InitializeComponent();

            for (int i = 0; i < GlobalVariables.configsetup[0]; i++)
            {
                SlotField[i] = new System.Windows.Forms.Panel();
                SlotContainer[i] = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
                [B]SlotForm[/B][i] = new Microsoft.VisualBasic.PowerPacks.OvalShape();

                flowLayoutPanel1.Controls.Add(SlotField[i]);

                SlotField[i].Controls.Add(SlotContainer[i]);
                SlotField[i].Margin = new System.Windows.Forms.Padding(5);
                SlotField[i].Name = "SlotField" + (i + 1).ToString();
                SlotField[i].Size = new System.Drawing.Size(50, 50);
                SlotField[i].TabIndex = 0;
                SlotField[i].TabStop = false;
                SlotContainer[i].Location = new System.Drawing.Point(0, 0);
                SlotContainer[i].Margin = new System.Windows.Forms.Padding(0);
                SlotContainer[i].Name = "SlotContainer" + (i + 1).ToString();
                SlotContainer[i].Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] { SlotForm[i] });
                SlotContainer[i].Size = new System.Drawing.Size(50, 50);
                SlotContainer[i].TabIndex = 0;
                SlotContainer[i].TabStop = false;
                SlotForm[i].BorderColor = System.Drawing.Color.Black;
                SlotForm[i].BorderWidth = 7;
                SlotForm[i].Enabled = false;
                SlotForm[i].FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
                SlotForm[i].FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;
                SlotForm[i].Location = new System.Drawing.Point(5, 5);
                SlotForm[i].Name = "SlotForm" + (i + 1).ToString();
                SlotForm[i].Size = new System.Drawing.Size(33, 33);

            }
           
        }
        MindMasterColourForm ColourForm = new MindMasterColourForm([B]this[/B]);

        System.Windows.Forms.Panel[] SlotField = new System.Windows.Forms.Panel[GlobalVariables.configsetup[0]];
        Microsoft.VisualBasic.PowerPacks.ShapeContainer[] SlotContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer[GlobalVariables.configsetup[0]];
        Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm = new Microsoft.VisualBasic.PowerPacks.OvalShape[GlobalVariables.configsetup[0]];

        public Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm
        {
            get
            {
                return [B]myovalshape[/B];
            }
            set
            {
                [B]myovalshape[/B] = value;
            }
        }


        private void buttonSetUp_Click(object sender, EventArgs e)
        {
            Hide();
            BasicFunctions.setuppage();
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            Hide();
            ColourForm.Close();
            BasicFunctions.startpage();
        }

        private void UserControlGame_Load(object sender, EventArgs e)
        {
            ColourForm.Location = new Point(this.Parent.Location.X + 521, this.Parent.Location.Y + 383);
            ColourForm.Show();

        }

    }

P.S. I have put every bit of code marked with an error by VS2008 in red.

And finally I tried one change:

public Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm = new Microsoft.VisualBasic.PowerPacks.OvalShape[GlobalVariables.configsetup[0]];

And no public/get/set declaration. The only error that remains is the this in MindMasterColourForm ColourForm = new MindMasterColourForm(this); - so I can even try debugging the code.

Can you upload your project? You can .ZIP it and attach it to the thread by clicking the "Go Advanced" button.

I wouldn't mind to know how to do it the way Diamonddrake suggested, if only to learn.

But the solution was too simple. None of Diamonddrake's code is need, just when declaring the SlotForm it has to be done public AND static (as the focus changes between different forms).

System.Windows.Forms.Panel[] SlotField = new System.Windows.Forms.Panel[GlobalVariables.configsetup[0]];
        Microsoft.VisualBasic.PowerPacks.ShapeContainer[] SlotContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer[GlobalVariables.configsetup[0]];
        [B]public static[/B] Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm = new Microsoft.VisualBasic.PowerPacks.OvalShape[GlobalVariables.configsetup[0]];

If you're still interested in the project, tell me!

A great Thank You to everyone who tried to help.

:)

Okay, I still would like to know whether there is a way to access the component SlotForm without duplicate declaration and without making it static?

// inside your UserControlGame (note I changed your array to begin with lowercase s char)
        Microsoft.VisualBasic.PowerPacks.OvalShape[] slotForm = new Microsoft.VisualBasic.PowerPacks.OvalShape[GlobalVariables.configsetup[0]];

        public Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm
        {
            get
            {
                return slotForm; // returns ref to your array
            }
            set
            {
                slotForm = value; // sets your array to a new ref
            }
        }

// usage from within MindMasterColourForm class

        Microsoft.VisualBasic.PowerPacks.OvalShape ovalShape = ugc.SlotForm[i]; // access one of the array items...

make a public property of type UserControl, and return your private member user control, so you will have all sorts of access to your UserControl members.

Okay, thanks for helping.

So, separating slotForm and SlotForm seems clear enough, but the Microsoft.VisualBasic.PowerPacks.OvalShape ovalShape = ugc.SlotForm; code...

First, ugc indicates that you still want Diamonddrake's code

UserControlGame ucg;
public MindMasterColourForm(UserControlGame ucgParam)
{
        InitializeComponent();
        ucg =  = ucgParam;
}

But that brings back the this in MindMasterColourForm ColourForm = new MindMasterColourForm(this); which doesn't work.

Second, I don't get it. Creating ovalShape will serve what purpose? After all, the idea is to attribute a value to ucg.SlotForm.SomeProperty.

As MindMasterColourForm ColourForm = new MindMasterColourForm(this); doesn't function, I have sofar left any code connected with it, which will give the "An object reference is required for the non-static field, method, or property 'MindMasterGUI.UserControlGame.SlotForm.get'" error message on UserControlGame.SlotForm.SomeProperty.

I cannot tell from the code statements why you are getting the error. I suggest you zip up the project and attach.

commented: i agree +10

Sorry, I don't have much time to reply right now. but the reason that the "this" is throwing an error is because using "this" in the context I provided needs to be within a function and not directly in the class.

so you would have to create a variable to hold the instance directly in the class

MindMasterColourForm ColourForm;

then inside a method of some sort, for example the load event of the usergamecontrol create the instance and pass the "this" argument

ColourForm = new MindMasterColourForm(this);

sorry for the confusion. if you are still having trouble I will come back later and try to explain better and maybe post an example project to show you how it could work.

YES! Now it seems to function properly!

Here the code that did the trick.

The UserControl that features the components whose properties I want to change:

public partial class UserControlGame : UserControl
    {

        public UserControlGame()
        {
            InitializeComponent();

            for (int i = 0; i < GlobalVariables.configsetup[0]; i++)
            {
                SlotField[i] = new System.Windows.Forms.Panel();
                SlotContainer[i] = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
                SlotForm[i] = new Microsoft.VisualBasic.PowerPacks.OvalShape();

                flowLayoutPanel1.Controls.Add(SlotField[i]);

                SlotField[i].Controls.Add(SlotContainer[i]);
                SlotField[i].Margin = new System.Windows.Forms.Padding(5);
                SlotField[i].Name = "SlotField" + (i + 1).ToString();
                SlotField[i].Size = new System.Drawing.Size(50, 50);
                SlotField[i].TabIndex = 0;
                SlotField[i].TabStop = false;
                SlotContainer[i].Location = new System.Drawing.Point(0, 0);
                SlotContainer[i].Margin = new System.Windows.Forms.Padding(0);
                SlotContainer[i].Name = "SlotContainer" + (i + 1).ToString();
                SlotContainer[i].Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] { SlotForm[i] });
                SlotContainer[i].Size = new System.Drawing.Size(50, 50);
                SlotContainer[i].TabIndex = 0;
                SlotContainer[i].TabStop = false;
                SlotForm[i].BorderColor = System.Drawing.Color.Black;
                SlotForm[i].BorderWidth = 6;
                SlotForm[i].Enabled = false;
                SlotForm[i].FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
                SlotForm[i].FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;
                SlotForm[i].Location = new System.Drawing.Point(5, 5);
                SlotForm[i].Name = "SlotForm" + (i + 1).ToString();
                SlotForm[i].Size = new System.Drawing.Size(34, 34);

            }
           
        }
        [B]MindMasterColourForm ColourForm;[/B]

        System.Windows.Forms.Panel[] SlotField = new System.Windows.Forms.Panel[GlobalVariables.configsetup[0]];
        Microsoft.VisualBasic.PowerPacks.ShapeContainer[] SlotContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer[GlobalVariables.configsetup[0]];
        [B]Microsoft.VisualBasic.PowerPacks.OvalShape[] slotForm = new Microsoft.VisualBasic.PowerPacks.OvalShape[GlobalVariables.configsetup[0]];[/B]

        [B]public Microsoft.VisualBasic.PowerPacks.OvalShape[] SlotForm
        {
            get
            {
                return slotForm; // returns ref to your array
            }
            set
            {
                slotForm = value; // sets your array to a new ref
            }
        }[/B]


        private void buttonSetUp_Click(object sender, EventArgs e)
        {
            Hide();
            ColourForm.Close();
            BasicFunctions.setuppage();
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            Hide();
            ColourForm.Close();
            BasicFunctions.startpage();
        }

        private void UserControlGame_Load(object sender, EventArgs e)
        {
            [B]ColourForm = new MindMasterColourForm(this);[/B]
            ColourForm.Location = new Point(this.Parent.Location.X + 521, this.Parent.Location.Y + 381);
            ColourForm.Show();


            GlobalVariables.position = new int[GlobalVariables.configsetup[2] + 1, GlobalVariables.configsetup[0] + 2];
            int[] colorvec;
            colorvec = new int[GlobalVariables.configsetup[1]];
            Random rand = new Random();

        }

    }

And now the Form from which I want to control the properties:

public partial class MindMasterColourForm : Form
    {
        [B]UserControlGame ucg;[/B]
        [B]public MindMasterColourForm(UserControlGame ucgParam)[/B]
        {
            InitializeComponent();
            [B]ucg = ucgParam;[/B]

            for (int i = 0; i < GlobalVariables.configsetup[1]; i++)
            {
                ColourButton[i] = new System.Windows.Forms.Button();
                this.buttonClear = new System.Windows.Forms.Button();

                ColourButton[i].Parent = this;
                ColourButton[i].BackColor = System.Drawing.Color.FromArgb(BasicFunctions.SetColour(i + 1)[0], BasicFunctions.SetColour(i + 1)[1], BasicFunctions.SetColour(i + 1)[2]);
                if (i > GlobalVariables.configsetup[1]/2) ColourButton[i].Location = new System.Drawing.Point(20 + (i - 1 - GlobalVariables.configsetup[1] / 2) * 58, 62);
                else ColourButton[i].Location = new System.Drawing.Point(20 + i * 58, 20);
                ColourButton[i].Margin = new System.Windows.Forms.Padding(10);
                ColourButton[i].Name = (i + 1).ToString();
                ColourButton[i].Size = new System.Drawing.Size(48, 32);
                ColourButton[i].TabIndex = 0;
                ColourButton[i].Text = (i + 1).ToString();
                ColourButton[i].UseVisualStyleBackColor = false;
                ColourButton[i].Click += new System.EventHandler(ColourClicker_Click);
                this.Controls.Add(ColourButton[i]);

                buttonClear.Parent = this;
                buttonClear.Location = new System.Drawing.Point(83 + ((GlobalVariables.configsetup[1] / 2)) * 58, 20);
                buttonClear.Margin = new System.Windows.Forms.Padding(5, 20, 20, 10);
                buttonClear.Name = "buttonClear";
                buttonClear.Size = new System.Drawing.Size(48, 84);
                buttonClear.TabIndex = 1;
                buttonClear.Text = "Clear";
                buttonClear.UseVisualStyleBackColor = true;
                buttonClear.Click += new System.EventHandler(buttonClear_Click);

                ClientSize = new System.Drawing.Size(151 + ((GlobalVariables.configsetup[1] / 2)) * 58, 124);

            }
        }

        System.Windows.Forms.Button[] ColourButton = new System.Windows.Forms.Button[GlobalVariables.configsetup[1]];

        private void MindMasterColourForm_Load(object sender, EventArgs e)
        {
            GlobalVariables.colorinputcounter = 0;
        }


        private void ColourClicker_Click(object sender, EventArgs e)
        {
            GlobalVariables.tempcolorinput[GlobalVariables.colorinputcounter] = int.Parse(((Button)sender).Text);
            [B]ucg.[/B]SlotForm[GlobalVariables.colorinputcounter].FillColor = System.Drawing.Color.FromArgb(BasicFunctions.SetColour(int.Parse(((Button)sender).Text))[0], BasicFunctions.SetColour(int.Parse(((Button)sender).Text))[1], BasicFunctions.SetColour(int.Parse(((Button)sender).Text))[2]);
            [B]ucg.[/B]SlotForm[GlobalVariables.colorinputcounter].BorderWidth = 2;
            [B]ucg.[/B]SlotForm[GlobalVariables.colorinputcounter].Location = new System.Drawing.Point(3, 3);
            [B]ucg.[/B]SlotForm[GlobalVariables.colorinputcounter].Size = new System.Drawing.Size(38, 38);

            if (GlobalVariables.colorinputcounter < GlobalVariables.configsetup[0] - 1)
            {
                GlobalVariables.colorinputcounter += 1;
            }
            else
            {
                for (int i = 0; i < GlobalVariables.configsetup[1]; i++)
                {
                    ColourButton[i].Enabled = false;
                }
            }

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GlobalVariables.configsetup[0]; i++)
            {
                GlobalVariables.tempcolorinput[i] = 0;
                [B]ucg.[/B]SlotForm[i].FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
                [B]ucg.[/B]SlotForm[i].BorderWidth = 6;
                [B]ucg.[/B]SlotForm[i].Location = new System.Drawing.Point(5, 5);
                [B]ucg.[/B]SlotForm[i].Size = new System.Drawing.Size(34, 34);
            }
            for (int i = 0; i < GlobalVariables.configsetup[1]; i++)
            {
                ColourButton[i].Enabled = true;
            }

            GlobalVariables.colorinputcounter = 0;
        }


    }

The advantage over the much simpler way of declaring SlotForm in the UserControlGame once as public static is that the array Microsoft.VisualBasic.PowerPacks.OvalShape[] slotForm = new Microsoft.VisualBasic.PowerPacks.OvalShape[x]; can have a variable size, i.e., x is a variable, while initializing it as static it won't change during runtime. That means, even if x is a variable, the size (Length) of the array will keep it's initial value, even when x changes. (At least that's what I have seen so far. Correct me if this is wrong.)

setting any variables as public is considered no longer acceptable in 3.5. because in theory the variable could be attempted to be read from or written to at the same time crashing the app, now you can still do it, the get and set methods are built into .net that let the variable call check if its beeing accessed and wait if it is.

also, setting an objects reference to static is used so that if multiple usergamecontrol objects existed, that object would have the same values throughout all the instances. it should only be used if multiple objects of the same type are expected to always have the same value as the other instances... as far as I can tell, no a static reference isn't a good idea here. just set it to public should be enough, and a property to expose it would technically do the same thing, just be more modern object oriented programming.

glad you got it working!

Best of luck.

commented: good follow up +3

Thank and until the next (which won't be long).

I hope putting my code and your explanation here might help others to one day.

Well, I was certain I'd come back for more.

So far it has worked out beautifully, but now I found an addendum.

In the problem so far I wanted to set the values (properties) of variables (components) of UserControlGame from within MindMasterColourForm which has been created by (and at the same time with) UserControlGame.

So the code...

MindMasterColourForm ColourForm;
private void UserControlGame_Load(object sender, EventArgs e)
{
        ColourForm = new MindMasterColourForm(this);
}

... is doing exactly what it should do.
On creating MindMasterColourForm it has already a sort of reference to UserControlGame (as a parameter).

But now I want to create another Form, say MindMasterResultForm, that will only be shown after clicking on some buttons on UserControlGame, AND (the first part is easy) control its components from within UserControlGame.

Applying the same general code that has worked so wonderfully on the UserControlGame/MindMasterColourForm connection would require that UserControlGame would be initiated like this too...

UserControl MainPage = new UserControlGame(this);

As you can see, being a UserControl and not a Form already changes the code. And this time this can not work as the code does not appear in either class. (Actually UserControlGame is created using a public static function that is called by either one of two separate UserControls that exist before UserControlGame.)

public static void gamepage()
{
        UserControl MainPage = new UserControlGame();
        MainPage.Parent = MindMasterForm.ActiveForm;
        MainPage.Show();
}

Again, I don't want to use the public static declaration as I want to use variables. And again, the idea is not better code but to understand how to do that.

I guess I found the answer myself and it's quite simple.

The form to access was created using MindMasterResultForm ResultForm = new MindMasterResultForm(); and so it's components can be access by using ResultForm.ComponentsName.

I guess I found the answer myself and it's quite simple.

The form to access was created using MindMasterResultForm ResultForm = new MindMasterResultForm(); and so it's components can be access by using ResultForm.ComponentsName.

I have twice marked this thread as solved and had to unmark it later as some problems came up. This time I wanted to be sure until I mark it again. But now it is okay.

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.