0

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.

NewHelpNeeder
  • 693
  • 1
  • 10
  • 19
  • My what deep scopes you have. Perhaps it would be easier to see with fewer layers of indention. E.G. writing `else if` as a pair. – EnabrenTane Jan 13 '12 at 05:01
  • @EnabrenTane, I can't figure out any easier solution. I have a timer inside of timer and this is confusing hell out of me. – NewHelpNeeder Jan 13 '12 at 05:03
  • Right but ` else { if` is all at the same logical scope. Then your last double if statement is almost an and condition except for when you call `SendKeys.Send` – EnabrenTane Jan 13 '12 at 05:06
  • I'm not getting the problem with your code. What's not working with that? – Ivo Jan 13 '12 at 05:08
  • @ivowiblo, the code compiles fine. The problem is what my incrementation isn't correct. My typing starts from line 1 not line 0. – NewHelpNeeder Jan 13 '12 at 05:09
  • I've added some solutions in my answer – Ivo Jan 13 '12 at 05:16

2 Answers2

1

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);
}
Ivo
  • 8,172
  • 5
  • 27
  • 42
  • I'm afraid I can't. both SendKeys.Send(lbMessage.Items[]) are somewhat different. One has numbered index, one is random index. – NewHelpNeeder Jan 13 '12 at 05:08
  • I've edited so it supports both index. Sorry for the confusion. – Ivo Jan 13 '12 at 05:12
  • I've added a way to set the start according the value of cbPause. – Ivo Jan 13 '12 at 05:14
  • Oh wow, that saved more than few lines of code. Thanks. But I want to point out that it doesn't fix my problem. But thanks anyway, nice solution. – NewHelpNeeder Jan 13 '12 at 05:18
  • Did you check the possible fix for the Interval method I've mentioned? – Ivo Jan 13 '12 at 05:20
  • Yes I did. I have inserted them right above SetInterval(); in Interval method. But it causes longer than usual space when end of lines is reached. And it doesn't let me start from 0 count. – NewHelpNeeder Jan 13 '12 at 05:23
  • ok, what I mean was to just add an `else` before your `tickCount++;` so it increases only if it didn't start over. – Ivo Jan 13 '12 at 05:26
  • I do understand and I have tried both of your solutions. Something isn't quite right. – NewHelpNeeder Jan 13 '12 at 05:30
  • and that is...? From the code, you are setting the value to 0 and then increasing the value with the ++ operator. What I suggested was to not use the ++ if you just put it to 0. I don't know what else could be wrong there, maybe you can give me more information. – Ivo Jan 13 '12 at 05:39
  • The value of 0 is only if I reach the end line of my ListBox, this is when I set it to 0. This code I have tried few times now and it causes same thing as I explained on my comment 2 comments up. I'm not sure what could be the cause. If you like I can send you my project. It's open source so I don't mind. – NewHelpNeeder Jan 13 '12 at 06:36
  • Ok. Could you explain the expected behavior of the presented code? I just can't understand what you are trying to do. – Ivo Jan 13 '12 at 07:15
  • Well, I got that working. But happen was that when cbPause is not selected then I'm starting typing with line 0; then I select cbPause then I start typing with line 1. this was my problem. – NewHelpNeeder Jan 13 '12 at 07:35
  • Ok. If you are happy, I'm happy. – Ivo Jan 13 '12 at 14:42
1

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();
}
NewHelpNeeder
  • 693
  • 1
  • 10
  • 19