How to make a label variable?
Here the labels are window forms. Is there a way to simplify the following code by making labels variable? The code is written in MFC. Thank you in advance!
if(a==1)
label1->Text="ok";
if(a==2)
label2->Text="ok";
if(a==3)
label3->Text="ok";
[288 byte] By [
mimi17] at [2007-11-20 11:46:19]

# 1 Re: How to make a label variable?
What type of project are you working in (MFC, Win32, Managed C++, C#)?
Arjay at 2007-11-11 4:03:09 >

# 2 Re: How to make a label variable?
What type of project are you working in (MFC, Win32, Managed C++, C#)?
It is MFC.
mimi17 at 2007-11-11 4:04:09 >

# 3 Re: How to make a label variable?
I searched for passing variables to window forms in MFC. I can't find anything that is relevant. Is it just not possible?
mimi17 at 2007-11-11 4:05:07 >

# 4 Re: How to make a label variable?
It's possible. For a label, you need to change the default IDC_STATIC id to something else (like IDC_FNAME) and then use DDX to connect up a variable with it. It's remarkably simple once you do it, but maybe confusing the first time. Here's how.
1) Open the dialog editor
2) highlight the static control (be sure to rename it something other than IDC_STATIC1 - call it IDC_MYLABEL).
3) Right click on the label
4) Choose 'Add variable'
5) When the 'Add Member Variable Wizard' opens, change the 'Category' from 'Control' to value.
6) Set the Variable Type: to CString
7) Enter a variable name (e.g. m_sMyLabel).
8) Press 'Finish'
See the screen shot for the add member variable wizard dialog in VS2005. VS2003 is similar. If you are using VC6, you should upgrade, um, I mean you need to use the class wizard.
Now you have a variable hooked up to this static control (i.e. label).
From now on, all you need to do is manipulate the m_sMyLabel variable (i.e. read/write operations are performed on this variable not the control) and let DDX handling updating the control.
If you need to change the value in the control, just set the m_sMyLabel variable and call UpdateData( FALSE ).
If you need to get the text out of the control (when since you are using a label you'll never need to do this, but for completeness say you have an edit control), just call UpdateData( ) and read the m_sMyLabel variable.
Leveraging DDX in MFC is one of the most easiet ways to simplify your MFC code. It probably can reduce the code size by nearly half.
I guess it's not well documented because many folks use the GetDlgItem method of connecting to controls.
Arjay at 2007-11-11 4:06:08 >

# 5 Re: How to make a label variable?
It's possible. For a label, you need to change the default IDC_STATIC id to something else (like IDC_FNAME) and then use DDX to connect up a variable with it. It's remarkably simple once you do it, but maybe confusing the first time. Here's how.
1) Open the dialog editor
2) highlight the static control (be sure to rename it something other than IDC_STATIC1 - call it IDC_MYLABEL).
3) Right click on the label
4) Choose 'Add variable'
5) When the 'Add Member Variable Wizard' opens, change the 'Category' from 'Control' to value.
6) Set the Variable Type: to CString
7) Enter a variable name (e.g. m_sMyLabel).
8) Press 'Finish'
See the screen shot for the add member variable wizard dialog in VS2005. VS2003 is similar. If you are using VC6, you should upgrade, um, I mean you need to use the class wizard.
Now you have a variable hooked up to this static control (i.e. label).
From now on, all you need to do is manipulate the m_sMyLabel variable (i.e. read/write operations are performed on this variable not the control) and let DDX handling updating the control.
If you need to change the value in the control, just set the m_sMyLabel variable and call UpdateData( FALSE ).
If you need to get the text out of the control (when since you are using a label you'll never need to do this, but for completeness say you have an edit control), just call UpdateData( ) and read the m_sMyLabel variable.
Leveraging DDX in MFC is one of the most easiet ways to simplify your MFC code. It probably can reduce the code size by nearly half.
I guess it's not well documented because many folks use the GetDlgItem method of connecting to controls.
Thanks a lot but I can't see the add variable in step 4. My project type is window form application.net. Sorry for not having mentioned it at the beginning.
mimi17 at 2007-11-11 4:07:18 >

# 6 Re: How to make a label variable?
Thanks a lot but I can't see the add variable in step 4. My project type is window form application.net. Sorry for not having mentioned it at the beginning.You had responded previously that the application was MFC.
Arjay at 2007-11-11 4:08:09 >

# 7 Re: How to make a label variable?
You had responded previously that the application was MFC.
Sorry, I confused MFC with Window form application.net. I thought they are the same thing. Sorry.
mimi17 at 2007-11-11 4:09:13 >

# 8 Re: How to make a label variable?
When you mean simplify the code are you talking about access within the form or external to the form? Well, it doesn't matter really. What you can do is create a property that wraps the label.text variable. Adjust the scope (protected or public) as you see fit. Make a property for each label you want to expose.
public string Label1
{
get { return label1.Text; }
set { label1.Text = value; }
}
This will simplify your code somewhat
if(a==1)
Label1="ok";
if(a==2)
Label2="ok";
if(a==3)
Label3="ok";
You can further simplify it by creating a method (but don't use magic numbers, use an enum instead).
protected void SetLabels( int magicNumber )
{
switch( magicNumber )
{
case 1: Label1 = "ok"; break;
case 2: Label2 = "ok"; break;
case 3: Label3 = "ok"; break;
default:
Debug.Assert( false, "Invalid magic number");
}
}
Arjay at 2007-11-11 4:10:16 >

# 9 Re: How to make a label variable?
Thank you. You are a hero!
How about this?
for(int x=100;x>=a;x--)// a is an unknown variable & a<100
{
label_x="ok";
}
mimi17 at 2007-11-11 4:11:12 >

# 10 Re: How to make a label variable?
I don't know what you are trying to accomplish. This loop would just set the same label to 'ok' 100 times. Is that what you are after?
Btw, if label_x is a property you may want to review the .net naming convention guidelines. You can name the variables whatever you want, it's just easier if you follow them when other folks maintain your code.
Arjay at 2007-11-11 4:12:13 >

# 11 Re: How to make a label variable?
Ok. I am going to check it. Thank you! You really help me a lot.
mimi17 at 2007-11-11 4:13:18 >
