WPF C#输入框
我正在使用C#构建一个WPF应用程序。我想弹出一个对话框,提示用户输入他/她的名字。之后,我将跟踪该名称,并使用该名称将一些数据保存到WPF C#输入框,c#,wpf,visual-studio,xaml,visual-studio-2010,C#,Wpf,Visual Studio,Xaml,Visual Studio 2010,我正在使用C#构建一个WPF应用程序。我想弹出一个对话框,提示用户输入他/她的名字。之后,我将跟踪该名称,并使用该名称将一些数据保存到.txt文件中 例如: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/
.txt
文件中
例如:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Content="Cool Button" x:Name="CoolButton" Click="CoolButton_Click"/>
<ListBox x:Name="MyListBox"/>
</StackPanel>
<!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! -->
<Grid x:Name="InputBox" Visibility="Collapsed">
<Grid Background="Black" Opacity="0.5"/>
<Border
MinWidth="250"
Background="Orange"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="0,55,0,55"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel>
<TextBlock Margin="5" Text="Input Box:" FontWeight="Bold" FontFamily="Cambria" />
<TextBox MinWidth="150" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="InputTextBox"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="YesButton" Margin="5" Content="Yes" Background="{x:Null}" Click="YesButton_Click"/>
<Button x:Name="NoButton" Margin="5" Content="No" Background="{x:Null}" Click="NoButton_Click" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
</Grid>
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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;
}
}
}
名称输入为name=“约翰” 所以我有数据
data=“1,2,3” 然后我将“数据”保存在
John.txt
文件中
有人知道怎么做吗
我认为问题在于如何弹出一个对话框供用户输入用户名。只需在Visual Studio项目中创建另一个窗口类,它将用户名保存在公共属性中。然后在主窗口的某个位置创建此窗口的实例,并使用ShowDialog方法显示它。这会一直阻止,直到“对话框”窗口关闭。然后,您可以从公共属性获取用户名,并对其执行任何操作。在项目中创建/添加一个新的
窗口,以获取用户的输入。然后可以使用Window.Show
或Window.ShowDialog
将该窗口显示为弹出窗口
另外,在“已创建”窗口中添加一个“确定”按钮,并在“确定”按钮上单击“在文本文件中保存信息”MSDN上的自定义对话框部分可能会为您提供一些指导:。还有代码示例和XAML源代码
一旦你解决了这个问题,你就可以搜索如何将数据保存到文件中——这相当简单,而且有多种方法可以实现(其中之一是使用TextWriter
class:)。我更喜欢使用不会锁定应用程序的对话框,并且远离更传统的Win32对话框
示例
隐藏输入对话框
在本例中,我使用了应用程序使用的基于解决方案的简化版本。它可能并不漂亮,但应该给你一个坚实的想法背后的基本知识
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Content="Cool Button" x:Name="CoolButton" Click="CoolButton_Click"/>
<ListBox x:Name="MyListBox"/>
</StackPanel>
<!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! -->
<Grid x:Name="InputBox" Visibility="Collapsed">
<Grid Background="Black" Opacity="0.5"/>
<Border
MinWidth="250"
Background="Orange"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="0,55,0,55"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel>
<TextBlock Margin="5" Text="Input Box:" FontWeight="Bold" FontFamily="Cambria" />
<TextBox MinWidth="150" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="InputTextBox"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="YesButton" Margin="5" Content="Yes" Background="{x:Null}" Click="YesButton_Click"/>
<Button x:Name="NoButton" Margin="5" Content="No" Background="{x:Null}" Click="NoButton_Click" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
</Grid>
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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;
}
}
}
在本例中,代码隐藏可以使用依赖项或作为ViewModel逻辑轻松完成,但为了简单起见,我将其保留在代码隐藏中。以下是我的解决方案。它是完全可定制的
string inputRead = new InputBox("text").ShowDialog();
或者,如果你愿意的话
string inputRead= new InputBox("Insert something", "Title", "Arial", 20).ShowDialog()
这是这个类的代码
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 = "Write here your name...";//default textbox content
string errormessage = "Invalid answer";//error messagebox content
string errortitle="Error";//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;
}
}
希望它能有用。谢谢!!我的修改版本:
public class InputBox
{
Window Box = new Window();//window for the inputbox
FontFamily font = new FontFamily("Avenir");//font for the whole inputbox
int FontSize = 14;//fontsize for the input
StackPanel sp1 = new StackPanel();// items container
string title = "Dica s.l.";//title as heading
string boxcontent;//title
string defaulttext = "";//default textbox content
string errormessage = "Datos no válidos";//error messagebox content
string errortitle = "Error";//error messagebox heading title
string okbuttontext = "OK";//Ok button content
string CancelButtonText = "Cancelar";
Brush BoxBackgroundColor = Brushes.WhiteSmoke;// Window Background
Brush InputBackgroundColor = Brushes.Ivory;// Textbox Background
bool clickedOk = false;
TextBox input = new TextBox();
Button ok = new Button();
Button cancel = 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 = 100;// Box Height
Box.Width = 450;// Box Width
Box.Background = BoxBackgroundColor;
Box.Title = title;
Box.Content = sp1;
Box.Closing += Box_Closing;
Box.WindowStyle = WindowStyle.None;
Box.WindowStartupLocation = WindowStartupLocation.CenterScreen;
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;
input.KeyDown += input_KeyDown;
sp1.Children.Add(input);
ok.Width = 70;
ok.Height = 30;
ok.Click += ok_Click;
ok.Content = okbuttontext;
cancel.Width = 70;
cancel.Height = 30;
cancel.Click += cancel_Click;
cancel.Content = CancelButtonText;
WrapPanel gboxContent = new WrapPanel();
gboxContent.HorizontalAlignment = HorizontalAlignment.Center;
sp1.Children.Add(gboxContent);
gboxContent.Children.Add(ok);
gboxContent.Children.Add(cancel);
input.Focus();
}
void Box_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//validation
}
private void input_MouseDown(object sender, MouseEventArgs e)
{
if ((sender as TextBox).Text == defaulttext && inputreset == false)
{
(sender as TextBox).Text = null;
inputreset = true;
}
}
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && clickedOk == false )
{
e.Handled = true;
ok_Click(input, null);
}
if (e.Key == Key.Escape)
{
cancel_Click(input, null);
}
}
void ok_Click(object sender, RoutedEventArgs e)
{
clickedOk = true;
if (input.Text == defaulttext || input.Text == "")
MessageBox.Show(errormessage, errortitle,MessageBoxButton.OK,MessageBoxImage.Error);
else
{
Box.Close();
}
clickedOk = false;
}
void cancel_Click(object sender, RoutedEventArgs e)
{
Box.Close();
}
public string ShowDialog()
{
Box.ShowDialog();
return input.Text;
}
}
看看。当你遇到第二个叫“约翰”的用户时,可能会发生什么?是的,我想到了。非常感谢。如何防止“InputBox”控件在显示时失去焦点?@Marc我基于ContentControl元素为我的对话框创建了一个自定义控件,在该控件中我覆盖了OnVisibleChanged,如果对话框可见,我将设置Keyboard.focus(textBox)代码>@eandersson正常,但如何防止失去焦点?当控件可见且用户多次按tab键时,焦点将显示在此用户控件后面的控件。我想防止这种情况发生。它应该保留在用户控件中。@eandersson I通过禁用(IsEnabled=false)所有基础控件解决了问题。