2

I'm using an InputBox from the Visual Basic Library and I need to determine which button has been selected by the end user. With the InputBox you can only grab the Entered Value by the user, I also need to detect if they hit Ok or Cancel.

Has anyone tried doing this before?

I know I could create a new form and do it that way, but wondering there's a workaround for this??

string answer =  Interaction.InputBox("Question","Title","");

I can determine if the user has input anything by the answer.length, but I need to know whether they hit "Cancel" or "Ok" after typing something as I need to abort on "Cancel".

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Derek
  • 8,300
  • 12
  • 56
  • 88

1 Answers1

1

You can't, except for checking the string length.

If the user clicks Cancel, a zero-length string is returned.

So if you want to be able to distinguish between a click of the cancel button and the fact the user just entered an empty string, you're out of luck.

But there are plenty of custom implementations out there, like this one on codeplex, which will return a DialogResult.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • Here lies the problem i think, as if the user doesnt enter any info, i need to advise them that they cant proceed without entering a value. So if they hit ok, with no value, i cant distinguish between that and teh cancel button being pressed. If that makes sense? – Derek Sep 20 '12 at 13:06
  • 1
    @Derek Yes, that's the problem with `Interaction.InputBox`. Better use [this one](http://inputbox.codeplex.com/). – sloth Sep 20 '12 at 13:08
  • I have decided to make my own control, rather than using the one from codeplex one. Thanks for the help on this. – Derek Sep 20 '12 at 16:38