多语言展示
当前在线:674今日阅读:113今日分享:31

C# Winform switch使用方法

在winform开发中,常会遇见一个对象可能存在多个属性的问题,简而言之,比如一个产品,结算方式有零售现金、零售月结、批发现金、批发月结。那么,我们在设定好价格表后,获取单价的时候就需要使用switch去判断当前是哪种交易型态,选择对应的价格。
工具/原料

电脑 VS软件

方法/步骤
1

在winform中把相关的控件设定好,如图所示,我们在选择不同的交易型态时,单价根据之前定义好的,显示出不一样的单价。

2

编写单价类型combobox框的文本改变事件,也就是说,我们选择不同的交易类型,去触发不同的单价显示。

3

连接数据库,查询当前选择的交易类型在数据库中对应的字段,并把查询出来 的字段赋值给 string a。这里为后面的switch做准备。                if (login.share_cls.conn.State == ConnectionState.Closed)                {                    login.share_cls.conn.Open();                }                SqlDataAdapter da = new SqlDataAdapter();                SqlCommand sql = new SqlCommand("select j_type from tjstype where j_name='" + comboBox2.Text + "' ", login.share_cls.conn);                da.SelectCommand = sql;                DataSet dt = new DataSet();                object obj1 = sql.ExecuteScalar();                da.Fill(dt);                string a = obj1.ToString();

4

编写switch case语句,这里是把四种交易模式查询出来的值赋给单价显示框   textbox7. switch (a)                {                    case "z_cash":                        SqlDataAdapter da1 = new SqlDataAdapter();                        SqlCommand sql1 = new SqlCommand("select z_cash from tprice where p_no='" + textBox2.Text + "'", login.share_cls.conn);                        da1.SelectCommand = sql1;                        DataSet dt1 = new DataSet();                        object obj2 = sql1.ExecuteScalar();                        da1.Fill(dt1);                        textBox7.Text = obj2.ToString();                        break;                    case "z_monc":                        SqlDataAdapter da2 = new SqlDataAdapter();                        SqlCommand sql2 = new SqlCommand("select z_monc from tprice where p_no='" + textBox2.Text + "'", login.share_cls.conn);                        da2.SelectCommand = sql2;                        DataSet dt2 = new DataSet();                        object obj3 = sql2.ExecuteScalar();                        da2.Fill(dt2);                        textBox7.Text = obj3.ToString();                        break;                    case "i_cash":                        SqlDataAdapter da3 = new SqlDataAdapter();                        SqlCommand sql3 = new SqlCommand("select i_cash from tprice where p_no='" + textBox2.Text + "'", login.share_cls.conn);                        da3.SelectCommand = sql3;                        DataSet dt3 = new DataSet();                        object obj4 = sql3.ExecuteScalar();                        da3.Fill(dt3);                        textBox7.Text = obj4.ToString();                        break;                    case "i_monc":                        SqlDataAdapter da4 = new SqlDataAdapter();                        SqlCommand sql4 = new SqlCommand("select i_monc from tprice where p_no='" + textBox2.Text + "'", login.share_cls.conn);                        da4.SelectCommand = sql4;                        DataSet dt4 = new DataSet();                        object obj5 = sql4.ExecuteScalar();                        da4.Fill(dt4);                        textBox7.Text = obj5.ToString();                        break

5

注意每个case下面,都要使用break结束。并且要符合switch case语法规则。

6

测试验证,选择不同的交易类型,显示不同的单价。我们的最终目标实现。

注意事项

要习惯写代码注释哦,我这里只是演示,所以没有写注释。

推荐信息