• 14
• 12
• 9
• 10
• 9

# [.net] C# CF 3.5 why isn't this loop being entered?

This topic is 2884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi again! This has literally been costing me sleep. I have a simple program I'm going to post to xda dev (and here, if people want it), basically looping through some user input. The program is a simple layout planner for use on a mobile device, so you can draw lines and boxes, create a grid and (on doubleclick) readout your x/y coords, all to help you plan out your application. This all works, except for the gridcreation. I'm using a method (AddGridLines(), copied from a AddLines() which works great) which I've basically copy/pasted/modified from something I wrote earlier, which works perfectly. The code takes TextBox input and creates lines from it which it displays. The code should have been generic enough to add to a grid generating input UI, but I made a few mistakes and decided: "if it's not broke don't fix it", so I just reworked the AddLines() into AddGridLines() instead of looping and using AddLines. The problem is that the method itself, the one it calls to, does seem to work. The loop which should call it, however, never seems to be entered! It's basically a for loop in an if loop. Using MessageBoxes, testing the method seperatley and other debug code, I KNOW the method it calls (AddGridLine()), works. As does the rendering code: if called seperately, I can display, hide and unhide the gridline it creates. I also know that the IF loop is entered. It is just that the FOR loop within the IF loop NEVER seems to be entered :( And I just cannot figure out why. I've even changed the conditional and the itterative statement, to see if that makes a difference...it doesn't seem to :( So, here's the code...please help prove I'm an idiot who made a simple mistake!
int[,] gridArray;
int gridArraySize = 0;
void gridOKBtn_Click(object sender, EventArgs e)
{
if ((gridWidthTb.Text.Length == 0) || (gridXSpacingTb.Text.Length == 0) || (gridYSpacingTb.Text.Length == 0) || (fromCentreRb.Checked == false && fromTopleftRb.Checked == false) )
{
MessageBox.Show("missing grid variables!");
}
else
{
int width = int.Parse(gridWidthTb.Text);
int xspacing = int.Parse(gridXSpacingTb.Text);
int yspacing = int.Parse(gridYSpacingTb.Text);
int halfwidth = ((int)(this.ClientSize.Width * 0.5));
int halfheight = ((int)(this.ClientSize.Height * 0.5));
//AddGridLine(gridArray, 1 , width, 240, 0, 240, 800, 0); //this does get called

if (fromCentreRb.Checked) //calc gridlines from centre
{
//for x to right of centre
for (int xstart = halfwidth; xstart >= this.ClientSize.Width; xstart = (xstart + xspacing))
{
int ystart = 0;
int yend = this.ClientSize.Height;
int xend = xstart;

MessageBox.Show(gridArray.GetLength(0).ToString());
AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
MessageBox.Show("added " + xstart.ToString() + ", " + ystart.ToString() + ", " + xend.ToString() + ", " + yend.ToString());
}
//for xlines Left of centre line
for (int xstart = (halfwidth - xspacing); xstart <= 0; xstart = (xstart - xspacing))
{
int ystart = 0;
int yend = this.ClientSize.Height;
int xend = xstart;

AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
}
//for y down from centre
for (int ystart = halfheight; ystart >= this.ClientSize.Height; ystart = (ystart + yspacing))
{
int yend = ystart;
int xstart = 0;
int xend = this.ClientSize.Width;

AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
}
//for y up from centre
for (int ystart = (halfheight - yspacing); ystart <= 0; ystart = (ystart - yspacing))
{
int yend = ystart;
int xstart = 0;
int xend = this.ClientSize.Width;

AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
}
//MessageBox.Show(gridArray.GetLength(0).ToString()); //this gets called too
}
else  //calc gridlines from topleft
{
for (int xstart = 0; xstart >= this.ClientSize.Width; xstart = (xstart + xspacing))
{
int xend = xstart;
int ystart = 0;
int yend = this.ClientSize.Height;

MessageBox.Show("xlines");
AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
MessageBox.Show("xlinesend");
}
//then do y lines
for (int ystart = 0; ystart >= this.ClientSize.Height; ystart = (ystart + yspacing))
{
int yend = ystart;
int xstart = 0;
int xend = this.ClientSize.Width;

MessageBox.Show("ylines");
AddGridLine(gridArray, 1, width, xstart, ystart, xend, yend, 1);
MessageBox.Show("ylinesend");
}
}
//MessageBox.Show(gridArray.GetLength(0).ToString());
this.Invalidate();
}
}


As I said, the Textbox.Text gets transfered/captured, and the AddGridLine() method works (if I put it in the IF statement, before the FOR loop). The first MessageBox, just before the first ELSE, gets called too (duh, really). If I put anything (msgbox, a AddGridLine() call) after the IF stament, before the first FOR loop, that gets called too! But anything IN the FOR loops? No go. Why? I just can't understand.... is it my itterator? Naw, cause that would mean the loop still should get called once, and it doesn't. *sigh*. I'm really starting to feel dumb :)

##### Share on other sites
I think you want to reverse the conditionals in your for loops. Your initial value is half the width, and yet you're saying loop while it's greater than the width. Since half the width is usually not greater than the width, it doesn't even start iterating.

##### Share on other sites
Yup, that was it.

Thanks for looking through this; I guess I'm still not an experienced enough coder to catch on to things like that. I'd screwed around with everything in that loop statement, EXCEPT for that :) Now I hope you've taught me another troubleshooting tip.

Thanks for the lesson, Mike :)