18

I recently started using C# and WPF for one of my projects.

Is there a quick way of getting an input from the user? I have not been able to find one for WPF projects.

I don't want have to create another window, add OK and Cancel buttons, and add event handlers for everything. I can do it, but I wanted to know a simpler way of doing it.

AFAIK, that was possible in win forms. You can get user input with just one single line of code. Can I do it in WPF as well?

5
  • tried using the <TextBox /> control? Commented Sep 21, 2009 at 15:46
  • 1
    You can't actually compare WPF and Windows Forms directly. Both technologies work very different. And in putting together a complex application I rarely came at a point where I needed a one-line way to get input from the user. That's more a command-line interface thing, imho. Commented Sep 21, 2009 at 15:53
  • It's not clear what you're looking for... If you're talking about the MessageBox, it also exists in WPF, as mentioned by Simon. Commented Sep 21, 2009 at 16:03
  • @johannes Rossel I am not putting a complex application together. I am just writing a small application that does a few things I do very frequently and manually. Commented Sep 21, 2009 at 16:03
  • @thomas Levesque, I needed a way to get an integer from the user so a text box with ok/cancel button is what I am looking for. Commented Sep 21, 2009 at 16:05

2 Answers 2

29

If you add the Microsoft.VisualBasic dll to your application, you can use the InputBox method to get a single value from the user.

Microsoft.VisualBasic.Interaction.InputBox("Prompt here", 
                                           "Title here", 
                                           "Default data", 
                                           -1,-1);

(Put -1,-1 in for the XPos,YPos to get it centred on the screen)

Sign up to request clarification or add additional context in comments.

5 Comments

Nice one Pete, I'd totally forgotten about the InputBox, haven't used that since my VB4 days in high school.
You have to give the punters what they want =:)
I thought, that's a nice solution. But the window really looks like Windows 98. Like, Ok and Cancel Buttons are stacked vertically. The German word for "Cancel" is too long and doesn't fit. Here you can see how it looks codeproject.com/Articles/10181/InputBox-in-C
use it temporarily as a quick "fix", by wrapping it in your own extension method. Then later on, replace it with your own souped up version. Scale later ;-)
Which namespace required to using above syntax ?, for me it showing Interaction is not exist in Microsoft.VisualBasic namespace.
2

If your talking about basic yes/no input then there is a wpf MessageBox that works in pretty much the same way as the winforms one - see System.Windows.MessageBox

Is that what you are thinking of?

Also, all winforms classes can still be used in WPF apps, you just need to add a reference to the appropriate assembly.

2 Comments

No, I need to get an integer input. But text input is also fine which I can convert to integer. So a text box with OK/Cancel button is what I was looking for.
Ahh....You're thinking of the InputBox, that's a hang over from visual basic in the pre .net days. (it might still be in vb.net). Check out Pete's answer for how to get to this in C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.