Solved

Creating a custom control

Posted on 2006-07-11
14
153 Views
Last Modified: 2010-04-23
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
Comment
Question by:sirbounty
  • 7
  • 4
14 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17080784
Okay... what isn't working about it? :)
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17080921
When I add it to a form, it only displays the name of the control...(AD UserName1)
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17080977
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
 
LVL 67

Author Comment

by:sirbounty
ID: 17081026
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17081042
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 67

Author Comment

by:sirbounty
ID: 17081202
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
 
LVL 67

Author Comment

by:sirbounty
ID: 17322469
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
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 17323272
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
 
LVL 67

Author Comment

by:sirbounty
ID: 17325316
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
 
LVL 67

Author Comment

by:sirbounty
ID: 17325726
I created a public GetName sub - and it works.
Just curious why it wouldn't on New?
Thanx for the help.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17407432
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now