[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

Finding selected label from a Tabpage.

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
crp81
Asked:
crp81
2 Solutions
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
(1) What you mean by selected label ? Is it mean same when label is clicked (or selected).

(2) Why such requirement?
0
 
jagssiduralaCommented:
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
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
kashyapCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now