Solved

Finding selected label from a Tabpage.

Posted on 2010-11-13
5
597 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
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 85

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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