2011-11-12 55 views
17

我在線研究,但沒有找到解決方案,我想要或不清楚它。WPF C#輸入框

我正在做一個使用C#的WPF應用程序。我想彈出一個對話框來提示用戶輸入他/她的名字。之後,我將跟蹤名稱並使用名稱將一些數據保存到txt文件中。

例如

名稱輸入是 名稱= 「約翰」

所以我有一個數據 數據= 「1,2,3」;

然後我在John.txt文件中保存「數據」。

任何人都知道該怎麼做?我認爲問題是如何彈出用戶輸入名稱的對話框。

謝謝!

+0

看看http://social.msdn.microsoft.com/Forums/en/winforms/thread/191ddf61-3ae5-4845-b852-56bb9b77238a。 – mjwills

回答

30

我更喜歡採用不會鎖定應用程序的對話框,並從較傳統的Win32對話框移開。

Input Dialog

輸入對話框隱藏

Input Dialog not showing.

在這個例子中我使用MVVM基於溶液我使用我的應用程序的簡化版本。這可能不太好,但應該給你一個關於它背後的基礎知識的堅實想法。

的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> 

這很容易顯示此對話框,你只需要在InputBox網格的可見性設置爲可見。然後您只需處理Yes/No按鈕並從TextBox中獲取輸入文本。

因此,您只需將Visibility選項設置爲Visible即可,而不是使用需要ShowDialog()的代碼。在這個例子中還有一些事情要做,我們將在代碼隱藏中處理,例如在處理「是/否」按鈕點擊之後清除InputText框。

代碼隱藏:

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; 
     } 
    } 
} 

代碼隱藏可以很容易地使用依賴在這種情況下完成的,或作爲視圖模型的邏輯,但爲了簡單起見,我將它裝在代碼隱藏。

+1

是的,我明白了。謝謝! – user981924

+0

如何防止'InputBox'控件在顯示時失去焦點? – Marc

+0

@Marc我已經創建了一個基於我的對話框的ContentControl元素的自定義控件,並在裏面我重寫OnVisibleChanged,如果對話框可見,我設置'Keyboard.Focus(textBox);'。 – eandersson

4

只需在Visual Studio項目中創建另一個Window類,該項目將用戶名保存在公共屬性中。然後在主窗口的某個位置創建此窗口的實例,並使用ShowDialog方法顯示它。這會阻塞,直到您的「對話框」窗口關閉。然後,您可以從公共財產獲取用戶名,並根據您的需要做任何事情。

3

在您的項目中創建/添加新的Window以便從用戶處獲得輸入。然後,您可以使用Window.ShowWindow.ShowDialog表明窗口彈出窗口

還添加了一個OK鍵N創建的窗口和OK鍵單擊保存的信息在自定義對話框上的文本文件

1

科MSDN可能會給你一些指導:Custom Dialog Box in WPF。還有代碼示例和XAML源代碼。

處理完畢後,您可以搜索如何將數據保存到文件 - 這相當簡單,並且有很多方法可以做到這一點(其中一個使用TextWriter類:example)。

4

這是我的解決方案,我花了大約3小時鍵入它。它是完全可定製的。

string var = new InputBox(「text」);

這是類

 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; 
      } 


     } 

希望它可以成爲有用的代碼。