In Need of some C# help

I have created a form which has two buttons on it

BtnUP and BtnDown

Also on the form I have a a box two ORANGE picture boxes

PicPrevious and PicNew

The object of the program is depending on how many times that the BtnUp as been clicked and it records it in a table and the nuber of clicks that the BtnDown as been clicked once both buttons have been clicked then I would like some kind of ratio between the results of up and down clicks which say if the clicks are of a ratio of more than 4:1 then one of the orange picture boxes changes to Blue but if the ratio is less than 1:4 the colour of the picture box changes from orange to black.

If anyone as any ideas I can send you the program that i'm trying to put the code into so you get more of a understanding plus if you don't get me when i say ratio please check out

Any suggestions??

Andy.

Dani AI

Generated

wanted the two orange picture boxes to flip color based on the up/down click ratio; was right to treat a ratio as a real number, but a few practical points make the behavior predictable and safe.

Keep two integer counters and only compute a ratio once both counters are > 0 (don’t initialise them to 1 — that distorts results). Use a double for the division and named constants for thresholds so the logic is explicit (for example UPPER = 4.0 and LOWER = 0.25). Decide whether thresholds are inclusive (>= / <=) or strict (> / <) and use those consistently.

Example WinForms pattern (keeps it clear and avoids division-by-zero):

private int upClicks = 0;
private int downClicks = 0;
private const double UPPER = 4.0;
private const double LOWER = 0.25;

private void BtnUp_Click(object sender, EventArgs e)
{
    upClicks++;
    UpdateRatioDisplay();
}

private void BtnDown_Click(object sender, EventArgs e)
{
    downClicks++;
    UpdateRatioDisplay();
}

private void UpdateRatioDisplay()
{
    if (upClicks == 0 || downClicks == 0)
        return; // wait until both buttons have been pressed

    double ratio = (double)upClicks / downClicks;

    if (ratio >= UPPER)
    {
        PicNew.Image = null; // allow BackColor to show
        PicNew.BackColor = Color.Blue;
    }
    else if (ratio <= LOWER)
    {
        PicPrevious.Image = null;
        PicPrevious.BackColor = Color.Black;
    }
    else
    {
        PicNew.BackColor = PicPrevious.BackColor = Color.Orange;
    }
}

Troubleshooting notes: if a PictureBox has an Image, BackColor may be hidden — either clear/swap the Image or use a Panel/control intended for solid color. For persisted counts (you mentioned a table), perform atomic increments on the DB (UPDATE ... SET clicks = clicks + 1) and then read back the current values to refresh the UI; use parameterized queries and transactions to avoid race conditions. For very large counters use Int64.

Recommended Answers

All 5 Replies

Is it the logic you're having trouble with or the C# GUI calls? A ratio can simple be expressed as a faction and thus a real number (i.e. the double data type). So 4:1 is really 4.0/1.0 = 4 and 1:4 is 1.0/4.0 = 0.25. It seems to me that you can simply keep track of the number of times each button is pressed (i.e. increment two separate variables accordingly); after you're done the incrementing, do the division as described above and you'll know which box to display. If I've misunderstood, I apologize; if you'd like a small algorithm, holla :icon_biggrin:

yes please a small algorithm would be nice. thanks.

Hehe okay I'm not sure how to describe this algorithm but of course each of your buttons will have a Click event on it - in this event you'd do the following for the up button:

upCount++;
doUpdateOfBox();

Where upCount and downCount represent the number of clicks made on a button. They would be int variables. In other buttons even would be

downCount++;
doUpdateOfBox();

And the method would be:

method doUpdateOfBox()
{
  double ratio =  upCount*1.0/downCount ;
  if (ratio > 4)
    changeToOrangeBox();
      else if (ratio < 0.25)
        changeToBlueBox();
          else ???
}

I'm not sure what you're supposed to do when either of the ratios isn’t satisfied :?: For example, when both buttons have been clicked the same number of times (which would be the case initially). To avoid division by zero errors, both count variables should be initialized to 1 :icon_smile: Notice that I converted your ratios into real numbers. Does that help any?

would it possible to send you a screen shot of my program so you can see what i'm trying to achieve?? thanks for that as that helped slightly.

Is your program the same as the flash application link you provided?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.