Hello there,
I got some strings of "x10" "x11" "x2" and stuff like that.
Is there a way to take only the number from the string and convert it to int ?
lets say

label1.Test = "x11";

I want to take the 11 to some int.

If you know all your strings start with "x", you could use:

string test = "x11";
            test = test.Remove(0, 1);//remove  1 char at position 0 
            int i = int.Parse(test);

You can take part of the string using substring.

string s="x10";
            int i = int.Parse(s.Substring(1));

@JuhaW perhaps your intentions are good but should you not try to test code before you post it?
The Substring method is indeed another way to go, but then perhaps with this overload : http://msdn.microsoft.com/en-us/library/aka44szs.aspx
The code you provided gives "1" instead of "10".

@ddanbe

It hurts to have to correct you! But Juha's code does return 10 for i.

The overload he used will return the portion of the string from starting index specified (1) all the way until the end of the string.

Hi guys, I simply cant get this thing to work.

After I hide Form 2 and back to Form 1 [look at the code] and then Show Form 2 again.. the labels still show the old numbers...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Stratego
{

    public partial class Form2 : Form
    {

        int i = 1;
        //int j;
        int s1, s2, s3, s4, s5, s6, s7, s8, s9, sb, sf, ss;

        public Form2(Form1 frm1Handle) //Handle Form1 stuff.
        {
            InitializeComponent(); //Handle Form1 stuff.
            HandleToForm1 = frm1Handle; //Handle Form1 stuff.
        }

        private Form1 HandleToForm1; //Handle Form1 stuff.

        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            //Get a key press
            int key = (int)e.KeyCode;

            if ((key == (int)Keys.Up) && (pictureBox2.Location.Y > 17))
            {
                pictureBox2.Top -= 48;
                i -= 3;
            }

            if ((key == (int)Keys.Down) && (pictureBox2.Location.Y < 161))
            {
                pictureBox2.Top += 48;
                i += 3;
            }

            if ((key == (int)Keys.Left) && (pictureBox2.Location.X > 7))
            {
                pictureBox2.Left -= 40;
                i--;
            }

            if ((key == (int)Keys.Right) && (pictureBox2.Location.X < 87))
            {
                pictureBox2.Left += 40;
                i++;
            }

            if (key == (int)Keys.Enter)
            {
                string ps1 = xSolis1.Text.Remove(0, 1);
                s1 = int.Parse(ps1);
                string ps2 = xSolis2.Text.Remove(0, 1);
                s2 = int.Parse(ps2);
                string ps3 = xSolis3.Text.Remove(0, 1);
                s3 = int.Parse(ps3);
                string ps4 = xSolis4.Text.Remove(0, 1);
                s4 = int.Parse(ps4);
                string ps5 = xSolis5.Text.Remove(0, 1);
                s5 = int.Parse(ps5);
                string ps6 = xSolis6.Text.Remove(0, 1);
                s6 = int.Parse(ps6);
                string ps7 = xSolis7.Text.Remove(0, 1);
                s7 = int.Parse(ps7);
                string ps8 = xSolis8.Text.Remove(0, 1);
                s8 = int.Parse(ps8);
                string ps9 = xSolis9.Text.Remove(0, 1);
                s9 = int.Parse(ps9);
                string ps10 = xSolis10.Text.Remove(0, 1);
                sb = int.Parse(ps10);
                string ps11 = xSolis11.Text.Remove(0, 1);
                sf = int.Parse(ps11);
                string ps12 = xSolis12.Text.Remove(0, 1);
                ss = int.Parse(ps12);

                if (i == 1)
                {
                    s1--;
                    HandleToForm1.SetImage(Properties.Resources.b9);
                    xSolis1.Text = "";
                    xSolis1.Text = "x"+s1;
                }
                if (i == 2)
                {
                    s2--;
                    HandleToForm1.SetImage(Properties.Resources.b8);
                    xSolis2.Text = "";
                    xSolis2.Text = "x" + s2;
                }
                if (i == 3)
                {
                    s3--;
                    HandleToForm1.SetImage(Properties.Resources.b7);
                    xSolis3.Text = "";
                    xSolis3.Text = "x" + s3;
                }
                if (i == 4)
                {
                    s4--;
                    HandleToForm1.SetImage(Properties.Resources.b6);
                    xSolis4.Text = "";
                    xSolis4.Text = "x" + s4;
                }
                if (i == 5)
                {
                    s5--;
                    HandleToForm1.SetImage(Properties.Resources.b5);
                    xSolis5.Text = "";
                    xSolis5.Text = "x" + s5;
                }
                if (i == 6)
                {
                    s6--;
                    HandleToForm1.SetImage(Properties.Resources.b4);
                    xSolis6.Text = "";
                    xSolis6.Text = "x" + s6;
                }
                if (i == 7)
                {
                    s7--;
                    HandleToForm1.SetImage(Properties.Resources.b3);
                    xSolis7.Text = "";
                    xSolis7.Text = "x" + s7;
                }
                if (i == 8)
                {
                    s8--;
                    HandleToForm1.SetImage(Properties.Resources.b2);
                    xSolis8.Text = "";
                    xSolis8.Text = "x" + s8;
                }
                if (i == 9)
                {
                    s9--;
                    HandleToForm1.SetImage(Properties.Resources.b1);
                    xSolis9.Text = "";
                    xSolis9.Text = "x" + s9;
                }
                if (i == 10)
                {
                    sb--;
                    HandleToForm1.SetImage(Properties.Resources.bb);
                    xSolis10.Text = "";
                    xSolis10.Text = "x" + sb;
                }
                if (i == 11)
                {
                    sf--;
                    HandleToForm1.SetImage(Properties.Resources.bf);
                    xSolis11.Text = "";
                    xSolis11.Text = "x" + sf;
                }
                if (i == 12)
                {
                    ss--;
                    HandleToForm1.SetImage(Properties.Resources.bs);
                    xSolis12.Text = "";
                    xSolis12.Text = "x" + ss;
                }

                HandleToForm1.SetBoard(i + 100);
                Hide();
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
        }

    }
}

and this is from Form1 when I reshow Form2.

if (key == (int)Keys.Enter)
            {
                int startposx = ((resx / 2) - 192);
                int startposy = ((resy / 2) - 192);
                Form2 Form2 = new Form2(this);
                Form2.StartPosition = FormStartPosition.Manual;
                Form2.Location = new Point(((startposx) + (43 * j)), ((startposy) + (43 * i)));
                Form2.Show();
            }
Form2 Form2 = new Form2(this);

This is creating a new instance of Form2. The new instance is not going to inherently show any of the changes you may have made in a prior instance.

You will have to (a) persist the updated data in a structure not in Form2 (but accessible to it) or (b) figure out a way to prevent the closing and disposal of your Form2 instances so you can work with the same object rather than new instances.

@ddanbe

It hurts to have to correct you! But Juha's code does return 10 for i.

The overload he used will return the portion of the string from starting index specified (1) all the way until the end of the string.

:'(:$ You are quite right to correct me! It was very late in the evening and I was testing Substring on a string with the "x" already removed...:@
My excuses to JuhaW.
I guess I'm human, I make mistakes.
Keep up the good work apegram!

ty apegram. I am recoding this part.
EDIT : it still doesnt work. I am backuping the information in Form1 and then access to the information from Form2 and change it when I want to.

EDIT2: I put this thing in Form1_Load so whenever form1 load its reset the default values :X
Where can I backup my information so its only will set this info in the first time ?

form2Backup[0] = 8;
                form2Backup[1] = 5;
                form2Backup[2] = 4;
                form2Backup[3] = 4;
                form2Backup[4] = 4;
                form2Backup[5] = 3;
                form2Backup[6] = 2;
                form2Backup[7] = 1;
                form2Backup[8] = 1;
                form2Backup[9] = 6;
                form2Backup[10] = 1;
                form2Backup[11] = 1;

Just for knowledge, is there a method to :
Lets say I got 12 int[s] s1, s2, s3 ... s12.
and I got an array "new int[12]".

I want to update each one of the int[s] to his place in the arrat. like int[0] = s1, int[1] = s2...
without re typing each one.


What I did now is

for (int d = 0; d <= 11; d++)
                {
                    form1Backup[d] = HandleToForm1.getBackup(d);
                }

                s1 = form1Backup[0];
                s2 = form1Backup[1];
                s3 = form1Backup[2];
                s4 = form1Backup[3];
                s5 = form1Backup[4];
                s6 = form1Backup[5];
                s7 = form1Backup[6];
                s8 = form1Backup[4];
                s9 = form1Backup[8];
                s10 = form1Backup[9];
                s11 = form1Backup[10];
                s12 = form1Backup[11];
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.