[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

several labels in Designer.cs, the text on the labels should be changed. Several if's?

Hi there;

I got several labels in Designer.cs, and the text on the labels should be changed.

Should I go for several ifs?

I am asking this for the sake of clean coding.

Kind regards.
0
jazzIIIlove
Asked:
jazzIIIlove
  • 8
  • 7
7 Solutions
 
Jacques Bourgeois (James Burger)Commented:
Why the ifs? How and where do you plan to use them? Do you want to change the labels only under certain conditions?

One thing is sure, do not change the structure of the Designer.cs file. There are no ifs in there, so do not add any. This code is generated by the Form Designer. It knows how where things are placed in that file. If you change the way that code is set, the Form Designer might crash when you will add or remove controls, or when you change their properties later on.

If you need to intervene on the text at run time, do it in the form constructor or the Load event instead of doing it in the Designer.cs file.
0
 
jazzIIIloveAuthor Commented:
>>do it in the form constructor or the Load event instead of doing it in the Designer.cs file.

Can you give a simple example for this?

Kind regards.
0
 
Jacques Bourgeois (James Burger)Commented:
In order to give you an example, I need to know what you want to do exactly, get your answers to the questions in the first line of my post.
0
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.

 
jazzIIIloveAuthor Commented:
I mean suppose you have several labels to change their text in the runtime.

>>If you need to intervene on the text at run time, do it in the form constructor or the Load event instead of >>doing it in the Designer.cs file.

Can you provide the LoadEvent example for this, I mean should I have if checks for this, if (this case), then this text on the label else...

Something like that?

Kind regards.
0
 
Jacques Bourgeois (James Burger)Commented:
Sorry for the delay, I was teaching all day out of town today and had a 3 hours drive to come back.

Still not sure that it is what you want, but here is how I see it. Do not write any code in the .Designer.cs file. Leave it as it is. Do all your work in the .cs file.

First, double click on the background of the form. This will create the Load event and register it so that the system will call it when the form is loaded. Here is what it may look like:
private void Form2_Load ( object sender, EventArgs e )
{
    int x = 10;
    if (x==10)
    {
        label1.Text = "What do you want...";
        label2.Text = "...in your label?";
    }
    else
    { 
        label1.Text = "Good morning!";
        label2.Text = "How are you?";
    }
}

Open in new window

0
 
jazzIIIloveAuthor Commented:
ok, but this definitely works but is this way professional enough?

Regards.
0
 
Jacques Bourgeois (James Burger)Commented:
Most of the time, because there is no other way. But it depends on the condition. That is why my 3 words where "Why the ifs?" when I first answered you.

There are situations where there are alternatives, such as if you work with bilingual applications (using resources is then usually a better choice) or when some of the Label's text is defined by the user himself (using a configuration file is then usually a better choice).

Sometimes, doing it from data in a database might be a better solution. It would take longer to load the form because of the extra time needed to fetch the data from the database, and you might have to change the text individually anyway, depending on the structure of the database. But it would permit changing the text without having to recompile the application.

So, depending on why you need to change the text, there might be better solutions.
0
 
jazzIIIloveAuthor Commented:
Hi there;

ok, how can I do this with a dictionary together with a lambda expression? (Let's take your example).

Kind regards.
0
 
Jacques Bourgeois (James Burger)Commented:
Can you please tell me what type of If you would have. Otherwise, nothing makes sense and any example I might give would be useless.
0
 
jazzIIIloveAuthor Commented:
ah, I have no idea, just I want to try to use a dictionary idea storing the label index and set the text of the labels with lambda expression.

Possible or am i forcing the idea?

Regards.
0
 
Jacques Bourgeois (James Burger)Commented:
I think you are forcing it.

The notion of a Label index is not a good one. It would be a maintenance nightmare.

There are only 3 places where you can retrieve some kind of index for a control.

Their index in the Controls collection of the form, and the TabIndex. If you need to make any change to the form, those values will change for individual controls.

You could use the Tag property of the controls to identify each of them. At least, there, you would have a fixed value that you defined yourself, and then, using a dictionary might work. But even then, you would need to make changes to the dictionnary everytime you add or remove a control from the form.

If the labels all change under a given condition, I think that using resources would be a better way to do that. Although resources have been designed primarily for multilingual applications, they could be used for what you want to do. During development, resources are xml files that contains strings, images, anything that can change between 2 different versions. One of those file (the default) is compiled into the application. The others are compiled as dll. When the application is running, you can switch between the default an any other resource. This automatically change the strings, images, etc. that are displayed by the application. Handled properly, they are automatically destroyed when you remove a control from the screen.

I would give them a look if I were you.
0
 
jazzIIIloveAuthor Commented:
ok,
>>during development, resources are xml files that contains strings, images, anything that can change >>between 2 different versions.

ok we are talking about resx files, right?

If so, is it safe to edit them?

Regards
0
 
Jacques Bourgeois (James Burger)Commented:
Yes, these are the resx files.

Since you are working with labels, the safest method to edit them would be by first changing the Localizable property of the form to True.

Once this is done, the original form becomes the default.

Change the Language property.
Type new values in the Text property of the Labels.
You can even change the Location and Size of the Labels, something that might be needed when you change the Text. The Location and Size will be saved in the resource along the Text.
When you switch between the default language and the other language, you will see the display change accordingly.
Note that you can add and remove controls only from the default language.

You could consider the default as being Set 1 and the second language as being Set 2.

You could make the same kind of switch in the application by changing the language of the UI through the classes in the System.Globalization namespace. You need to set the language before displaying a Form for the thing to work properly. Note that this could also change other things such as the date format and such, but these things are usually simple enough to adjust.

0
 
jazzIIIloveAuthor Commented:
Is it healthy to write codes manually in resx files as they seem to connect to the form.

Regards.
0
 
Jacques Bourgeois (James Burger)Commented:
As I told you in my previous post, the best way to write in the resx files associated to the forms is by setting the Localizable property of the form, and then switching between default an one or more languages for each set of Text.

Once Localizable is set to True, entering anything in the Text property of any control will automatically write into the .resx file for the language specified by the Language property. You do not have to manually write in the .resx file, the Text entry in the Properties window does it for you.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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