2010-09-07 11 views
0

J'ai besoin d'ajouter un formulaire à mon application existante. J'ai tout mis en page, mais comment puis-je l'utiliser pour utiliser le code du formulaire pour le rendre transparent. Des pensées? et désolé pour le mur de code juste pensé que cela pourrait aider.Ajout d'un formulaire à une application de console existante?

Le bouton Valider devrait obtenir ses informations de la console comme ci-dessous

alt text

L'action Generate devrait ajouter le chiffre de contrôle comme dans cet exemple 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"; 
    } 
} 

}

Répondre

2

Je ne suis pas sûr de ce que vous demandez ici. Si vous voulez que l'utilisateur d'entrer le numéro ISBN sous la forme, alors vous feriez ceci:

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. 
    } 
} 

Si vous voulez afficher le formulaire et le champ d'entrée affichant la ISBN que l'utilisateur est entré sur la ligne de commande Ensuite, vous devrez ajouter une propriété ou une méthode à la classe IsbnForm afin de pouvoir définir la valeur avant d'afficher le formulaire. C'est, à l'intérieur IsbnForm, ajoutez cette propriété:

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

Et puis, pour le remplir:

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

Voici comment vous auriez à le faire. Vous devrez toutefois ajouter les espaces de noms qui contiennent les contrôles de formulaires Windows car les projets de console ne les ont pas par défaut. – Justin

+0

@Justin: Ou laissez l'IDE remplir les noms complets au fur et à mesure que vous tapez. :) –

Questions connexes