Click here to Skip to main content
15,397,224 members
Home / Discussions / C#
   

C#

 
GeneralRe: want to insert leave dates for weekdays excluding weekends from given date range using c# ASP.net Pin
OriginalGriff23-Sep-20 23:54
mveOriginalGriff23-Sep-20 23:54 
QuestionWPF design UI Pin
Member 1493238422-Sep-20 5:55
MemberMember 1493238422-Sep-20 5:55 
AnswerRe: WPF design UI Pin
Gerry Schmitz22-Sep-20 6:28
mveGerry Schmitz22-Sep-20 6:28 
QuestionBetter way to handle 50 labels Pin
Member 1494373721-Sep-20 14:54
MemberMember 1494373721-Sep-20 14:54 
AnswerRe: Better way to handle 50 labels Pin
Richard Andrew x6421-Sep-20 16:12
professionalRichard Andrew x6421-Sep-20 16:12 
AnswerRe: Better way to handle 50 labels Pin
OriginalGriff21-Sep-20 20:58
mveOriginalGriff21-Sep-20 20:58 
AnswerRe: Better way to handle 50 labels Pin
Gerry Schmitz22-Sep-20 6:45
mveGerry Schmitz22-Sep-20 6:45 
AnswerRe: Better way to handle 50 labels Pin
BillWoodruff22-Sep-20 7:59
mveBillWoodruff22-Sep-20 7:59 
As an alternative to the excellent example posted by OriginalGriff, I'd like to demonstrate how easy it is to make a Component [^] that "bakes in" the functionality you describe; the Component is derived from Label:

... you might ask: why go to all this trouble ? D'Oh! | :doh: I assert that mastery of using custom (inherited) WinForm Controls is a very valuable skill that can help with code encapsulation, and "separation of concerns" ...

0 as you may know: a Component is created using the Project/Add Component menu choice, and, like a UserControl, will appear in the Toolbar.

... this Component, AutoNumberedLabel ...

1 keeps a static Dictionary <int, AutoNumberedLabel> which is updated automatically each time you create an instance by drag-dropping from toolbar, or, in code.

2 provides a method that looks up the AutoNumberedLabel instance from an integer parameter..

3 provides a public read-only property that holds the AutoNumberedLabel's integer id.

4 provides a public method that sets the AutoNumberedLabel's BackColor
C#
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class AutoNumberedLabel : Label
    {
        public AutoNumberedLabel()
        {
            InitializeComponent();
        }
        
        // this constructor is called when the control is created at run-time
        public AutoNumberedLabel(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            LabelId = lblId;

            IntToLabel.Add(lblId++, this);
        }

        static Dictionary<int, AutoNumberedLabel> IntToLabel = new Dictionary<int, AutoNumberedLabel>();

        static int lblId = 0;

        public int LabelId { get; }

        public static AutoNumberedLabel GetLabelByID(int id)
        {
            if (IntToLabel.ContainsKey(id))
            {
                return IntToLabel[id];
            }

            throw new KeyNotFoundException();
        }

        public static void SetLabelColorByID(int id, Color color)
        {
            if (IntToLabel.ContainsKey(id))
            {
               IntToLabel[id].BackColor =  color;
               return;
            }

            throw new KeyNotFoundException();
        }
    }
}
Here's how you might use this, assuming you've assigned all your AutoNumberedLabels the same Click Eventhandler:
C#
private void autoNumberedLabel_Click(object sender, EventArgs e)
{
    AutoNumberedLabel lbl = (AutoNumberedLabel) sender;
    
    int Id = lbl.LabelId;

    // test if a round-trip works
    AutoNumberedLabel lbl2 = AutoNumberedLabel.GetLabelByID(Id);
    if(lbl2 != lbl) throw new ArgumentException();

    AutoNumberedLabel.SetLabelColorByID(Id, Color.Gold);
}

«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali


modified 22-Sep-20 23:59pm.

AnswerRe: Better way to handle 50 labels Pin
Mycroft Holmes22-Sep-20 12:20
professionalMycroft Holmes22-Sep-20 12:20 
GeneralRe: Better way to handle 50 labels Pin
Member 149437371-Oct-20 18:52
MemberMember 149437371-Oct-20 18:52 
AnswerRe: Better way to handle 50 labels Pin
Member 1494373728-Sep-20 13:41
MemberMember 1494373728-Sep-20 13:41 
QuestionComplex View Model with Nested class Properties / Array Submit Pin
Guillermo Perez18-Sep-20 15:50
MemberGuillermo Perez18-Sep-20 15:50 
GeneralRe: Complex View Model with Nested class Properties / Array Submit Pin
Richard MacCutchan18-Sep-20 21:47
mveRichard MacCutchan18-Sep-20 21:47 
AnswerRe: Complex View Model with Nested class Properties / Array Submit Pin
Gerry Schmitz19-Sep-20 2:43
mveGerry Schmitz19-Sep-20 2:43 
QuestionInsert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 8:40
Membercms965118-Sep-20 8:40 
AnswerRe: Insert on database contents of word file using OpenXML and c# Pin
Gerry Schmitz18-Sep-20 9:36
mveGerry Schmitz18-Sep-20 9:36 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 9:52
Membercms965118-Sep-20 9:52 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Gerry Schmitz18-Sep-20 11:18
mveGerry Schmitz18-Sep-20 11:18 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 20:13
Membercms965118-Sep-20 20:13 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Dave Kreskowiak18-Sep-20 18:45
mveDave Kreskowiak18-Sep-20 18:45 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 20:11
Membercms965118-Sep-20 20:11 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Pete O'Hanlon18-Sep-20 23:10
mvaPete O'Hanlon18-Sep-20 23:10 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Gerry Schmitz19-Sep-20 2:17
mveGerry Schmitz19-Sep-20 2:17 
QuestionThe forms are cut for me using high resolution on WinForm Pin
goldsoft18-Sep-20 1:51
Membergoldsoft18-Sep-20 1:51 
AnswerRe: The forms are cut for me using high resolution on WinForm Pin
Gerry Schmitz18-Sep-20 7:39
mveGerry Schmitz18-Sep-20 7:39 

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.