2010-09-07 122 views
0

我需要添加一個窗體到我現有的應用程序中,我已經完成了所有的佈局,但是如何獲取它以使用窗體中的代碼使其無縫。有什麼想法嗎?並認爲它可能有所幫助,對代碼牆感到抱歉。將窗體添加到現有控制檯應用程序?

驗證按鈕應該從控制檯的信息像下面

alt text

的產生動作應該在這個例子中 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(" ", ""); 
     } 
    } 

    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"; 
    } 
} 
追加校驗位像

}

回答

2

我不確定你在問什麼。如果你想顯示的形式,並且具有輸入字段顯示ISBN用戶在命令行中輸入

using (var frm = new IsbnForm()) 
{ 
    var rslt = frm.ShowDialog(); 
    if (rslt == DialogResult.OK) 
    { 
     // Access property that gets the value the user entered. 
    } 
    else 
    { 
     // User canceled the form somehow, so show an error. 
    } 
} 

:如果您希望用戶在表單中輸入ISBN,那麼你應該這樣做,那麼您需要將一個屬性或方法添加到IsbnForm類中,以便您可以在顯示錶單之前設置該值。也就是說,裏面IsbnForm,添加該屬性:

public string Isbn 
{ 
    get { return xInputTextBox.Text; } 
    set { xInputTextBox.Text = value; } 
} 

,然後填充它:

Console.Write("Enter an ISBN: "); 
var isbn = Console.ReadLine(); 
using (var frm = new IsbnForm()) 
{ 
    frm.Isbn = isbn; // populates the field in the form. 
    var rslt = frm.ShowDialog(); 
    // etc, etc. 
} 
+0

這是你如何將不得不這樣做。但是,您將需要添加包含Windows窗體控件的命名空間,因爲控制檯項目在默認情況下沒有它們。 – Justin 2010-09-07 21:19:55

+0

@Justin:或者讓IDE在輸入時填寫完全限定的名稱。 :) – 2010-09-07 21:25:20

相關問題