locked
adding a INPUTBOX in C# - NEWBIE

    Question

  • I need to implement an InputBox in C#, i know that cann't do this in C#. I thought of adding a textbox and a button, to a form application and implement it. But i am unable to get the same functionality as in the VB inputbox, where when the input box is popped up the program will wait until the user enters values to it, and continue there after. How do i code this in C# ?

    please note :  i don't Mind using the VB code, but i only need 1 button to appear on the form. the VB, style of the inputbox has a OK and a Cancel button, I only need the OK button. and also i need to do some validation on the content.

    This is the VB way of doing it in C# ;

    string num= Microsoft.VisualBasic.Interaction.InputBox("Enter number:", "your telephone number", "", 10, 18);


     

    Thursday, June 23, 2011 4:17 PM

Answers

  • Sure you can do this is C#:

    //on Form1:
    //open Form2:
    using(Form2 f2 = new Form2())
    {
       if(f2.ShowDialog() == DialogResult.OK)
       {
    
       }
    }
    
    //of form2:
    //in some event ot method, when all set, call:
    this.DialogResult = DialogResult.OK;
    
    ///this will leave the form2 and back to form1.
    



    Mitja
    • Marked as answer by LocalP Tuesday, June 28, 2011 6:36 AM
    Thursday, June 23, 2011 4:51 PM
  • Hi

    You may add a reference to the Microsoft.VisualBasic.dll. And then use the InputBox in c# code.

    private void button1_Click(object sender, System.EventArgs e)
    {
     // This test that the InputBox can handle more newline than one.
     InputBoxResult test = InputBox.Show("Prompt" + "\n" + "DDDD" + 
        "Prompt" + "\n" + "DDDD" +
         "Prompt" + "\n" + "DDDD" + "Prompt" + "\n" + "DDDD" +
         "Prompt" + "\n" + "DDDD" +
         "Prompt" + "\n" + "DDDD"
         ,"Title","Default",100,0); 
     if( test.ReturnCode == DialogResult.OK )
      MessageBox.Show(test.Text);
    } 
    
    

    http://www.codeproject.com/KB/edit/InputBox.aspx

    Hope this can help you.


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    • Marked as answer by Cookie Luo Tuesday, June 28, 2011 7:03 AM
    Tuesday, June 28, 2011 3:53 AM

All replies

  • Instead on instaniating your form with Show() instantiate it as a DialogBox.Show and the main form will wait until the input form is closed.
    Thursday, June 23, 2011 4:24 PM
  • Instead on instaniating your form with Show() instantiate it as a DialogBox.Show and the main form will wait until the input form is closed.
    There is something called 'DialogResult' but i am unable to find a 'DialogBox' . How should i proceed 
    Thursday, June 23, 2011 4:42 PM
  • Sure you can do this is C#:

    //on Form1:
    //open Form2:
    using(Form2 f2 = new Form2())
    {
       if(f2.ShowDialog() == DialogResult.OK)
       {
    
       }
    }
    
    //of form2:
    //in some event ot method, when all set, call:
    this.DialogResult = DialogResult.OK;
    
    ///this will leave the form2 and back to form1.
    



    Mitja
    • Marked as answer by LocalP Tuesday, June 28, 2011 6:36 AM
    Thursday, June 23, 2011 4:51 PM
  • Sorry, ShowDialog method

     

    http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx

     

    Thursday, June 23, 2011 4:51 PM
  • Sure you can do this is C#:

     

    //on Form1:
    //open Form2:
    using(Form2 f2 = new Form2())
    {
      if(f2.ShowDialog() == DialogResult.OK)
      {
    
      }
    }
    
    //of form2:
    //in some event ot method, when all set, call:
    this.DialogResult = DialogResult.OK;
    
    ///this will leave the form2 and back to form1.
    


     


    Mitja
    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\proj\proj\example.cs(77,22): error CS1061: 'proj.example' does not contain a definition for 'DialogResult' and no extension method 'DialogResult' accepting a first argument of type 'proj.example' could be found (are you missing a using directive or an assembly reference?)
    It points to the following line of your code ;
    <strong>this.DialogResult</strong> = DialogResult.OK;
    
    Thursday, June 23, 2011 5:01 PM
  • Hi

    You may add a reference to the Microsoft.VisualBasic.dll. And then use the InputBox in c# code.

    private void button1_Click(object sender, System.EventArgs e)
    {
     // This test that the InputBox can handle more newline than one.
     InputBoxResult test = InputBox.Show("Prompt" + "\n" + "DDDD" + 
        "Prompt" + "\n" + "DDDD" +
         "Prompt" + "\n" + "DDDD" + "Prompt" + "\n" + "DDDD" +
         "Prompt" + "\n" + "DDDD" +
         "Prompt" + "\n" + "DDDD"
         ,"Title","Default",100,0); 
     if( test.ReturnCode == DialogResult.OK )
      MessageBox.Show(test.Text);
    } 
    
    

    http://www.codeproject.com/KB/edit/InputBox.aspx

    Hope this can help you.


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    • Marked as answer by Cookie Luo Tuesday, June 28, 2011 7:03 AM
    Tuesday, June 28, 2011 3:53 AM