There was a need to create a window for entering the file name which will be created later. Creating a window is easy, but I cannot think of how to return data from this window. Is there any way to determine if a Click() event has occurred Or tell me more competent or reasonable ways. I would be glad even to the direction in which to look.

The code for the window being created:

 class InputBox : Window { //private static InputBoxDialog; private static TextBox textBox; private static Button button; private static Label label; private static Window NewWindow; private static Window CurrentWindow; private static string TextVar; public InputBox() { } public InputBox(ref Window textVar) { } public void Show(string labelContent) { CreateBox(labelContent); } public void Show(string labelContent, string titleContent) { CreateBox(labelContent, titleContent); } public void Show(string labelContent, string titleContent, string buttonContent) { CreateBox(labelContent, titleContent, buttonContent); } private void CreateBox(string labelContent, string titleContent = "Input Box", string buttonContent = "Enter") { NewWindow = new Window(); NewWindow.Name = "NewWindow"; NewWindow.Title = titleContent; NewWindow.Width = 300; NewWindow.Height = 200; var stackPanel = new StackPanel { Orientation = Orientation.Vertical }; label = new Label { Content = labelContent, Name = "NewWindowLabel" }; textBox = new TextBox { Text = "", Name = "NewWindowTextBox" }; button = new Button { Content = buttonContent, Name = "NewWindowButton" }; button.Click += NewWindowButton_Click; stackPanel.Children.Add(label); stackPanel.Children.Add(textBox); stackPanel.Children.Add(button); NewWindow.Content = stackPanel; NewWindow.Show(); } private void NewWindowButton_Click(object sender, RoutedEventArgs e) { TextVar = textBox.Text; NewWindow.Close(); } } 

Calling this window:

 public void GetFileName() { var InputBoxMsg = "Enter you file name"; InputBox inputBox = new InputBox(); inputBox.Show(InputBoxMsg); } 
  • one
  • 3
    To enter file names, it is better to use special regular Windows dialog boxes - Andrey NOP
  • 3
    Try using MvvM. For WPF, it's better suited - Tom Dugger

1 answer 1

Judging by the description of the question you need a dialog box, therefore you need to display it as a dialog

 NewWindow.Content = stackPanel; NewWindow.ShowDialog(); 

And then you save the information in a private TextVar field.

 private static string TextVar; 

then it only remains to change it to a public property, which will be responsible for returning the result or if everything is done in one method.

I changed your code a bit - removed redundant Show methods - if you have default parameters, you can specify only required parameters when calling, as well as all or some of those for which standard values ​​are defined. In addition, he removed the constructors, since in this case they are not needed.

 public class InputBox { public static string Show(string labelContent, string titleContent = "Input Box", string buttonContent = "Enter") { var NewWindow = new Window(); NewWindow.Name = "NewWindow"; NewWindow.Title = titleContent; NewWindow.Width = 300; NewWindow.Height = 200; var textVar = ""; var stackPanel = new StackPanel { Orientation = Orientation.Vertical }; var label = new Label { Content = labelContent, Name = "NewWindowLabel" }; var textBox = new TextBox { Text = "", Name = "NewWindowTextBox" }; var button = new Button { Content = buttonContent, Name = "NewWindowButton" }; button.Click += (s, e) => { textVar = textBox.Text; NewWindow.Close(); } stackPanel.Children.Add(label); stackPanel.Children.Add(textBox); stackPanel.Children.Add(button); NewWindow.Content = stackPanel; NewWindow.ShowDialog(); return textVar; } } 

and then the call will be as follows

 public string GetFileName() { var InputBoxMsg = "Enter you file name"; return InputBox.Show(InputBoxMsg); } 

But this is still a WinForms approach; WPF is a way to define a special VM in which information about the specified file would be stored.

  • The version you described was already tried by me. The value of the variable is returned before the event is triggered. In this case, an empty field is returned. As for the "redundant" Show methods, I used them as an overload method, and also brought the logic and calculations to a hidden method. - Kemal Abdullaev
  • one
    @KemalAbdullaev are you sure you changed the challenge? - user227049
  • @KemalAbdullaev suggest go to chat to quickly understand the reasons. Please ping me when you have time. To make a ping, put the dog @ and then my nickname (only the first part is possible ) - user227049
  • Yes you are right. I did not change the call. If you call via ShowDialog () then everything works. Thank. - Kemal Abdullaev