Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding selected label from a Tabpage.

Posted on 2010-11-13
5
Medium Priority
?
623 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 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline

782 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