473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Windows form - Label

fayazmd
41
Hi,

I am stuck with a problem on labels. I have 30 labels on my from. And from one method i am returning a value, based on it i have to make one label as invisible.

I took switch - case, but i am guided to take object of that label and make it invisible.

I followed this code, but it is throwing error.

Label ToHide = new Label();
ToHide = Convert.ChangeType("Label"+ind, TypeCode.Object);
(ToHide as Label).Visible = false;

Here ind is int returning from one method, and i am concatinating it to get label name.
Mar 3 '08 #1
8 1770
Plater
7,872 Expert 4TB
You would need to get the instance of that control first and not just create a new label.
Try something like:
Expand|Select|Wrap|Line Numbers
  1. Label ToHide;
  2. ToHide =this.Controls.Find("Label" + ind, true);
  3. if(ToHide!=null)
  4. {//it found your label
  5.    (ToHide as Label).Visible = false;
  6. }
  7.  
Mar 3 '08 #2
fayazmd
41
Thanks for your reply.
I am sorry, I forgot to mention. I am working in .net 2003.
Where i couldn't find 'this.Controls.Find'. Please suggest me how can i proceed.


You would need to get the instance of that control first and not just create a new label.
Try something like:
Expand|Select|Wrap|Line Numbers
  1. Label ToHide;
  2. ToHide =this.Controls.Find("Label" + ind, true);
  3. if(ToHide!=null)
  4. {//it found your label
  5.    (ToHide as Label).Visible = false;
  6. }
  7.  
Mar 4 '08 #3
Here is another way you can do this without using the Controls.Find method:

Expand|Select|Wrap|Line Numbers
  1. string labelToHide = "Label" + ind;
  2. if(this.Controls.ContainsKey(labelToHide))
  3. {
  4.     this.Controls[labelToHide].Visible = false;
  5. }
  6.  
A word of caution, the this.Controls.ContainsKey method does not do a recursive search like the Controls.Find method does. So if your labels are added to a container, such as a panel, then the this.Controls.ContainsKey method will return false.

Hi,

I am stuck with a problem on labels. I have 30 labels on my from. And from one method i am returning a value, based on it i have to make one label as invisible.

I took switch - case, but i am guided to take object of that label and make it invisible.

I followed this code, but it is throwing error.

Label ToHide = new Label();
ToHide = Convert.ChangeType("Label"+ind, TypeCode.Object);
(ToHide as Label).Visible = false;

Here ind is int returning from one method, and i am concatinating it to get label name.
Mar 4 '08 #4
Plater
7,872 Expert 4TB
Then I guess you will need to make a mimic of this function:

Expand|Select|Wrap|Line Numbers
  1. private Control Find(Control startpoint, string ControlName, bool Recurse)
  2. {
  3.     foreach (Control c in startpoint.Controls)
  4.     {
  5.       if (c.Name == ControlName)
  6.       {
  7.           return c;
  8.       }
  9.       if ((Recurse) & (c.HasChildren))
  10.       {
  11.         Control temp=Find(c, ControlName, Recurse);
  12.         if (temp != null)
  13.         {
  14.           return temp;
  15.         }
  16.       }
  17.     }
  18. return null;
  19. }
  20.  
Then you can use it like:
Expand|Select|Wrap|Line Numbers
  1. Control myc=Find(this,"Label"+ind,true);
  2. if(myc!=null)
  3. {//found the control
  4. }
  5.  
Mar 4 '08 #5
fayazmd
41
Thanks, it's working.


Then I guess you will need to make a mimic of this function:

Expand|Select|Wrap|Line Numbers
  1. private Control Find(Control startpoint, string ControlName, bool Recurse)
  2. {
  3.     foreach (Control c in startpoint.Controls)
  4.     {
  5.       if (c.Name == ControlName)
  6.       {
  7.           return c;
  8.       }
  9.       if ((Recurse) & (c.HasChildren))
  10.       {
  11.         Control temp=Find(c, ControlName, Recurse);
  12.         if (temp != null)
  13.         {
  14.           return temp;
  15.         }
  16.       }
  17.     }
  18. return null;
  19. }
  20.  
Then you can use it like:
Expand|Select|Wrap|Line Numbers
  1. Control myc=Find(this,"Label"+ind,true);
  2. if(myc!=null)
  3. {//found the control
  4. }
  5.  
Mar 5 '08 #6
fayazmd
41
Hi Plater,

Nice method. It replaced my lines of coding to few lines.

I understood the method that you are looking for each control and control's name against all controls in your form. But, i didn't get recurse and has children part. This block of code

if ((Recurse) & (c.HasChildren))
{
Control temp=Find(c, ControlName, Recurse);
if (temp != null)
{
return temp;
}
}

Could you please explain me this block. Thanks in advance.


Then I guess you will need to make a mimic of this function:

Expand|Select|Wrap|Line Numbers
  1. private Control Find(Control startpoint, string ControlName, bool Recurse)
  2. {
  3.     foreach (Control c in startpoint.Controls)
  4.     {
  5.       if (c.Name == ControlName)
  6.       {
  7.           return c;
  8.       }
  9.       if ((Recurse) & (c.HasChildren))
  10.       {
  11.         Control temp=Find(c, ControlName, Recurse);
  12.         if (temp != null)
  13.         {
  14.           return temp;
  15.         }
  16.       }
  17.     }
  18. return null;
  19. }
  20.  
Then you can use it like:
Expand|Select|Wrap|Line Numbers
  1. Control myc=Find(this,"Label"+ind,true);
  2. if(myc!=null)
  3. {//found the control
  4. }
  5.  
Mar 5 '08 #7
Plater
7,872 Expert 4TB
The reason you have to check for children is that the Controls is really a "tree" heirarchy.
Say you have a form called Form1
Then you add a Texbox tb1 and a groupbox gb1.
In the group box you stick two labels lb1 and lb2

Form1.Controls will contain:
tb1
gb1

gb1 will "HasChildren"
and gb1.Controls will contain
lb1
lb2

So just searching in Form1.Controls will not contain child controls of it's children.
Therefor I recurse(if you set the boolean to true) through the children to look for other children to see if it's a match.
Mar 5 '08 #8
fayazmd
41
Thank you once again for explanation.

The reason you have to check for children is that the Controls is really a "tree" heirarchy.
Say you have a form called Form1
Then you add a Texbox tb1 and a groupbox gb1.
In the group box you stick two labels lb1 and lb2

Form1.Controls will contain:
tb1
gb1

gb1 will "HasChildren"
and gb1.Controls will contain
lb1
lb2

So just searching in Form1.Controls will not contain child controls of it's children.
Therefor I recurse(if you set the boolean to true) through the children to look for other children to see if it's a match.
Mar 6 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sinisa | last post by:
I have a Windows form which has multiple groups of the same window controls. I wanted to create a control class that has the standard controls in one. something like: public class Sensor :...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
0
by: Phil G. | last post by:
Hi, my 'project' requires that I create a form with text info. at set time periods. These time periods are not evenly spaced so I pass a param for the delay(seconds). In order to debug this I have...
2
by: Joneleth | last post by:
Hi, sorry for that kind of a silly question, but j just got crazy finding a way to display a transparent label with a SIMPLE white border on a form with a dark backgrond color. I'd like to...
6
by: coldude | last post by:
hi there, I need to be able to generate multiple labels for the use in a Windows form, the quantity of labels would depend on a count, is it possible to generate multiple labels in code? If so how...
3
by: michaelstonecroft | last post by:
I have a System.Windows.Forms.Label obj. How can I copy/paste the text in the label during run time. The TextBox obj has the context menu by default. Can I have the same for Label? Thanks for the...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
11
by: Peter Larsen [] | last post by:
Hi, I have two questions related to the Label control. How do i prevent the label from expand the canvas into 2 lines (word wrap) ?? At the moment i set AutoSize to false (to prevent the word...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.