2013-12-17 64 views
-1

我有兩種形式在我的項目工作(Form1的&窗體2)充分利用數據庫中的列值,並用文本框

在Form1上用戶被要求輸入用戶名(textboxUsername)&密碼操縱它(textboxPassword)

當用戶登錄時,Form2彈出作爲他的帳戶,他可以看到他的ID,用戶名,姓名,姓氏,生日和金錢列。

登錄部分:

MessageBox.Show("Data verified!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       this.Hide(); 
       Form2 frm2 = new Form2("Welcome: " + textBoxUsername.Text, textBoxUsername.Text); 
       frm2.ShowDialog(); 

現在在Form2按鈕1,用戶可以看到他的專欄文章:

public class Form2: Form 
{ 
    private string currentUserName = string.Empty; 
    public Form2(string welcome, string UserName) 
    { 
     label2.Text = welcome; 
     currentUserName = UserName; 
    } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    string constring = @"Data Source=V-K\;Initial Catalog=ATMKlientet;Integrated Security=True"; 
    SqlCommand cmdDataBase = new SqlCommand(" select * from Clients where [email protected]", conDataBase); 
      cmdDataBase.Parameters.AddWithValue("@uname", currentUserName); 

    try 
    { 
     SqlDataAdapter sda = new SqlDataAdapter(); 
     sda.SelectCommand = cmdDataBase; 
     dbdataset = new DataTable(); 
     sda.Fill(dbdataset); 
     BindingSource bSource = new BindingSource(); 

     bSource.DataSource = dbdataset; 
     dataGridView1.DataSource = bSource; 
     sda.Update(dbdataset); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

現在,在窗體2我想補充凡在用戶登錄文本框可以鍵入數字值。如果他的Money列是100,那麼當他在textBox中鍵入50時,Money列在數據庫中應保持爲50。否則,如果列值是ex.100並且用戶撤回200,則應該生成錯誤。這就像減法或每次用戶在文本框中輸入一個值時退出。

回答

0

使用textbox_keydownEvent

textBox1.KeyDown += textBox1_KeyDown; 

內碼法:

private void textBox1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) //it will only allow numeric keys 
     { 
      e.Handled = true; //it will handle every non-numeric key, and will only allow integers and control keys 
     } 
     else 
       //write your code to what ever you want to do with database or gridview columns 
    }