简体   繁体   中英

I can't figure out where to put my incrementing variable

Ok, here's my problem. When I check check box cbPause ( cbPause = true ) then my count start 1 not 0 . The counter is named tickCount and if I put tickCount inside of if (cbPause.Checked == true) . If I put it ad the end of my Interval method it also starts with 1 and not 0. Where should I place incrementing variable, in my situation, so it starts with 0? I must have tickCount++; somewhere, but I don't know where.

PS. tickCount is used to see which line I must type. It starts from 0 so I type line 0 from ListBox.

This is a main timer counts time between each message typed:

private void Interval(object sender, EventArgs e)
{
    if (cbPause.Checked == true) 
    {
       randomLine = random.Next(lbMessage.Items.Count); 
       tmrSpace.Enabled = true;
    }
    else
    {
        if (cbRandomLine.Checked == true)
        {
            SendKeys.Send(lbMessage.Items[random.Next(lbMessage.Items.Count)].ToString() + "{enter}");
        }
        else
        {
            if (tickCount < lbMessage.Items.Count)
            {
                SendKeys.Send(lbMessage.Items[tickCount].ToString() + "{enter}");
                if (tickCount == lbMessage.Items.Count) tickCount = 0;
                tickCount++;
            }
        }
    }

    SetInterval();
}

This method is a second timer which allows me to type like a typewriter which types string with small time spaces between each character.

private void Space(object sender, EventArgs e)
{
    if (cbRandomLine.Checked == true)
    {
        SendKeys.Send(lbMessage.Items[randomLine].ToString().Substring(currentChar++, 1));

        if (currentChar == lbMessage.Items[randomLine].ToString().Length)
        {
            SendKeys.Send("{enter}");
            tmrSpace.Enabled = false;
            currentChar = 0;
        }
    }
    else
    {
        if (tickCount < lbMessage.Items.Count)
        {
            SendKeys.Send(lbMessage.Items[tickCount].ToString().Substring(currentChar++, 1));

            if (currentChar == lbMessage.Items[tickCount].ToString().Length)
            {
                SendKeys.Send("{enter}");
                tmrSpace.Enabled = false;
                currentChar = 0;
            }
        }
    }

    tmrSpace.Interval = random.Next(50, 100);
}

Any tips are highly appreciated. Thank you in advance.

Have you tried this?

if (tickCount == lbMessage.Items.Count)
   tickCount = cbPause.Checked ? 1 : 0;
else
   tickCount++;

Anyway, in your code, cbPause.Checked is always false when you hit that part, so it could be just:

if (tickCount == lbMessage.Items.Count)
   tickCount = 0;
else
   tickCount++;

Also you can rewrite the space method like this:

private void Space(object sender, EventArgs e)
{
    if (cbRandomLine.Checked || tickCount < lbMessage.Items.Count)
    {
        var index = cbRandomLine.Checked ? randomLine : tickCount;
        var item = lbMessage.Items[index ].ToString();

        SendKeys.Send(item.Substring(currentChar++, 1));

        if (currentChar == item.Length)
        {
            SendKeys.Send("{enter}");
            tmrSpace.Enabled = false;
            currentChar = 0;
        }
    }

    tmrSpace.Interval = random.Next(50, 100);
}

This is how I have done it. Just added one bool check:

private void Interval(object sender, EventArgs e)
{
    if (cbPause.Checked) 
    {
       randomLine = random.Next(lbMessage.Items.Count); 
       tmrSpace.Enabled = true;

       if (whenStart)
           tickCount++;
       else whenStart = true;
    }
    else
    {
        if (cbRandomLine.Checked)
        {
            SendKeys.Send(lbMessage.Items[random.Next(lbMessage.Items.Count)].ToString() + "{enter}");
        }
        else
        {
            if (tickCount < lbMessage.Items.Count)
            {
                SendKeys.Send(lbMessage.Items[tickCount].ToString() + "{enter}");
                tickCount++;
            }
        }
    }

    if (tickCount == lbMessage.Items.Count) tickCount = 0;

    SetInterval();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM