1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by computer_pet, Jan 26, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. computer_pet

    computer_pet Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    484
    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
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi computer_pet,

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

    Code:
    [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]
     
  3. computer_pet

    computer_pet Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    484
    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
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Something like this :

    Code:
    [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]
     
  5. computer_pet

    computer_pet Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    484
    thanks,
    will try when I get home :)
     
  6. computer_pet

    computer_pet Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    484
    Thanks chicon :D 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 :)
     
  7. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    I'm not a C# specialist but i would try something like this :

    Example for a ListBox

    Code:
    [SIZE=3]
    ListBox1.DataSource = null;
    [/SIZE]
     
  8. computer_pet

    computer_pet Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    484
    That will just change the datasource the listbox belongs to. I need to clear the contents.

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

    Any ideas?

    Thanks
     
  9. computer_pet

    computer_pet Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    484
    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:
    Code:
    20060211193000 -
    20060211000000
    = 193000
    
    So in code I think it would be something like this:
    Code:
    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 :)

     
  10. computer_pet

    computer_pet Thread Starter

    Joined:
    Jun 22, 2003
    Messages:
    484
    Worked a way of doing it, when I have some time will post some code.
     
  11. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/437587

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice