I am building a WPF application using C#. I want to pop out a dialog box to prompt the user to enter his/her name. After that, I will keep track of the name and save some data into a .txt file using the name.
For example:
Name input is
name = "John"
And so I have data
data="1, 2, 3";
and then I save the "data" in John.txt file.
Does anyone know how to do it?
I think the problem is how to pop out a dialog for the user to enter name.
I prefer to take an approach using dialogs that doesn’t lock up the application, and moves away from the more traditional Win32 Dialog.
Example
Input Dialog Hidden
In this example I use a simplified version of the MVVM based solution I am using for my applications. It may not be pretty, but should give you a solid idea on the basics behind it.
The XAML:
It’s very easy to show this dialog as you only need to set the Visibility of the
InputBoxgrid to visible. You then simply handle the Yes / No buttons and get the Input text from the TextBox.So instead of using code that requires
ShowDialog(), you simply set theVisibilityoption toVisible. There are still some things to do in this example that we will handle in code-behind, like for example clearing the InputText box after handling the Yes/No Button clicks.The code-behind:
The code-behind could easily be done using a Dependency, or as ViewModel logic in this case, but for simplicity I kept it in the code-behind.