locked
Editing Textboxes in a for loop, code only edits the last box in the loop. Using windows form. RRS feed

  • Question

  • so my code goes as followed

     for(int i = 0; i < NumberOfTimers; i++)
                {
                    StatusBox[i].Text = "valve is OFF";
                    if (DateTime.Now.Hour >= StartTimes[i].Value.Hour && DateTime.Now.Minute >= StartTimes[i].Value.Minute)
                    {
                        if (StartTimes[i].Value.Minute + DurationTimers[i].Value - 1 >= DateTime.Now.Minute)
                        {
                            StatusBox[i].Text = "valve is ON";
                            ValveStatus = true;
                        }
                    }
                }

    The problem is that no matter what length NumberOfTimers is, only the last statusBox in the array gets changed, this includes the "valve is OFF" part too.

    what is going on?


    Thursday, January 23, 2020 10:53 PM

All replies

  •                 if (DateTime.Now.Hour >= StartTimes[i].Value.Hour && DateTime.Now.Minute >= StartTimes[i].Value.Minute)

    Hi Septic Feast,

    I am not too familiar with reading C++ code, but I do not trust the above line.

    I would use something like (in VBA-syntax):

                    IF (DateTime.Now.Hour > StartTimes[i].Value.Hour) _
                    OR (DateTime.Now.Hour = StartTimes[i].Value.Hour AND DateTime.Now.Minute >= StartTimes[i].Value.Minute) THEN …

    Imb.

    Thursday, January 23, 2020 11:21 PM
  • the code is in c#, and the timer code in question works well (i have an pre-set StatusBox that isnt related to the array or the for loop)

    what im mostly concerned about is how the StatusBox[i] = "Valve is OFF"; doesnt work at all, and its entirely independent of the timer checking.

    Thursday, January 23, 2020 11:54 PM
  • IIRC, you have to set the focus to the control in order to change its display.

    (air code)

    StatusBox[i].SetFocus = True;

    StatusBox[i].Text = "new value"


    peter n roth - http://PNR1.com, Maybe some useful stuff

    Wednesday, January 29, 2020 10:44 PM