Click here to Skip to main content
15,913,944 members
Home / Discussions / Windows Forms
   

Windows Forms

 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
Henry Minute30-Jun-09 0:34
Henry Minute30-Jun-09 0:34 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
honey.rpk30-Jun-09 0:58
honey.rpk30-Jun-09 0:58 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
Henry Minute30-Jun-09 1:08
Henry Minute30-Jun-09 1:08 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
Henry Minute30-Jun-09 1:22
Henry Minute30-Jun-09 1:22 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
honey.rpk30-Jun-09 2:30
honey.rpk30-Jun-09 2:30 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
Henry Minute30-Jun-09 5:51
Henry Minute30-Jun-09 5:51 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
ice.cool 172930-Jun-09 20:23
ice.cool 172930-Jun-09 20:23 
GeneralRe: How to make a control as a child to user control but not for a form? [modified] PinPopular
Henry Minute30-Jun-09 6:26
Henry Minute30-Jun-09 6:26 
Before I post the code, I would like to give you a couple of pointers about the code you posted.

1) the namespace. Do try not to use the same name for your namespace as you do for your class. This can sometimes confuse Visual Studio IDE
and generally cause delays that you don't need, and you have to write stuff like 'tool.tool.whatever', which just looks ugly Smile | :) . For example, if the code you posted is in the same project as your main form, have the namespace as something like toolTestApp, if it is a separate project have the namespace something like toolLibrary.

2) Where you have put your code. Just from the way the code was posted it looks as if this:
private string labeltext;
private Color pnl1color;
private Color pnl2color;







public string Labeltext
{
get
{
return labeltext;
}
set
{
labeltext = value;
label1.Text = labeltext;
}
}


public Color Pnl1color
{
get
{
return pnl1color;
}
set
{
pnl1color = value;
panel1.BackColor = pnl1color;
}
}
public Color Pnl2color
{
get
{
return pnl2color;
}
set
{
pnl2color = value;
panel2.BackColor = pnl2color;
}
}

is in the 'tool.Designer.cs' file. Bad Idea! The IDE is continually rewriting this file, your stuff could get deleted/overwritten, it probably won't at the moment but Microsoft could change the way that it creates the file so that it totally deletes the original first. Don't take the chance, move all that stuff to the 'tool.cs' file.


OK now the good bit.
The first thing to do is to create your own Designer to use instead of ParentControlDesigner. You will need what ParentControlDesigner does, so inherit from that. Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;

namespace toolLibrary
{
	public class toolDesigner : ParentControlDesigner
	{
		public override void Initialize(System.ComponentModel.IComponent component)
		{
			base.Initialize(component);

			if (this.Control is tool)
			{
				this.EnableDesignMode(((tool)this.Control).ControlPanel, "ControlPanel");
			}
		}
	}
}


Now the changes to your code. There aren't many, and I've tried to highlight them all, but read it very carefully in case I missed any.
I'm only posting the main file as the only changes I made to the 'tool.Designer.cs' file was to move the stuff I talked about earlier.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace toolLibrary
{
	[Designer(typeof(toolDesigner))]  //<======= HERE, changed to use the new tool designer
	public partial class tool : UserControl
	{
		public tool()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			//some code to make panel visible and invisible
		}

		private void tool_Load(object sender, EventArgs e)
		{
			panel2.Visible = true;
			label1.Text = "enter name of panel";
		}

		public string Labeltext
		{
			get
			{
				return this.label1.Text; // <==== I've changed this a bit, as with all the stuff I moved, keep your stuff
                                                         //       if you want/prefer it that way
			}
			set
			{
				label1.Text = value;
			}
		}

		public System.Drawing.Color Pnl1color
		{
			get
			{
				return this.panel1.BackColor;
			}

			set
			{
				panel1.BackColor = value;
			}
		}

		public System.Drawing.Color Pnl2color
		{
			get
			{
				return panel2.BackColor;
			}

			set
			{
				panel2.BackColor = value;
			}
		}

		[
		DesignerSerializationVisibility(DesignerSerializationVisibility.Content) // <======= MUST have this
		]
		public Panel ControlPanel  // <========== HERE, this is required for the thing to work.
		{
			get
			{
				return this.panel2;
			}
		}
	}
}


Well, that's it. Give it a try. If it doesn't work, or you don't understand anything, please come back.

I couldn't find any decent references except for MSDN and you know where that is don't you. Smile | :)

Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

modified on Tuesday, June 30, 2009 2:14 PM

GeneralRe: How to make a control as a child to user control but not for a form? Pin
dan!sh 30-Jun-09 6:39
professional dan!sh 30-Jun-09 6:39 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
Henry Minute30-Jun-09 8:20
Henry Minute30-Jun-09 8:20 
Generalattn. Henry : small suggestion : Re: How to make a control as a child to user control but not for a form? [modified] Pin
BillWoodruff30-Jun-09 19:11
professionalBillWoodruff30-Jun-09 19:11 
GeneralRe: attn. Henry : small suggestion : Re: How to make a control as a child to user control but not for a form? Pin
Henry Minute30-Jun-09 22:57
Henry Minute30-Jun-09 22:57 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
BillWoodruff30-Jun-09 9:07
professionalBillWoodruff30-Jun-09 9:07 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
honey.rpk30-Jun-09 18:55
honey.rpk30-Jun-09 18:55 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
honey.rpk30-Jun-09 19:46
honey.rpk30-Jun-09 19:46 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
Henry Minute30-Jun-09 22:48
Henry Minute30-Jun-09 22:48 
General[Message Deleted] Pin
ice.cool 17291-Jul-09 0:00
ice.cool 17291-Jul-09 0:00 
GeneralRe: Thanks for solution Pin
Henry Minute1-Jul-09 0:20
Henry Minute1-Jul-09 0:20 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
honey.rpk1-Jul-09 2:37
honey.rpk1-Jul-09 2:37 
GeneralRe: How to make a control as a child to user control but not for a form? Pin
Henry Minute1-Jul-09 4:20
Henry Minute1-Jul-09 4:20 
General[Message Deleted] Pin
ice.cool 17291-Jul-09 18:59
ice.cool 17291-Jul-09 18:59 
GeneralRe: How to reduce size of usercontrol in order to make it exhibit expandable and collapsable features? Pin
Henry Minute1-Jul-09 22:35
Henry Minute1-Jul-09 22:35 
AnswerRe: How to make a control as a child to user control but not for a form? Pin
BillWoodruff29-Jun-09 12:43
professionalBillWoodruff29-Jun-09 12:43 
QuestionA small help Pin
ice.cool 172928-Jun-09 21:10
ice.cool 172928-Jun-09 21:10 
Questionneed help Pin
Bahram.Zarrin27-Jun-09 5:47
Bahram.Zarrin27-Jun-09 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.