在禁用按钮之前,最多使用该按钮3次

本文关键字:按钮 3次 | 更新日期: 2023-09-27 17:50:56

我希望能够使用此按钮最多搜索oracle三次,并且在三次尝试禁用按钮并使用不同的搜索之后。下面是我的代码,当按钮被点击搜索第一。如果catch被使用了三次,我希望能够禁用该按钮。

private void btnCancelSearch_Click(object sender, EventArgs e)
{
    try
    {
        //Connect to Database
        OracleConnection conn = new OracleConnection(oradb);
        conn.Open();
        OracleCommand cmd = conn.CreateCommand();
        //Define SQL Query (Select)
        strSQL = "SELECT * FROM Bookings WHERE BookingNo = '" + txtCnlBookingNo.Text + "'";
        cmd.CommandText = strSQL;
        OracleDataReader dr = cmd.ExecuteReader();
        dr.Read();
        txtBookingNo.Text = dr.GetValue(0).ToString();
        txtBkgSurname.Text = dr.GetValue(1).ToString();
        txtBkgForename.Text = dr.GetValue(2).ToString();
        txtBkgContactNo.Text = dr.GetValue(3).ToString();
        txtBkgStreet.Text = dr.GetValue(4).ToString();
        txtBkgTown.Text = dr.GetValue(5).ToString();
        txtBkgCounty.Text = dr.GetValue(6).ToString();
        txtBkgCountry.Text = dr.GetValue(7).ToString();
        txtBkgEmail.Text = dr.GetValue(8).ToString();
        cboBkgNoGuests.Text = dr.GetValue(9).ToString();
        cboBkgPayment.Text = dr.GetValue(10).ToString();
        dtpBkgCheckIn.Text = dr.GetValue(11).ToString();
        dtpBkgCheckOut.Text = dr.GetValue(12).ToString();
    }
    catch
    {
        //Display confirmation message
        MessageBox.Show("Not a valid Booking No");
    }

在禁用按钮之前,最多使用该按钮3次

取决于您的应用程序。您可以跟踪一个名为:

的变量
int ButtonClickedCount = 0;

每次单击按钮时递增此变量。

如果点击次数超过,通知用户或禁用按钮。

不确定这是否是你想要达到的目标:

声明一个变量来跟踪用户点击的次数,然后应用你的逻辑?

int count = 0;

private void btnCancelSearch_Click(object sender, EventArgs e)
{
if(count <3){
    count++;
    try
    {
        //Connect to Database
        OracleConnection conn = new OracleConnection(oradb);
        conn.Open();
        OracleCommand cmd = conn.CreateCommand();
        //Define SQL Query (Select)
        strSQL = "SELECT * FROM Bookings WHERE BookingNo = '" + txtCnlBookingNo.Text + "'";
        cmd.CommandText = strSQL;
        OracleDataReader dr = cmd.ExecuteReader();
        dr.Read();
        txtBookingNo.Text = dr.GetValue(0).ToString();
        txtBkgSurname.Text = dr.GetValue(1).ToString();
        txtBkgForename.Text = dr.GetValue(2).ToString();
        txtBkgContactNo.Text = dr.GetValue(3).ToString();
        txtBkgStreet.Text = dr.GetValue(4).ToString();
        txtBkgTown.Text = dr.GetValue(5).ToString();
        txtBkgCounty.Text = dr.GetValue(6).ToString();
        txtBkgCountry.Text = dr.GetValue(7).ToString();
        txtBkgEmail.Text = dr.GetValue(8).ToString();
        cboBkgNoGuests.Text = dr.GetValue(9).ToString();
        cboBkgPayment.Text = dr.GetValue(10).ToString();
        dtpBkgCheckIn.Text = dr.GetValue(11).ToString();
        dtpBkgCheckOut.Text = dr.GetValue(12).ToString();
    }
    catch
    {
        //Display confirmation message
        MessageBox.Show("Not a valid Booking No");
    }
}else
{
    //Implement whatever search you want here
    //And disable your button here
}
}