# C# - label(i.ToString()); (when in for loop)



## computer_pet (Jun 22, 2003)

Hey,

I am new to csharp and am wondering if it is possible to do something.

When in a For type loop, such as
for (int i = 1; i < 15; i++)

and while in the loop you want to set a label to some text like
label1 = i;

how can you increase the label object after each loop, i tried

label(i) = i;
OR
label(i.ToString()) = i;

but did not work.

What I am saying is that I have 14 labels on my windows form, and when in this loop I want to set these labels to the number it is in the loop.

How can i do this?

Thanks in Advance


----------



## Chicon (Jul 29, 2004)

Hi computer_pet,

There's no need to use labels. Use the *switch* statement like this :


```
[SIZE=3]
for (int i = 1; i < 15; i++) {
   switch(i) {
      case 1:
         some statements
         break;
      case 2:
         some statements
         break;
      .
      .
      case 14 :
         some statements
         break; 
   }
}
[/SIZE]
```


----------



## computer_pet (Jun 22, 2003)

thanks 
Works great

One other question:

I have data that I have put into strings which are in a numerical date format such as "015500" would be 1:55am and "232500" is 11:25pm (first two numbers specify the hour, next two the minutes and last two are seconds but never use the last two. My question is that is there anyway that these strings can be converted to a nice looking "9:00am" format for the end user?

Theses strings once collected are being stored into a database in a varchar(50) format but I can change the format. I see in the drop down list there is a datetime one, could I use this and csharp will do it for me?

If there is no solution, I would have to do it using about 10000 if statements!  

Thanks in advance


----------



## Chicon (Jul 29, 2004)

Something like this :


```
[SIZE=3]
int hour_to_convert = something;
boolean isPM;
String result = "";
int hour;
int minute; 


if (hour_to_convert > 125959) {
   isPM = true;
  hour_to_convert = hour_to_convert - 120000;
}
else isPM = false;

//stripping of the seconds
hour_to_convert = (hour_to_convert - (hour_to_convert % 100)) / 100;

minute = hour_to_convert % 100;

hour = (hour_to_convert - minute) / 100;

result = "" + hour + ":" + minute;
if (isPM) result = result + "pm";
else result = result + "am";

[/SIZE]
```


----------



## computer_pet (Jun 22, 2003)

thanks,
will try when I get home


----------



## computer_pet (Jun 22, 2003)

Thanks chicon  worked great.
This hopefully will be my last question for this thread, how can you delete all the rows in a datasource? or do you have to individually specifiy them?

Thanks


----------



## Chicon (Jul 29, 2004)

I'm not a C# specialist but i would try something like this :

Example for a ListBox


```
[SIZE=3]
ListBox1.DataSource = null;
[/SIZE]
```


----------



## computer_pet (Jun 22, 2003)

That will just change the datasource the listbox belongs to. I need to clear the contents.

I tried

```
bindingcourcename.Clear();
```
 but I get an error saying this can not be done.

Any ideas?

Thanks


----------



## computer_pet (Jun 22, 2003)

Okay have a similar situation. 
The numerical data is in the form of "20060211193000 +1000" which means;
first four specify year, next two numbers specify month, next two numbers specify day of month, then comes hour, then minutes, then secconds. 
The " +1000" I guess is time zone, but do not need that. So that particular number means:
"11/02/2006 7:30:00pm"

Is there anyway to do the same thing as before; convert it to a nice looking time with am and pm.
The year, month, date, time zone I do not need.
Just want in form of "7:30pm". (but if not possible will accept with the date aswell)

Is this possible? I think you can get rid of the "+1000" part by making it a calculation then just subtract 1000 from the answer (it is always +1000"). 
I also have access to the date, so once the +1000 bit is removed, can use that number as a int. ie:

```
20060211193000 -
20060211000000
= 193000
```
So in code I think it would be something like this:

```
int date = currentdate * 100000 //This adds the extra 000000
int hour_to_convert = timewiththedate - date // Removes the date and leaves only the time 
//part; which the original code can use
```
Then just use the orginal code to get to the time.

How can I use a string to make a calculation?

Any Help would be great 



Chicon said:


> Something like this :
> 
> 
> ```
> ...


----------



## computer_pet (Jun 22, 2003)

Worked a way of doing it, when I have some time will post some code.


----------

