creating a custom control vs. UserControl

What's the difference between a custom control and a UserControl?

Page 1 of 1

5 Replies - 4014 Views - Last Post: 11 November 2009 - 09:40 AM Rate Topic: -----

#1 codetonic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 13-May 09

creating a custom control vs. UserControl

Posted 09 November 2009 - 05:37 PM

Using Visual C# 2008 Express.

What's the difference between creating a custom control by adding controls to a panel, for example, or adding the thing called UserControl from the Project->Add Windows Form wizard?

I'm trying to create a run-time custom control to a VS c# Windows Form program.
When I instantiate the UserControl from my main form, the subcontrols I placed in the UserControl designer
don't appear in the Intellisense selector:
UserControl UserControl1 = new UserControl();
UserControl1.<>
How do I set the subcontrol properties that are in UserControl1?

			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory = "txt|*.txt|all|*.*";
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				StreamReader inFile = new StreamReader(openFileDialog1.FileName);
				while (!inFile.EndOfStream)
				{
					String inln = inFile.ReadLine();
					UserControl UserControl1 = new UserControl();
					UserControl1.label1 = inln; // This seems like the right place but Intellisense doesn't agree.
//
// The error: UserControl does not contain a definition for label1. Are you missing a using directive or assembly reference?
//				   
				}
				inFile.Close();
			}
		}


I like using VS, but it seems I'm always doing something just beyond my and its capability!

This post has been edited by codetonic: 10 November 2009 - 03:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: creating a custom control vs. UserControl

#2 codetonic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 13-May 09

Re: creating a custom control vs. UserControl

Posted 10 November 2009 - 03:22 PM

I'm thinking it has something to do with namespace.
As obvious as namespace appears to want to be,
there are subtleties that still get me.

In VSC#2008, is namespace additive?
If namespace custom1 appears in different
code snippets in the VS design environment,
are the contents added to the namespace called custom1
or is there overwriting and supercession going on?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CustomControl1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory = "txt|*.txt|all|*.*";
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				StreamReader inFile = new StreamReader(openFileDialog1.FileName);
				while (!inFile.EndOfStream)
				{
					String inln = inFile.ReadLine();
					UserControl UserControl1 = new UserControl1();
					UserControl1.SetLabelText(inln);
					
				}
				inFile.Close();
			}
		}
			
	}
}


namespace CustomControl1
{
	public partial class UserControl1
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
.
.
.

		}

		#endregion

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.RadioButton maleButton;
		private System.Windows.Forms.RadioButton femaleButton;
		private System.Windows.Forms.RadioButton childButton;
		private System.Windows.Forms.RadioButton adolesentButton;
		private System.Windows.Forms.RadioButton adultButton;
		private System.Windows.Forms.RadioButton seniorButton;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label2;

		public void SetLabelText(string text)
		{
			label1.Text = text;
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#3 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: creating a custom control vs. UserControl

Posted 10 November 2009 - 03:51 PM

Quote

I'm trying to create a run-time custom control to a VS c# Windows Form program.
When I instantiate the UserControl from my main form, the subcontrols I placed in the UserControl designer
don't appear in the Intellisense selector:
UserControl UserControl1 = new UserControl();
UserControl1.<>
How do I set the subcontrol properties that are in UserControl1?


You need to set the access modifier of the controls to public.
Was This Post Helpful? 0
  • +
  • -

#4 codetonic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 13-May 09

Re: creating a custom control vs. UserControl

Posted 10 November 2009 - 05:54 PM

lesPaul456, thanks for the reply,

Could you be more specific?
When you say "access modifier" are you talking about the member declaration (ie., public):
public void SetLabelText(string text)
		{
			label1.Text = text;
		}


Or is it a property for the class or instantiation of UserControl1?
if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				StreamReader inFile = new StreamReader(openFileDialog1.FileName);
				while (!inFile.EndOfStream)
				{
					String inln = inFile.ReadLine();
					UserControl myControl = new UserControl1();
					myControl.SetLabelText(inln);
					
				}
				inFile.Close();
			}


I don't see any properties called "access modifier".
Thanks
Was This Post Helpful? 0
  • +
  • -

#5 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: creating a custom control vs. UserControl

Posted 10 November 2009 - 06:12 PM

Follow this link: http://msdn.microsof...y/ms173121.aspx
Was This Post Helpful? 1
  • +
  • -

#6 codetonic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 13-May 09

Re: creating a custom control vs. UserControl

Posted 11 November 2009 - 09:40 AM

Subtle while trying to see the answer.
Obvious now that I know.

The first instantiation sets the class type to UserControl
which is the parent class to the user control I was trying
to create.
The second instantiation sets the class type to the inherited
class I created, which DOES contain the additional members
I want to use.
UserControl myControl = new UserControl1();
					myControl.SetLabelText(inln); // None existant

UserControl1 myControl = new UserControl1();
					myControl.SetLabelText(inln); // There you are




View PostlesPaul456, on 10 Nov, 2009 - 05:12 PM, said:


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1