学习心得之输入输出控件

学习笔记之C#输入输出控件

思维导图:

1. Textbox  直接在框框中使用可用来输入输出

2. DataTimePicker

CustomFormat属性:可以用来控制控件值显示的日期格式。在设置这个属性之前,需要将Format属性设置为Custom,表示使用自定义的格式。

3. Radiobutton  

 Checked 属性设置为 true,并且调用 Click 事件处理程序。当 Checked 属性的值更改时,将引发 CheckedChanged 事件。如果 AutoCheck 属性设置为 true(默认值),则当选择单选按钮时,将自动清除该组中的所有其他单选按钮。

4. combobox

ComboBox的常用下拉格式DropDownStyle 有三种,分别是:

Simple是文本可编辑,下拉列表总可见;

DropDown是默认样式,文本可编辑,下拉需用户点击箭头;

DropDownList是文本不可编辑,下拉需用户点击箭头

获取控件选中项的内容,代码如下所示:

  1. MessageBox.Show(comboBox1.SelectedItem.ToString()); 

    获取控件选中项的索引,代码如下所示:

  2. MessageBox.Show(comboBox1.SelectedIndex.ToString());

5. PictureBox

AutoSize 属性:自动调整大小以显示整幅图形

loadpicture:picture加载图片的方法。

picture.LoadPicture([FileName], [Size], [ColorDepth], [X], [Y])

例子代码如下:

private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection sqlConnection = new SqlConnection();                                                          

         sqlConnection.ConnectionString ="Server=(local);Database=药品工作站管理系统;Integrated Security=sspi";                                   

SqlCommand sqlCommand = new SqlCommand();                                                                   

sqlCommand.Connection = sqlConnection;                                                                     

 sqlCommand.CommandText = "SELECT * FROM 操作员信息 WHERE 操作员编号=@操作员编号;";                                       

sqlCommand.Parameters.AddWithValue("@操作员编号", this.no.Text.Trim());

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                                       

sqlDataAdapter.SelectCommand = sqlCommand;                                                                 

 DataTable Table = new DataTable();                                                                    

 sqlConnection.Open();                                                                                      

 sqlDataAdapter.Fill(Table);                                                       

SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();                                                

  if (sqlDataReader.Read())                                                                                  

 {

                this.no.Text = sqlDataReader["操作员编号"].ToString();                                         

                this.name.Text = sqlDataReader["姓名"].ToString();

                this.Male.Checked = (bool)sqlDataReader["性别"];

                this.Female.Checked = !(bool)sqlDataReader["性别"];

                this.phone.Text = sqlDataReader["联系电话"].ToString();

            }

            sqlDataReader.Close();

        }

   private void button2_Click(object sender, EventArgs e)

        {

            if (this.no.Text.Trim() == "")                                                     

            {

                MessageBox.Show("编号不能为空!");                                                                    this.name.Focus();                                                               

                return;                                                                                             

        if (this.name.Text.Trim() == "")                                                     

            {

                MessageBox.Show("用户名不能为空!");                                                                   

 this.name.Focus();                                                                                

return;                                                              

}

            if (this.phone.Text.Trim() == "")   {

                MessageBox.Show("手机号不能为空!");                                                                   

 this.phone.Focus();                                                                               

 return;                                                             

 }

            SqlConnection sqlConnection = new SqlConnection();                                                      

sqlConnection.ConnectionString =

                "Server=(local);Database=药品工作站管理系统;Integrated Security=sspi";                                

SqlCommand sqlCommand = sqlConnection.CreateCommand();                                                  

sqlCommand.CommandText =

               "UPDATE 操作员信息"

                + " SET 姓名=@姓名,性别=@性别,联系电话=@联系电话"

                + " WHERE 操作员编号=@操作员编号;";

sqlCommand.Parameters.AddWithValue("@姓名", this.name.Text.Trim());                                     

sqlCommand.Parameters.AddWithValue("@性别", this.Male.Checked);

sqlCommand.Parameters.AddWithValue("@联系电话",this.phone.Text.Trim());

sqlCommand.Parameters.AddWithValue("@操作员编号", this.no.Text.Trim());

sqlConnection.Open();                                                                       

            int rowAffected = sqlCommand.ExecuteNonQuery();                                            

            sqlConnection.Close();                                                                                 

 if (rowAffected == 1)                                                                      

            {

MessageBox.Show("更新成功。");                                                                     

 }

            else   {

                MessageBox.Show("更新失败!");                                                          

            }

        }

 private void button3_Click(object sender, EventArgs e)

        {

            if (this.no.Text.Trim() == "")                                                                  {

                MessageBox.Show("编号不能为空!");                                                                 

 this.name.Focus();                                                                               

 return;                                                                                

            }

           if (this.name.Text.Trim() == "")                                                     

            {

                MessageBox.Show("用户名不能为空!");                                                                   

 this.name.Focus();                                                               

                return;                                                                                 

            }

            if (this.phone.Text.Trim() == "")                                                     

            {

                MessageBox.Show("手机号不能为空!");                                                    

                this.phone.Focus();                                                                               

 return;                                                                                 

            }

            SqlConnection sqlConnection = new SqlConnection();                                                     

 sqlConnection.ConnectionString =

                "Server=(local);Database=药品工作站管理系统;Integrated Security=sspi";                                  

SqlCommand sqlCommand = sqlConnection.CreateCommand();                                                  

sqlCommand.CommandText =

            "INSERT 操作员信息 (操作员编号,姓名,性别,联系电话) VALUES(@操作员编号,@姓名,@性别,@联系电话);";

            sqlCommand.Parameters.AddWithValue("@姓名", this.name.Text.Trim());                                    

sqlCommand.Parameters.AddWithValue("@性别", this.Male.Checked);

            sqlCommand.Parameters.AddWithValue("@联系电话", this.phone.Text.Trim());

            sqlCommand.Parameters.AddWithValue("@操作员编号", this.no.Text.Trim());

            int rowAffected = 0;                                                                       

    try                                                                                        

            {

                sqlConnection.Open();                                                                  

                rowAffected = sqlCommand.ExecuteNonQuery();                                            

            }

            catch (SqlException sqlEx)  {

                if (sqlEx.Number == 2627)                                                              

                {

MessageBox.Show("您增加的用操作员已存在,请重新输入!\n");                                          

}

          }

            finally                                                                                                 {

                sqlConnection.Close();                                                                 

            }

            if (rowAffected == 1)                                                                       

            {

                MessageBox.Show("添加成功。");                                                         

            }

            else                                                                                        

            {

                MessageBox.Show("添加失败!");                                                         

            }

        }

    }

 

 

6. DataGridview

DataSource 属性:绑定数据源

代码如下:

public partial class 药品信息查询 : Form

    {

       

        public 药品信息查询()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection sqlConnection = new SqlConnection();                                             

            sqlConnection.ConnectionString =

                "Server=(local);Database=药品工作站管理系统;Integrated Security=sspi";                                        

SqlCommand sqlCommand = new SqlCommand();                                                                   

sqlCommand.Connection = sqlConnection;                                                         

            sqlCommand.CommandText = "SELECT * FROM 药品信息;";                                          

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                          

            sqlDataAdapter.SelectCommand = sqlCommand;                                                     

            DataTable Table = new DataTable();                                                                   

sqlConnection.Open();                                                                                       

sqlDataAdapter.Fill(Table);                                                                          

sqlConnection.Close();                                                                                      

this.dgv.DataSource =Table;                                                      

  }

}

运行结果如下:

 

posted @ 2018-10-17 20:54  jiangfan  阅读(341)  评论(0编辑  收藏  举报