Control of UserControl components from another form

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Control of UserControl components from another form

 
0
  #11
Sep 24th, 2009
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.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: Alba Ra is an unknown quantity at this point 
Solved Threads: 0
Alba Ra Alba Ra is offline Offline
Light Poster

Re: Control of UserControl components from another form

 
0
  #12
Sep 24th, 2009
Okay, thanks for helping.

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

First, ugc indicates that you still want Diamonddrake's code
  1. UserControlGame ucg;
  2. public MindMasterColourForm(UserControlGame ucgParam)
  3. {
  4. InitializeComponent();
  5. ucg = = ucgParam;
  6. }

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[i].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[i].SomeProperty.
Alba Ra
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 914
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 146
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Control of UserControl components from another form

 
1
  #13
Sep 24th, 2009
I cannot tell from the code statements why you are getting the error. I suggest you zip up the project and attach.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 328
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: Control of UserControl components from another form

 
0
  #14
Sep 24th, 2009
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

  1. 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

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: Alba Ra is an unknown quantity at this point 
Solved Threads: 0
Alba Ra Alba Ra is offline Offline
Light Poster

Re: Control of UserControl components from another form

 
0
  #15
Sep 24th, 2009
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);

            }
           
        }
        MindMasterColourForm ColourForm;

        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 slotForm; // returns ref to your array
            }
            set
            {
                slotForm = value; // sets your array to a new ref
            }
        }


        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)
        {
            ColourForm = new MindMasterColourForm(this);
            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
    {
        UserControlGame ucg;
        public MindMasterColourForm(UserControlGame ucgParam)
        {
            InitializeComponent();
            ucg = ucgParam;

            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);
            ucg.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]);
            ucg.SlotForm[GlobalVariables.colorinputcounter].BorderWidth = 2;
            ucg.SlotForm[GlobalVariables.colorinputcounter].Location = new System.Drawing.Point(3, 3);
            ucg.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;
                ucg.SlotForm[i].FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
                ucg.SlotForm[i].BorderWidth = 6;
                ucg.SlotForm[i].Location = new System.Drawing.Point(5, 5);
                ucg.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.)
Alba Ra
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 328
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: Control of UserControl components from another form

 
1
  #16
Sep 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: Alba Ra is an unknown quantity at this point 
Solved Threads: 0
Alba Ra Alba Ra is offline Offline
Light Poster

Re: Control of UserControl components from another form

 
0
  #17
Sep 24th, 2009
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.
Alba Ra
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: Alba Ra is an unknown quantity at this point 
Solved Threads: 0
Alba Ra Alba Ra is offline Offline
Light Poster

Re: Control of UserControl components from another form

 
0
  #18
Sep 25th, 2009
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...
  1. MindMasterColourForm ColourForm;
  2. private void UserControlGame_Load(object sender, EventArgs e)
  3. {
  4. ColourForm = new MindMasterColourForm(this);
  5. }
... 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...
  1. 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.)
  1. public static void gamepage()
  2. {
  3. UserControl MainPage = new UserControlGame();
  4. MainPage.Parent = MindMasterForm.ActiveForm;
  5. MainPage.Show();
  6. }

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.
Alba Ra
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: Alba Ra is an unknown quantity at this point 
Solved Threads: 0
Alba Ra Alba Ra is offline Offline
Light Poster

Re: Control of UserControl components from another form

 
0
  #19
Sep 25th, 2009
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.
Alba Ra
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: Alba Ra is an unknown quantity at this point 
Solved Threads: 0
Alba Ra Alba Ra is offline Offline
Light Poster

Re: Control of UserControl components from another form

 
0
  #20
Sep 25th, 2009
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.
Alba Ra
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
Reply With Quote Quick reply to this message  
Reply

Tags
c-sharp, usercontrol

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC