如何从同一个textBox C#获取输出和输入

时间:2014-06-12 11:43:05

标签: winforms c#-4.0

我有一个函数AuthenticateUser();在Form1_Load中

     private void Form1_Load(object sender, EventArgs e)
    {
        AuthenticateUser();

    }

我的函数AuthenticateUser()只打印winform文本框中的第一行  “请输入您的帐号:”然后它永远不会得到输入或显示其余的问题。

这里是AuthenticateUser()函数:

    private void AuthenticateUser() // attempt to authenticate user against database
    {

        pantalla.Text = screen.DisplayMessage("\nPlease enter your account number:\n"); 

       int accountNumber = int.Parse(pantalla.Text);//enter the account number



        //pantalla.Text = accountNumber.ToString();

        pantalla.Text = screen.DisplayMessage("\nEnter your PIN: ");
        int pin = Convert.ToInt32(pantalla.Text);

        // set userAuthenticated to boolean value returned by database
        userAuthenticated = bankDatabase.AuthenticateUser(accountNumber, pin);

        // check whether authentication succeeded
        if (userAuthenticated)
        {
            currentAccountNumber = accountNumber; // save user's account #
            pantalla.Text = currentAccountNumber.ToString();
        }
        else
            pantalla.Text = screen.DisplayMessageLine("Invalid account number or PIN.Please try again.");                                           

    }

输出:

[1 ]http://i.stack.imgur.com/nSaWz.jpg

enter image description here

在这里我输入了帐号,它什么也没做!只是卡在那里,它甚至没有读取下一个输入。

3 个答案:

答案 0 :(得分:0)

使用UserControl会不会更好?

public Class ATMDialer : UserControl
{
  TextBox txt = new TextBox();
  Button _b1 = new Button();
  Button _b2 = new Button();
  .
  .
  .

  _b1.Clicked += new EventHandler(OnButtonClicked);
  _b2.Clicked += new EventHandler(OnButtonClicked);
  .
  .
  .
  private void OnButtonClicked(object sender, EventArgs e)
  {
    Button b = (Button)sender;
    txt.Text = txt.Text + b.Text;
  }
}

答案 1 :(得分:0)

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;

 namespace ATM2
 {
 public partial class Form1 : Form
 {
       private bool userAuthenticated; // true if user is authenticated
       private int currentAccountNumber; // user's account number

   private Screen screen;
   private Keypad keypad;
   private CashDispenser cashDispenser;
   private DepositSlot depositSlot;
   private BankDatabase bankDatabase;




  public Form1()
    {
        InitializeComponent();
        userAuthenticated = false; // user is not authenticated to start
        currentAccountNumber = 0; // no current account number to start
        screen = new Screen(); // create screen
        keypad = new Keypad(); // create keypad
        cashDispenser = new CashDispenser(); // create cash dispenser
        depositSlot = new DepositSlot(); // create deposit slot
        bankDatabase = new BankDatabase(); // create account info database


    }

    private void Form1_Load(object sender, EventArgs e)
    {
        AuthenticateUser();

    }

    private void pantalla_TextChanged(object sender, EventArgs e)
    {

    }




    // start ATM
    public void Run()
    {
        bool flag = true;
        // welcome and authenticate users; perform transactions
       #pragma warning disable
        while (flag) // infinite loop
        {

            // loop while user is not yet authenticated
            while (!userAuthenticated)
            {
               pantalla.Text= screen.DisplayMessageLine("\nWelcome!");
               break;
                AuthenticateUser(); // authenticate user

            } // end while


           // PerformTransactions(); // for authenticated user
            userAuthenticated = false; // reset before next ATM session
            currentAccountNumber = 0; // reset before next ATM session
           pantalla.Text= screen.DisplayMessageLine("\nThank you! Goodbye!");

           flag = false;
        } // end while
      #pragma warning restore

    } // end method Run



    private void AuthenticateUser() // attempt to authenticate user against database
    {

        pantalla.Text = screen.DisplayMessage("\nPlease enter your account number: \n");
       int accountNumber = int.Parse(pantalla.Text);



        //pantalla.Text = accountNumber.ToString();

        pantalla.Text = screen.DisplayMessage("\nEnter your PIN: ");
        int pin = Convert.ToInt32(pantalla.Text);

        // set userAuthenticated to boolean value returned by database
        userAuthenticated = bankDatabase.AuthenticateUser(accountNumber, pin);

        // check whether authentication succeeded
        if (userAuthenticated)
        {
            currentAccountNumber = accountNumber; // save user's account #
            pantalla.Text = currentAccountNumber.ToString();
        }
        else
            pantalla.Text = screen.DisplayMessageLine("Invalid account number or PIN. Please try again.");

    }




    private void button_Click(object sender, EventArgs e)
    {
        Button b = (Button) sender; //to cast an object to a different data type

        pantalla.Text = pantalla.Text + b.Text;

    }

    private void Enter_Click(object sender, EventArgs e)
    {



    }

    private void label1_Click(object sender, EventArgs e)
    {

    } // end method AuthenticateUser




}

}

答案 2 :(得分:0)

屏幕类:

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;

namespace ATM2
{

public class Screen
{
    public string DisplayMessage(string message)
    {
        string lineAbc;
        lineAbc = message;
        return lineAbc;
    }


    public string DisplayMessageLine(string message)
    {
        string lineAbc;
        lineAbc= message;
        return lineAbc;

    }


    public void DisplayDollarAmount(decimal amount)
    {
        Console.Write("{0:C}", amount);
    }

}


}