希望你们到目前为止都有一个出色的一周。
我一直在CSharp / MySql上开发POS应用程序,现在我对如何管理按钮事件感到非常困惑(即我到目前为止所认为的是那个如果服务员在点击某个表号后单击某个产品,那么这将生成一个插入数据库供以后查询,并带有一个标志,表明该检查已打开)...
到目前为止的原始编码(我仍然需要调整按钮名称和功能,但第一个按钮点击案例描述了我试图实现的想法):
String product = string.Empty;
String price = string.Empty;
Double total = 0.0;
string operand1 = string.Empty;
string operand2 = string.Empty;
string result;
char operation;
public LaCarta()
{
InitializeComponent();
displayTime();
}
private void LaCarta_Load(object sender, EventArgs e)
{
CoronaClara.Click += new EventHandler(btn_Click);
CoronaLight.Click += new EventHandler(btn_Click);
CoronaObscura.Click += new EventHandler(btn_Click);
Refresco.Click += new EventHandler(btn_Click);
NaranjadaGde.Click += new EventHandler(btn_Click);
NaranjadaCh.Click += new EventHandler(btn_Click);
LimonadaGde.Click += new EventHandler(btn_Click);
LimonadaCh.Click += new EventHandler(btn_Click);
HorchataGde.Click += new EventHandler(btn_Click);
HorchataCh.Click += new EventHandler(btn_Click);
AguaDePepinoGde.Click += new EventHandler(btn_Click);
NaipeCorazon.Click += new EventHandler(btn_Click);
NaipeDiamante.Click += new EventHandler(btn_Click);
NaipeEspada.Click += new EventHandler(btn_Click);
NaipeTrebol.Click += new EventHandler(btn_Click);
AguaDePepinoCh.Click += new EventHandler(btn_Click);
CervezaPacifico.Click += new EventHandler(btn_Click);
CervezaLeon.Click += new EventHandler(btn_Click);
CervezaVictoria.Click += new EventHandler(btn_Click);
NegraModelo.Click += new EventHandler(btn_Click);
CubetaDe20Pz.Click += new EventHandler(btn_Click);
CubetaDe10Pz.Click += new EventHandler(btn_Click);
CaguamaCorona.Click += new EventHandler(btn_Click);
JarraDeCheve.Click += new EventHandler(btn_Click);
LitroDeCheve.Click += new EventHandler(btn_Click);
MiniTarro.Click += new EventHandler(btn_Click);
MicheladaGde.Click += new EventHandler(btn_Click);
}
private void displayTime()
{
label21.Text = DateTime.Now.ToLongTimeString();
label22.Text = DateTime.Now.ToLongDateString();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
char open = 'Y';
int table = comboBox1.SelectedIndex;
MySqlConnection mcon = new MySqlConnection("server=localhost;database=database;username=root;password=***");
//Int32.TryParse(comboBox1.SelectedValue.ToString(), out val);
try
{
string query = "SELECT * FROM check where table_id = '" + table + "' and open = '" + open + "'";
// dataGridView1.DataSource = getData(query);
}
catch (Exception)
{
MessageBox.Show("Error loading table");
}
finally {
mcon.Close();
}
}
void btn_Click(object sender, EventArgs e)
{
try
{
Button btn = sender as Button;
MySqlConnection mcon = new MySqlConnection("server=localhost;database=database;username=root;password=***");
mcon.Open();
switch (btn.Name)
{
case "CoronaClara":
product = "CoronaClara";
LaCuenta.Items.Add(product);
string select = "SELECT price FROM products where prodnom = '" + product + "';";
MySqlCommand cmd = new MySqlCommand(select, mcon);
object result = cmd.ExecuteScalar();
precio = Convert.ToString(result);
//precio = Convert.ToDouble(result);
LaCuenta.Items.Add(product + " " + "$" + price);
txtInput.Text += price;
break;
case "btnTwo":
//txtInput.Text += "2";
break;
case "btnThree":
//txtInput.Text += "3";
break;
case "btnFour":
//txtInput.Text += "4";
break;
case "btnFive":
//txtInput.Text += "5";
break;
case "btnSix":
//txtInput.Text += "6";
break;
case "btnSeven":
//txtInput.Text += "7";
break;
case "btnEight":
//txtInput.Text += "8";
break;
case "btnNine":
//txtInput.Text += "9";
break;
case "btnZero":
//txtInput.Text += "0";
break;
case "btnDot":
//if (!txtInput.Text.Contains("."))
// txtInput.Text += ".";
break;
}
}
catch (Exception ex)
{
MessageBox.Show("Sorry for the inconvenience, Unexpected error occured. Details: " +
ex.Message);
}
}
private void bttn1_Click(object sender, EventArgs e)
{
txtInput.Text = operand1;
operation = '*';
txtInput.Text = string.Empty;
}
private void bttn2_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '*';
txtInput.Text = string.Empty;
}
private void bttn3_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '*';
txtInput.Text = string.Empty;
}
private void bttn4_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '*';
txtInput.Text = string.Empty;
}
private void btnEqual_Click(object sender, EventArgs e)
{
operand2 = txtInput.Text;
double opr1, opr2;
double.TryParse(operand1, out opr1);
double.TryParse(operand2, out opr2);
result = (opr1 * opr2).ToString();
txtInput.Text = result.ToString();
}
}
这基本上就是我想要互相交流的元素:图像按钮,列表框,乘数,从中可以选择多个表或客户名称的网格视图,以及总金额。
所以我的问题是:
你认为我走的是正确的方式吗?
如何让这些按钮点击事件相互影响?
非常感谢你的时间。
答案 0 :(得分:0)
我在你的代码中看到了许多缺陷!首先尝试在单个实用程序函数中重新组合数据库连接查询。这将减少代码冗余,并相信我将大大简化您的生活。其次,我确信产品不仅仅是一个字符串。它包含名称,价格等...创建类产品并添加所有详细信息,这将允许您获取信息。更容易。最后,在属性面板的设计模式中,有事件部分,单击它,对于每个按钮,将Click事件设置为btn_Click。
如果我理解得很清楚,对于你的第二个问题,创建一个产品类会对你有帮助。