从控制台获取表单输入

时间:2010-09-09 13:18:26

标签: c#

我想知道如何在此控制台应用程序中输入的值在表单文本框中工作如何才能执行此操作?

在控制台中输入00000000的示例就像在表单中输入一样,我想我希望它们是分开的但是可以无缝地完成任何想法。我无法弄清楚如何让文本框从控制台应用程序的Console.Readline()获取内容。

这是我试图让它发挥作用的一个例子

alt text

alt text

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 "false"...
                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";
}

}

1 个答案:

答案 0 :(得分:1)

创建表单实例,并在您在文本框中执行readline时传入存储的变量。

IsbnForm form = new IsbnForm();

您可以通过以下几种方式传递文本:

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

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

变量如:

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

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

或者只是将文本框控件公开,然后通过

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

当您显示表单时,您需要保持控制台程序运行,方法是使用readline,在窗体关闭时断开循环,或者可能使用ShowDialog而不是Show。

查看哪一个正常工作以及您希望它如何工作。

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

while(FormRunning == false) { }

form.ShowDialog();