-2

I'm trying to make a masked Textbox that has a partial value already set and then the user needs to enter 4 more digits then allow it to send out. I've had a hard time trying to explain but here's what it currently looks like for reference:

private void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Enter && maskedTextBox1.Text.Length == ("007744D4 0131").Length + 4)
        {
            NCInterface.ConstCodeAdd(codes[maskedTextBox1.Text], true);
        }
    }

Now the last part where it has NCInterface, is where it is being sent to the PS3 console, and where I need to know how to make sure that maskedTextBox1.Text converts properly. The maskedTextBox needs to be able to accept both numbers and letters. I have nothing to really compare this to.. But that's just where its sent from. But it just says that it cant convert type string to int. So if anyone is able to give me a hand it'd be much appreciated.

Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52
x66dme66x
  • 11
  • 1
  • 1
    Are you an actual C# developer? – Matías Fidemraizer May 20 '15 at 20:41
  • Haven't for a looong while. and the problem with just throwing that Int32.Parse in there (And I completely shouldve mentioned this in the OP. is that the entry into maskedtextbox1 can hold both numbers and letters. – x66dme66x May 20 '15 at 20:46
  • Maybe you should provide the signature of `NCInterface.ConstCodeAdd` and what it is supposed to do with this code. – Robert S. May 20 '15 at 20:48
  • Are only the 4 last digits the code you want to send? In this case use `IndexOf` to find the space and create a substring from the found index + 1 to the end. This substring can then be parsed as an `int`. – Robert S. May 20 '15 at 20:50

2 Answers2

1

Just based on your error you might do this:

NCInterface.ConstCodeAdd(int.Parse(codes[maskedTextBox1.Text]), true);
Adam Heeg
  • 1,704
  • 1
  • 14
  • 34
  • 1
    Don't forget the `try-catch` around it. I wonder if `007744D4 0131` should be a possible value (see question). `int.Parse` won't help in this case of course. But even if only valid numbers should be entered, a `try-catch` won't hurt. ;) – Robert S. May 20 '15 at 20:46
  • You just saved a lot of PS3 users some heartache :) – Adam Heeg May 20 '15 at 21:00
0

What you need to do is use TryParse

string testString = maskedTextBox1.Text;
int numberTest;
if(int.TryParse(testString, out numberTest)
{
      NCInterface.ConstCodeAdd(numberTest, true); 
}
Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52