WPF C#InputBox

我在网上进行了研究,但没有找到我想要或不清楚的解决方案。

我正在使用C#进行WPF应用程序。 我想弹出一个对话框,提示用户输入他/她的名字。 之后,我将跟踪名称并使用名称将一些数据保存到txt文件中。

例如,

名称输入是name =“John”

所以我有一个数据数据=“1,2,3”;

然后我将“数据”保存在John.txt文件中。

谁知道怎么做? 我认为问题是如何弹出一个对话框供用户输入名称。

谢谢!

我更喜欢使用不会锁定应用程序的对话框,并远离更传统的Win32对话框。

输入对话框

隐藏输入对话框

输入对话框未显示。

在这个例子中,我使用了我用于我的应用程序的基于MVVM的解决方案的简化版本。 它可能不是很漂亮,但应该给你一个坚实的想法背后的基础知识。

XAML:

                     

显示此对话框非常容易,因为您只需要将InputBox网格的可见性设置为可见。 然后,您只需处理Yes / No按钮并从TextBox中获取Input文本。

因此,您只需将Visibility选项设置为Visible ,而不是使用需要ShowDialog()代码。 在这个例子中还有一些事情我们将在代码隐藏中处理,例如在处理Yes / No Button点击后清除InputText框。

代码隐藏:

 namespace WpfApplication1 { ///  /// Interaction logic for MainWindow.xaml ///  public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void CoolButton_Click(object sender, RoutedEventArgs e) { // CoolButton Clicked! Let's show our InputBox. InputBox.Visibility = System.Windows.Visibility.Visible; } private void YesButton_Click(object sender, RoutedEventArgs e) { // YesButton Clicked! Let's hide our InputBox and handle the input text. InputBox.Visibility = System.Windows.Visibility.Collapsed; // Do something with the Input String input = InputTextBox.Text; MyListBox.Items.Add(input); // Add Input to our ListBox. // Clear InputBox. InputTextBox.Text = String.Empty; } private void NoButton_Click(object sender, RoutedEventArgs e) { // NoButton Clicked! Let's hide our InputBox. InputBox.Visibility = System.Windows.Visibility.Collapsed; // Clear InputBox. InputTextBox.Text = String.Empty; } } } 

代码隐藏可以使用Dependency轻松完成,或者在本例中使用ViewModel逻辑,但为了简单起见,我将其保留在代码隐藏中。

这是我的解决方案,我花了大约3个小时来打字。 它完全可定制。

 string var = new InputBox("text").ShowDialog(); 

这是class级的代码

 public class InputBox { Window Box = new Window();//window for the inputbox FontFamily font = new FontFamily("Tahoma");//font for the whole inputbox int FontSize=30;//fontsize for the input StackPanel sp1=new StackPanel();// items container string title = "InputBox";//title as heading string boxcontent;//title string defaulttext = "Scrivi quì il tuo nome...";//default textbox content string errormessage = "Scelta non valida";//error messagebox content string errortitle="Errore";//error messagebox heading title string okbuttontext = "OK";//Ok button content Brush BoxBackgroundColor = Brushes.GreenYellow;// Window Background Brush InputBackgroundColor = Brushes.Ivory;// Textbox Background bool clicked = false; TextBox input = new TextBox(); Button ok = new Button(); bool inputreset = false; public InputBox(string content) { try { boxcontent = content; } catch { boxcontent = "Error!"; } windowdef(); } public InputBox(string content,string Htitle, string DefaultText) { try { boxcontent = content; } catch { boxcontent = "Error!"; } try { title = Htitle; } catch { title = "Error!"; } try { defaulttext = DefaultText; } catch { DefaultText = "Error!"; } windowdef(); } public InputBox(string content, string Htitle,string Font,int Fontsize) { try { boxcontent = content; } catch { boxcontent = "Error!"; } try { font = new FontFamily(Font); } catch { font = new FontFamily("Tahoma"); } try { title = Htitle; } catch { title = "Error!"; } if (Fontsize >= 1) FontSize = Fontsize; windowdef(); } private void windowdef()// window building - check only for window size { Box.Height = 500;// Box Height Box.Width = 300;// Box Width Box.Background = BoxBackgroundColor; Box.Title = title; Box.Content = sp1; Box.Closing += Box_Closing; TextBlock content=new TextBlock(); content.TextWrapping = TextWrapping.Wrap; content.Background = null; content.HorizontalAlignment = HorizontalAlignment.Center; content.Text = boxcontent; content.FontFamily = font; content.FontSize = FontSize; sp1.Children.Add(content); input.Background = InputBackgroundColor; input.FontFamily = font; input.FontSize = FontSize; input.HorizontalAlignment = HorizontalAlignment.Center; input.Text = defaulttext; input.MinWidth = 200; input.MouseEnter += input_MouseDown; sp1.Children.Add(input); ok.Width=70; ok.Height=30; ok.Click += ok_Click; ok.Content = okbuttontext; ok.HorizontalAlignment = HorizontalAlignment.Center; sp1.Children.Add(ok); } void Box_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if(!clicked) e.Cancel = true; } private void input_MouseDown(object sender, MouseEventArgs e) { if ((sender as TextBox).Text == defaulttext && inputreset==false) { (sender as TextBox).Text = null; inputreset = true; } } void ok_Click(object sender, RoutedEventArgs e) { clicked = true; if (input.Text == defaulttext||input.Text == "") MessageBox.Show(errormessage,errortitle); else { Box.Close(); } clicked = false; } public string ShowDialog() { Box.ShowDialog(); return input.Text; } } 

希望它有用。

只需在Visual Studio项目中创建另一个Window类,该类将用户名保存在公共属性中。 然后在主窗口的某处创建此窗口的实例,并使用ShowDialog方法显示它。 这将阻止,直到您的“对话框”窗口关闭。 然后,您可以从公共财产中获取用户名,并随意使用它。

在项目中创建/添加一个新Window ,以便从用户那里获取输入。 然后,您可以使用Window.ShowWindow.ShowDialog将该窗口显示为弹出窗口

同时添加一个OK按钮n创建的窗口,然后单击确定按钮单击保存文本文件中的信息

MSDN上的自定义对话框部分可能会为您提供一些指导: WPF中的自定义对话框 。 还有代码示例和XAML源代码。

处理完毕后,您可以搜索如何将数据保存到文件中 – 这很简单,并且有很多方法可以做到这一点(其中一种方法是使用TextWriter类: 示例 )。