2011-11-12 45 views
17

私はオンラインでリサーチしましたが、私が望んでいるかわからない解決策は見つかりませんでした。WPF C#InputBox

私はC#を使用してWPFアプリケーションを実行しています。私は彼/彼女の名前を入力するユーザーを求めるダイアログボックスが表示されます。その後、私は名前を追跡し、名前を使ってtxtファイルにいくつかのデータを保存します。例について

名入力 名= "ジョン"

そして私はデータを持っている データ= "1、2、3" です。

そしてJohn.txtファイルに "data"を保存します。

誰でも知っていますか?私は問題は、ユーザーが名前を入力するためのダイアログを表示する方法だと思います。

ありがとうございました!

+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から入力テキストを取得するだけです。

ShowDialog()が必要なコードを使用する代わりに、VisibilityオプションをVisibleに設定するだけです。この例では、Yes/Noボタンのクリックを処理した後に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-projectで別のWindowクラスを作成するだけで、パブリックプロパティでユーザー名を保持できます。次に、メインウィンドウのどこかにこのウィンドウのインスタンスを作成し、ShowDialogメソッドを使用して表示します。これは、あなたの「ダイアログ」ウィンドウが閉じられるまでブロックされます。その後、パブリックプロパティからユーザー名を取得し、それに必要なものを実行できます。

3

ユーザーが入力するプロジェクトに新規のWindowを作成/追加します。あなたは、その後もOKボタンを追加するポップアップウィンドウ

としてそのウィンドウを表示するにはWindow.ShowまたはWindow.ShowDialogを使用するn個のウィンドウを作成し、[OK]ボタンを上のカスタムダイアログボックス上のテキストファイル

1

セクションの情報を保存する]をクリックすることができますMSDNは、あなたにいくつかの指針を与えるかもしれません:Custom Dialog Box in WPF。コードサンプルとXAMLソースもあります。

これを処理すると、データをファイルに保存する方法を検索することができます。これはかなり簡単です。その1つはTextWriterクラス(example)を使用しています。

4

私の解決策は、私はそれを入力するのに約3時間かかりました。それは完全にカスタマイズ可能です。

文字列var =新しい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; 
      } 


     } 

では、それが有用であることを願って。