开发者

Get input from console into a form

开发者 https://www.devze.com 2023-01-15 17:19 出处:网络
I want to see how I can get the value entered in this console application to work inside a form textbox how can I do this?

I want to see how I can get the value entered in this console application to work inside a form textbox how can I do this?

Example entering 00000000 in console to be as if it were entered in the form so I guess I want them to be separate but work seamless any ideas. I cant figure out how to get the textbox to get the contents from the Console.Readline() of the console application.

This is and example of how I am trying to get it to work

Get input from console into a form

Get input from console into a form

public static void Main(string[] args)
    {
        Console.Write("Enter a valid 10 digit ISBN Number ");
        string isbn = isbnChecker.DestabilizeIsbn(Console.ReadLine()); // Normalizes the input and puts it on string "str"
        if (isbn.Length > 10 || isbn.Length < 9) // If the string length is greather than 10, or smaller than 9
        {
            Console.WriteLine("The number you have entered is not a valid ISBN try again."); // Print invalid number
            Console.ReadLine();
        }
        else if (isbn.Length == 10) // If the length is 10
        {
            if (isbnChecker.CheckNumber(isbn)) // If function CheckNum return "true"...
                Console.WriteLine("The number you have entered is a valid ISBN");

            else // If it returns "f开发者_如何转开发alse"...
                Console.WriteLine("The number you have entered is not a valid ISBN try again.");
                Console.ReadLine();
        }
        else // Else (If the number is NOT greater than 10 or smaller than 9, NOR is it 10 -> If the number is 9)
        {
            Console.WriteLine("The Check digit that corresponds to this ISBN number is " + checkIsbnClass.CheckIsbn(isbn) + "."); // Print the checksum digit
            Console.ReadLine();
        }



    }





public static class isbnChecker
    {
        public static bool CheckNumber(string isbn) // Checks if the checksum digit is correct
        {
            if (isbn[9].ToString() == checkIsbnClass.CheckIsbn(isbn)) // If the 10th digit of the number is the same as the calculated digit...
                return true;
            else // If they're not the same...
                return false;
        }
        public static string DestabilizeIsbn(string isbn) // replace the string
        {
            return isbn.Replace("-", "").Replace(" ", "");
        }
    }
  [1]: http://i.stack.imgur.com/bAcDJ.jpg
  public static string CheckIsbn(string isbn) // Calculates the 10th digit of a 9-digits partial ISBN number
        {
            int sum = 0;
            for (int i = 0; i < 9; i++) // For each number...
            {
                sum += int.Parse(isbn[i].ToString()) * (i + 1); // ...Multiply the number by it's location in the string
            }
            if ((sum % 11) == 10) // If the remainder equals to 10...
            {
                return "x"; // Output X
            }
            else // If it does not equal to 10...
            {
                return (sum % 11).ToString(); // Output the number
            }
        }
      public static bool CheckNumber(string isbn) // Checks if the checksum digit is correct
        {
            if (isbn[9].ToString() == CheckIsbn(isbn)) // If the 10th digit of the number is the same as the calculated digit...
                return true;
            else // If they're not the same...
                return false;
        }
      public static string DestabilizeIsbn(string isbn) // replace the string
      {
          return isbn.Replace("-", "").Replace(" ", "");
      }

    }


public partial class IsbnForm : Form
{
public IsbnForm()
{
    InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    this.xInputTextBox.Text = "Enter a Valid ISBN";
}

}


Create an instance of the form and pass in the variable you store when you do a readline into the textbox.

IsbnForm form = new IsbnForm();

You can pass the text in a few ways, via method:

//In the IsbnForm class
public SetTextboxText(String Text) { textbox.text = Text; }

//In the console application
form.SetTextboxText(isbn);

a variable such as:

//In the IsbnForm class
public String TextboxText { set { textbox.text = value; } }

//In the console application
form.TextboxText = isbn;

or just making the textbox control public, and then changing the value via

//In the console application.
form.textbox.text = isbn;

You'll need to keep the console program running when you show your form, either by having a readline, a loop that breaks when the form is closed or possibly using ShowDialog instead of Show.

See which one works properly and how you'd expect it to work.

form.Show();
Console.Readline();

or

while(FormRunning == false) { }

or

form.ShowDialog();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号