• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

Creating a custom control

I need a little help with this.  I'm trying to create a custom control - based off a label, that will take the current logged on user ID (my.user.name) and convert it to the user's full name.

I have the code to accomplish all of this - but I can't seem to get it to work the way I'd like.

I want, in the end, to have this custom-label, that when placed on a form and compiled, will show the above information without any other coding.

I'm really new to custom controls, so presumably that's where my problem lies to begin with.
Anyone more experienced care to lend a hand?  I'm using both .net 2003/2005 -but I'd rather get it working in the newer version.  Thanx! (I can post code if necessary)
  • 7
  • 4
1 Solution
Jeff CertainCommented:
Okay... what isn't working about it? :)
sirbountyAuthor Commented:
When I add it to a form, it only displays the name of the control...(AD UserName1)
Jeff CertainCommented:
So... you created a new user control, and then added the controls to it. Then you put your code into the user control, *** rebuilt the application ***, then added the control to the form?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

sirbountyAuthor Commented:
I *think* so?  Again - this was like my legitimate second attempt - and the first was guided by my textbook.

I created a new project / windows control library
I then deleted the default control (textbook said so) and readded user control, naming it appropriately.
Then, when I entered
Inherits System.Windows.Forms.Label
I got an error - and let the 'autocorrect' method fix it (I forget the actual error - but apparently somewhere else, it was still dependent on usercontrol, rather than specific label).

One item of note - when I do this under 2003, it adds the "Windows Form Designer generated code".
It doesn't do this under 2005 (for me anyway).

Perhaps that's the problem and that section contains whatever I'm missing.  Though I'm not sure why it doesn't auto generate nor why it flags my inherits section as an error initially (well, I know 'why' - I just don't know why it works without extra effort in 2003, but not in 2005).

I'm not against restarting this from scratch either if you want to walk me through it.  I want the control based off a label and when the form is loaded, this routine is run to establish the AD username.  I've got the code for it currently in the Public Sub New, under the 'Add any initialization after the InitializeComponent() call, section...
Jeff CertainCommented:
2005 does autogenerate.... it just puts it in a Partial Class in a hidden file so that you can't mess with it.

More later -- have to head to the office.
sirbountyAuthor Commented:
Ok - no rush.  Kind of playing around with creating my own in a real-world scenario.  I think this might be beneficial for an upcoming project...
sirbountyAuthor Commented:
Not that points really matter to me - but 'technically', I was waiting for Chaosian to respond, based upon his last post:

"More later -- have to head to the office."

I would like to continue this if he's still around.  Otherwise, I can open a new question later.
Jeff CertainCommented:

I'll respond :)

When you create a user control in VS2005, it hides all of the code that used to be autogenerated in the main class. It puts it into a partial class that it hides from you. This way, you can't change the stuff you're not supposed to be changing.

Usually, I create my controls as part of a project. However... I'll walk through the process for you here:
1. Create a new Windows Control Library project.
2. Delete the default control.
3. Right-click on the project. Select Add>>User Control. Name it appropriately.
4. There are two things you can do now. The easiest is to simply add a label to your control, then add the properties and code you require. The harder way is to click on "show all files" in the solution explorer, expand your control and open the designer. This is where the partial class is hiding. You can then change the inheritance to inherit label. Another error will show up due to an Autoscale property that doesn't exist in label. Remove it.

sirbountyAuthor Commented:
Ok - I started from scratch as you've outlined here.

I went with the harder way and changed it to inherit label.
Then in my New sub, I have the code to pull the username from DirectoryServices.
Saved and built the project, then added the control to a new blank form and ran it.
All it displays is the control's default name - not my username.

I have a try/catch that should set me.text to the 'found' username.
To test, I changed this line to
but it fails to show even that.
Any ideas?
sirbountyAuthor Commented:
I created a public GetName sub - and it works.
Just curious why it wouldn't on New?
Thanx for the help.
sirbountyAuthor Commented:
No, Chaosian deserves the points.  
I was just waiting in case he cared to follow up on my last comment.
I'll close this out - sorry 'bout the delay...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now