Click here to Skip to main content
15,509,982 members
Home / Discussions / C#
   

C#

 
QuestionMessage Removed Pin
23-Sep-20 7:44
mveRichard MacCutchan23-Sep-20 7:44 
QuestionWPF design UI Pin
Member 1493238422-Sep-20 6:55
Member 1493238422-Sep-20 6:55 
AnswerRe: WPF design UI Pin
Gerry Schmitz22-Sep-20 7:28
mveGerry Schmitz22-Sep-20 7:28 
QuestionBetter way to handle 50 labels Pin
Member 1494373721-Sep-20 15:54
Member 1494373721-Sep-20 15:54 
AnswerRe: Better way to handle 50 labels Pin
Richard Andrew x6421-Sep-20 17:12
professionalRichard Andrew x6421-Sep-20 17:12 
AnswerRe: Better way to handle 50 labels Pin
OriginalGriff21-Sep-20 21:58
mveOriginalGriff21-Sep-20 21:58 
AnswerRe: Better way to handle 50 labels Pin
Gerry Schmitz22-Sep-20 7:45
mveGerry Schmitz22-Sep-20 7:45 
AnswerRe: Better way to handle 50 labels Pin
BillWoodruff22-Sep-20 8:59
mveBillWoodruff22-Sep-20 8: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 13:20
professionalMycroft Holmes22-Sep-20 13:20 
GeneralRe: Better way to handle 50 labels Pin
Member 149437371-Oct-20 19:52
Member 149437371-Oct-20 19:52 
AnswerRe: Better way to handle 50 labels Pin
Member 1494373728-Sep-20 14:41
Member 1494373728-Sep-20 14:41 
QuestionComplex View Model with Nested class Properties / Array Submit Pin
Guillermo Perez18-Sep-20 16:50
Guillermo Perez18-Sep-20 16:50 
GeneralRe: Complex View Model with Nested class Properties / Array Submit Pin
Richard MacCutchan18-Sep-20 22:47
mveRichard MacCutchan18-Sep-20 22:47 
AnswerRe: Complex View Model with Nested class Properties / Array Submit Pin
Gerry Schmitz19-Sep-20 3:43
mveGerry Schmitz19-Sep-20 3:43 
QuestionInsert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 9:40
cms965118-Sep-20 9:40 
AnswerRe: Insert on database contents of word file using OpenXML and c# Pin
Gerry Schmitz18-Sep-20 10:36
mveGerry Schmitz18-Sep-20 10:36 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 10:52
cms965118-Sep-20 10:52 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Gerry Schmitz18-Sep-20 12:18
mveGerry Schmitz18-Sep-20 12:18 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 21:13
cms965118-Sep-20 21:13 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Dave Kreskowiak18-Sep-20 19:45
mveDave Kreskowiak18-Sep-20 19:45 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
cms965118-Sep-20 21:11
cms965118-Sep-20 21:11 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Pete O'Hanlon19-Sep-20 0:10
mvaPete O'Hanlon19-Sep-20 0:10 
GeneralRe: Insert on database contents of word file using OpenXML and c# Pin
Gerry Schmitz19-Sep-20 3:17
mveGerry Schmitz19-Sep-20 3:17 
QuestionThe forms are cut for me using high resolution on WinForm Pin
goldsoft18-Sep-20 2:51
goldsoft18-Sep-20 2:51 
AnswerRe: The forms are cut for me using high resolution on WinForm Pin
Gerry Schmitz18-Sep-20 8:39
mveGerry Schmitz18-Sep-20 8: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.