Solved

Finding selected label from a Tabpage.

Posted on 2010-11-13
5
604 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 34126968
(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
ID: 34127009
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
ID: 34127036
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
ID: 34127096
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 86

Expert Comment

by:Mike Tomlinson
ID: 34127791
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

634 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