Implementation method of calling Inputbox class in VB in C

  • 2021-09-20 21:21:38
  • OfStack

C # doesn't have the Inputbox class of its own, but Inputbox is also quite easy to use, so there are two ways to use it

1: Indirect call to Inputbox functionality in vb

1. Add a reference to Microsoft. VisualBasic in your project
2. Add the namespace Using Microsoft. VisualBasic to the project;
3. In the future, you can directly use many class libraries in VB (cool …)

For example: textBox1.Text=Microsoft. VisualBasic. Interaction. InputBox ("prompt text", "dialog box title", "default value", X coordinates, Y coordinates);

The above X coordinates and Y coordinates can be 1 and-1, indicating that the middle position of the screen is displayed.

2: You can also write an InputBox () function yourself. Dynamically generate an FORM, TEXTBOX and BUTTON, etc., determine the position, and return the string input by the user.


public partial class InputBox : Form
{    
  private InputBox()
  {
    InitializeComponent();
  }

  public String getValue()
  {
    return textBox1.Text;
  }

  public static bool Show(String title,String inputTips,bool isPassword,ref String value)
  {
    InputBox ib = new InputBox();
    if (title != null)
    {
      ib.Text = title;
    }
    if (inputTips != null)
    {
      ib.label1.Text = inputTips;
    }

    if (isPassword)
    {
      ib.textBox1.PasswordChar = '*';
    }

    if (ib.ShowDialog()==DialogResult.OK)
    {
      value = ib.getValue();
      ib.Dispose();
      return true;
    }
    else
    {
      ib.Dispose();
      return false;
    }
  }
}

Usage


String value;

if (InputBox.Show(" User input ", " Password: ", true, ref value))
{
  // Actions after successful input 
}

Related articles: