-1

I am trying to build a scratch-like application to simulate block-based programming approach. I want the user to add the commands to command pool(queue) and run them in order when the user clicks on the "RUN" button.

I researched on the internet and found queueing is possible by Action lists.

ScreenShot of the form

    //Global Variables

    List<Int32> NumberOfSteps = new List<Int32>();
    List<Int32> NumberOfDegrees = new List<Int32>();
    List<Int32> GlideXPos = new List<Int32>();
    List<Int32> GlideYPos = new List<Int32>();
    List<String> SayList = new List<String>();
    List<String> SoundList = new List<String>();


    List<Action> queue = new List<Action>();



    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox7_TextChanged(object sender, EventArgs e)
    {

    }

    private static Bitmap RotateImageByAngle(Image oldBitmap, float angle)
    {
        var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
        var graphics = Graphics.FromImage(newBitmap);
        graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
        graphics.RotateTransform(angle);
        graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
        graphics.DrawImage(oldBitmap, new Point(0, 0));
        return newBitmap;
    }

    private void button3_Click(object sender, EventArgs e)
    {

         pictureBox1.Image = RotateImageByAngle(pictureBox1.Image, 30);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        queue.Add(() => { HAREKETETTIR(Convert.ToInt32(txtNumberOfSteps.Text)); });

    }

    private void HAREKETETTIR(int miktar)
    {
        deneme2 += miktar;
        Point deneme = new Point(deneme2, 455);
        pictureBox1.Location = deneme;

    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }


    private void btnRotate_Click(object sender, EventArgs e)
    {
        queue.Add(() => { HAREKETETTIR(Convert.ToInt32(txtNumberOfSteps.Text)); });

    }

    private void RUN_Click(object sender, EventArgs e)
    {
        foreach (Action item in queue)
        {
            item();
        }
    }
}

When I click Run,The code creates an error as "Input string was not in a correct format."

How can I solve the issue here?

Thanks

user2394800
  • 11
  • 1
  • 3

2 Answers2

3

Your question does not contain information, which part of program produces error, but it is probably Convert.ToInt32(txtNumberOfSteps.Text).

First reason of error could be that txtNumberOfSteps.Text does not contain text convertible to integer, so make sure user can enter only valid integer or provide appropriate error handling (e.g. check txtNumberOfSteps.Text before you enqueue action and display user some error message).

Second reason is more tricky to spot. If you add Action to queue like this

queue.Add(() => { HAREKETETTIR(Convert.ToInt32(txtNumberOfSteps.Text)); });

then method Convert.ToInt32() would try to convert value of txtNumberOfSteps.Text as it is in the moment you run the action, not as it was when you queued the action. So for example if user queues one action when txtNumberOfSteps.Text contains "2" and then he changes txtNumberOfSteps.Text to empty string and run actions in queue, you would received error, because empty string is not valid integer. This can be easily fixed by creating temporary variable containing copy of value in txtNumberOfSteps.Text in the moment of enqueing the action.

private void button1_Click(object sender, EventArgs e)
{
    var text = txtNumberOfSteps.Text;
    queue.Add(() => { HAREKETETTIR(Convert.ToInt32(text)); });
}

(and the same problem is in btnRotate_Click() method).

You can read about this problem in more detail here.

Community
  • 1
  • 1
Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • @user2394800: I'm glad to help you, so please don't forget to mark answer as accepted as it is [preferred way of saying thank you](http://meta.stackoverflow.com/questions/258004/should-thank-you-comments-be-flagged) on StackOverflow. – Ňuf Dec 11 '16 at 10:48
0

It looks to me like your error is because txtNumberOfSteps.Text is actually an invalid number. You are adding the actions to your queue correctly, and running them correctly as far as I can see.

gmn
  • 4,199
  • 4
  • 24
  • 46