| | |
Control of UserControl components from another form
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
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
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.
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)
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[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
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
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
then inside a method of some sort, for example the load event of the usergamecontrol create the instance and pass the "this" argument
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.
so you would have to create a variable to hold the instance directly in the class
C# Syntax (Toggle Plain Text)
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)
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.
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
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:
And now the Form from which I want to control the properties:
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.)
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
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
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.
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.
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
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... ... 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...
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.)
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.
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)
MindMasterColourForm ColourForm; private void UserControlGame_Load(object sender, EventArgs e) { ColourForm = new MindMasterColourForm(this); }
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)
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)
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.
Alba Ra
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
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.
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
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
•
•
Join Date: Sep 2009
Posts: 31
Reputation:
Solved Threads: 0
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.
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
C# (coming from C and C++)
PHP/XHTML/CSS
plans: Java, VB and Python
![]() |
Similar Threads
- AS3 FocusManager, tabIndex and tab loop control in flash form components (Graphics and Multimedia)
- Opening a new window form with web browser control,with webbrowser.navigating event (VB.NET)
- Update website control from usercontrol? (ASP.NET)
- access events of form in UserControl (Visual Basic 4 / 5 / 6)
- How to inherit windows controls on a form in another form? (VB.NET)
- How to make a child form fix into a main form which panel alocated. (C#)
- Control or Object (Other than MDI Form) In vb 6.0 which can hold a Form (Visual Basic 4 / 5 / 6)
- Usercontrol please help!!!!!!!!!!!!!!! (VB.NET)
- How to use scroll bar control (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: Custom Splitbuttons, Arrow lost.
- Next Thread: Declare 2 Dimension for List<String>
| Thread Tools | Search this Thread |







