943,844 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 4592
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 24th, 2009
0

Re: Control of UserControl components from another form

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.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 24th, 2009
0

Re: Control of UserControl components from another form

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
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Alba Ra is offline Offline
54 posts
since Sep 2009
Sep 24th, 2009
1

Re: Control of UserControl components from another form

I cannot tell from the code statements why you are getting the error. I suggest you zip up the project and attach.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 24th, 2009
0

Re: Control of UserControl components from another form

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

C# Syntax (Toggle Plain Text)
  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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Sep 24th, 2009
0

Re: Control of UserControl components from another form

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.)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Alba Ra is offline Offline
54 posts
since Sep 2009
Sep 24th, 2009
1

Re: Control of UserControl components from another form

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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Sep 24th, 2009
0

Re: Control of UserControl components from another form

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Alba Ra is offline Offline
54 posts
since Sep 2009
Sep 25th, 2009
0

Re: Control of UserControl components from another form

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...
C# Syntax (Toggle Plain Text)
  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...
C# Syntax (Toggle Plain Text)
  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.)
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Alba Ra is offline Offline
54 posts
since Sep 2009
Sep 25th, 2009
0

Re: Control of UserControl components from another form

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Alba Ra is offline Offline
54 posts
since Sep 2009
Sep 25th, 2009
0

Re: Control of UserControl components from another form

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Alba Ra is offline Offline
54 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Custom Splitbuttons, Arrow lost.
Next Thread in C# Forum Timeline: Declare 2 Dimension for List<String>





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC