C
C#2y ago
Gipper

❔ Better way to open a "window" to take in text input?

As in, when you click a button, a small window appears which exists only to take in the for example a string name with a box for the user to write the name into and maybe a text block saying something like "Enter name: ". The way I normally do this is add a whole new Window to the solution and make it do that. But I would like it possible to use something like a MessageBox to accomplish the same thing...Any luck?
10 Replies
Pobiega
Pobiega2y ago
iirc there is no messagebox that has a text input field. You could very easily design your own thou, and make some convenience methods on it to more or less get that behaviour MyInputBox.ShowDialog(out var name); for example.
Gipper
GipperOP2y ago
Mmm, ok I'll do it that way... Btw do you know any way in visual studio to create a "subsidiary" window? As in to add this input window underneath the window that calls it and gets the data? Just so it's nicely organized
Pobiega
Pobiega2y ago
Im not sure what you mean by that.
if (InputDialog.ShowDialog("Username:", out var username) == DialogResult.OK)
{
label3.Text = username;
}
if (InputDialog.ShowDialog("Username:", out var username) == DialogResult.OK)
{
label3.Text = username;
}
public partial class InputDialog : Form
{
public InputDialog()
{
InitializeComponent();
}

public static DialogResult ShowDialog(string prompt, out string value)
{
var x = new InputDialog();
x.label2.Text = prompt;

var result = x.ShowDialog();

value = x.textBox1.Text;

return result;
}
}
public partial class InputDialog : Form
{
public InputDialog()
{
InitializeComponent();
}

public static DialogResult ShowDialog(string prompt, out string value)
{
var x = new InputDialog();
x.label2.Text = prompt;

var result = x.ShowDialog();

value = x.textBox1.Text;

return result;
}
}
extremely simple, but works
Omnissiah
Omnissiah2y ago
there's still the old InputBox https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.interaction.inputbox?view=net-7.0 BUT you could eventually just create a little form in the code without having it in the project it would be probably just a half page method, at most a page
Interaction.InputBox(String, String, String, Int32, Int32) Method (...
Displays a prompt in a dialog box, waits for the user to input text or click a button, and then returns a string containing the contents of the text box.
Gipper
GipperOP2y ago
Sorry for the delayed response, can you explain what is happening here? I haven't worked with windows forms yet Visual studio doesnt know about anything named Forms which you are inheriting there from... and yes, I have written using System.Windows.Forms Hey guys, I've been googling around and it seems like this "page" feature that WPF has may work well for these purposes...Thoughts?
Pobiega
Pobiega2y ago
Ah I assumed winforms since you didn't specify
Gipper
GipperOP2y ago
I mean, it has the WPF tag at the top...
Pobiega
Pobiega2y ago
Fair. It's a shame they are so easy to miss but to stay on topic, while you probably could use a Page it may not be what you want. If you think of a webpage, its more like going to "About us" more than its opening a modal with a textbox.
Pobiega
Pobiega2y ago
Dialog Boxes Overview - WPF .NET
Learn about the varieties of dialog boxes in Windows Foundation Presentation (WPF). With a dialog box, you gather and display information to a user.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server