• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • 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)
0
sirbounty
Asked:
sirbounty
  • 7
  • 4
1 Solution
 
Jeff CertainCommented:
Okay... what isn't working about it? :)
0
 
sirbountyAuthor Commented:
When I add it to a form, it only displays the name of the control...(AD UserName1)
0
 
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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...
0
 
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.
0
 
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...
0
 
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.
0
 
Jeff CertainCommented:
Okay....

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.

0
 
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
Me.Text="Working"
but it fails to show even that.
Any ideas?
0
 
sirbountyAuthor Commented:
I created a public GetName sub - and it works.
Just curious why it wouldn't on New?
Thanx for the help.
0
 
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...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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