InputBox function

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I need WinForms function which displays promp and asks single field from
user, like

string res = Util.InputBox( "Enter value" );

How to implement this ?


Andrus.
 
Andrus said:
I need WinForms function which displays promp and asks single field from
user, like

string res = Util.InputBox( "Enter value" );

How to implement this ?

Make a form with a Label instance for the prompt ("Enter value") and a
TextBox instance for the user input, a button for "OK" and optionally a
"Cancel" button. Make sure the buttons are configured for the correct
DialogResult (set their DialogResult property as desired).

Instantiate the form, call ShowDialog() on it, if the return value is
DialogResult.OK, process the Text property of the TextBox instance as
your entered value.

Wrap all of the above in a method that does all of the work and simply
returns the string value obtained from the Text property of the TextBox.
Don't forget to dispose your form instance before returning.

Pete
 
I need WinForms function which displays promp and asks single field from
Make a form with a Label instance for the prompt ("Enter value") and a
TextBox instance for the user input, a button for "OK" and optionally a
"Cancel" button. Make sure the buttons are configured for the correct
DialogResult (set their DialogResult property as desired).

Instantiate the form, call ShowDialog() on it, if the return value is
DialogResult.OK, process the Text property of the TextBox instance as your
entered value.

Wrap all of the above in a method that does all of the work and simply
returns the string value obtained from the Text property of the TextBox.
Don't forget to dispose your form instance before returning.

Pete,

Can you provide example code please ?
Or maybe some VB assembly contains such function ?

Andrus.
 
Or, one could set a reference to Microsoft.VisualBasic.dll assembly and
call the InputBox method on the Interaction class in the
Microsoft.VisualBasic namespace.

Some don't like the idea of loading Microsoft.VisualBasic.dll, but it's
there to use if one wants (and I assume the OP wanted this, as he used the
name of the method in VB which does the same thing).
 
Why you re-invented wheel !?

Isn't it simpler to use

string res = Interaction.InputBox("prompt", "caption", "defaultresponse",

0, 0);



from VisualBasic.dll as Nicolas recommends?

Andrus.
 
Thank you.
I plan to use MS ReportViewer which loads VisualBasics.dll anyway, so no
difference.

I tried

Interaction.InputBox("prompt", "caption", "defaultresponse", 0, 0);

but window appears in the left upper corner.

How to force window to be centered in screen ?

Andrus.

Nicholas Paldino said:
Or, one could set a reference to Microsoft.VisualBasic.dll assembly and
call the InputBox method on the Interaction class in the
Microsoft.VisualBasic namespace.

Some don't like the idea of loading Microsoft.VisualBasic.dll, but it's
there to use if one wants (and I assume the OP wanted this, as he used the
name of the method in VB which does the same thing).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Duniho said:
Make a form with a Label instance for the prompt ("Enter value") and a
TextBox instance for the user input, a button for "OK" and optionally a
"Cancel" button. Make sure the buttons are configured for the correct
DialogResult (set their DialogResult property as desired).

Instantiate the form, call ShowDialog() on it, if the return value is
DialogResult.OK, process the Text property of the TextBox instance as
your entered value.

Wrap all of the above in a method that does all of the work and simply
returns the string value obtained from the Text property of the TextBox.
Don't forget to dispose your form instance before returning.

Pete
 
Andrus,

Well, you are specifying the top left of the screen using 0 and 0 for
the x and y parameters. Use -1, and it should center the input box.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrus said:
Thank you.
I plan to use MS ReportViewer which loads VisualBasics.dll anyway, so no
difference.

I tried

Interaction.InputBox("prompt", "caption", "defaultresponse", 0, 0);

but window appears in the left upper corner.

How to force window to be centered in screen ?

Andrus.

Nicholas Paldino said:
Or, one could set a reference to Microsoft.VisualBasic.dll assembly
and call the InputBox method on the Interaction class in the
Microsoft.VisualBasic namespace.

Some don't like the idea of loading Microsoft.VisualBasic.dll, but
it's there to use if one wants (and I assume the OP wanted this, as he
used the name of the method in VB which does the same thing).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Duniho said:
Andrus wrote:
I need WinForms function which displays promp and asks single field
from user, like

string res = Util.InputBox( "Enter value" );

How to implement this ?

Make a form with a Label instance for the prompt ("Enter value") and a
TextBox instance for the user input, a button for "OK" and optionally a
"Cancel" button. Make sure the buttons are configured for the correct
DialogResult (set their DialogResult property as desired).

Instantiate the form, call ShowDialog() on it, if the return value is
DialogResult.OK, process the Text property of the TextBox instance as
your entered value.

Wrap all of the above in a method that does all of the work and simply
returns the string value obtained from the Text property of the TextBox.
Don't forget to dispose your form instance before returning.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top