Solved

Finding selected label from a Tabpage.

Posted on 2010-11-13
5
593 Views
Last Modified: 2012-05-10
Could someone help me with this code, I am a beginner C# programmer, I think it will be easy for the experts.

I have a tabcontrol with 5 different tabpages, each tabpage has about six labels. one label in each tab has the forecolor = white (which means it is selected).

If a user selects a new label, I want a method to change the previously selected "White label" in the same tabpage to black.

Detail code please, I am a beginner at C#, Thanks
0
Comment
Question by:crp81
5 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
(1) What you mean by selected label ? Is it mean same when label is clicked (or selected).

(2) Why such requirement?
0
 
LVL 12

Expert Comment

by:jagssidurala
Comment Utility
I can tell the logic as per your comments given

First make all labels colors to Black , then make the selected label to color white.

0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 250 total points
Comment Utility
For demo purpose I have made program simple with two tabPage. And two labels in each.

Check code attached.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyTabControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // First two labels are in tabPage1 of tabControl
            this.label1.Click += new System.EventHandler(this.BlackToWhite);
            this.label2.Click += new System.EventHandler(this.BlackToWhite);

            // Rest two labels are in tabPage2 of tabControl
            this.label3.Click += new System.EventHandler(this.BlackToWhite);
            this.label4.Click += new System.EventHandler(this.BlackToWhite);
        }

        private void BlackToWhite(object sender, EventArgs e)
        {
            // Getting label for which event clicked
            Label label = (Label)sender;

            foreach (Label lb in label.Parent.Controls)
            {
                lb.ForeColor = Color.Black;
            }

            // Making selected label white            
            label.ForeColor = Color.White;
        }
    }
}

Open in new window

0
 
LVL 1

Assisted Solution

by:kashyap
kashyap earned 250 total points
Comment Utility
create a new dictionary collection class store the value as a list of labels mind you , you will be storing the object references exactly as they are for each label  for each key that will represent  tab index for each tab page that you are talking about the key will specify the tab index whenever a label is clicked you need to loop through the dictionary to find out which tab page is current tab page then retrieve the list of labels for that tab page and find out which is white then based on object reference that is to find out which label object is current clicked object you need to change its color to whatever is your required color and then change the remaining labels' color to black when you add your label reference to the list of values in the dictionary you need to add click even handler that will be the same for each label so you will get the sender object based on which you will get the current label
class whatever
{
Dictionary<integer,List<Label>> coll = new  Dictionary<integer,List(Label)>()
//start the tab page index with  0
//add the six labels in a list of label 
List<Label> lst = new .....
whatever()
lst.Add(label1);
lst.Add(label2);
.
.
..
lst.Add(label6)
coll.Add(0,lst)
//similarly add for each tab page changing the index like
coll.Add(1,lst2)
...
col.Add(5,lst6)
}
then in your click handler that you will associate for each label the same handler 


public void Label_Click(object sender,EventArgs e) 
{
 //search to which dictionary entry the object belongs
foreach(KeyValuePair<integer,List<Label>> kvp in coll)
if (kvp.value.contains(sender))
{
     foreach(Label l in kvp.Value)
     {
          if(l.Equals(sender))
          {
               l.Forecolor = Color.White
          }
         else
         {
               l.forecolor = Color.Black
         }
     }
return
}
}
}

Open in new window


Hope this works for you
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Sounds like you want ONE selection per GROUP...which is a perfect fit for RadioButtons.  Just place them on the TabPages and only one RadioButton per TabPage can be selected at a time.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

10 Experts available now in Live!

Get 1:1 Help Now