Winform常用代码

 

ContractedBlock.gifExpandedBlockStart.gifCode
ContractedBlock.gifExpandedBlockStart.gif开机自动启动并最小化到系统托盘#region 开机自动启动并最小化到系统托盘

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////开机自动启动:

///拖一个CheckBox


//软件启动时给CheckBox重置状态:

RegistryKey R_local 
= Registry.LocalMachine;
            RegistryKey R_run 
= R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            
if (R_run.GetValue("BirthdayTipF"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                checkBox1.Checked 
= false;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                checkBox1.Checked 
= true;
            }

            R_run.Close();
            R_local.Close();

//CheckChanged事件:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string R_startPath = Application.ExecutablePath;
            
if (checkBox1.Checked == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                RegistryKey R_local 
= Registry.LocalMachine;
                RegistryKey R_run 
= R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                R_run.SetValue(
"BirthdayTipF", R_startPath);
                R_run.Close();
                R_local.Close();
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    RegistryKey R_local 
= Registry.LocalMachine;
                    RegistryKey R_run 
= R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue(
"BirthdayTipF"false);
                    R_run.Close();
                    R_local.Close();
                }

                
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    MessageBox.Show(
"您需要管理员权限修改""提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    
throw;
                }

                
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////最小化到系统托盘

//拖一个NotifyIcon

//设置form的showInTastbar属性为false

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

private void Form1_SizeChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (this.WindowState == FormWindowState.Minimized)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.Hide();
                
this.notifyIcon1.Visible = true;
            }

        }

        
private void notifyIcon1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.Visible = true;
            
this.WindowState = FormWindowState.Normal;
            
this.notifyIcon1.Visible = false;
        }


#endregion


ContractedBlock.gifExpandedBlockStart.gif
关闭窗体时执行#region 关闭窗体时执行
        
private void SalesMain_FormClosing(object sender, FormClosingEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (MessageBox.Show("您确定要退出系统吗?""退出系统", MessageBoxButtons.OKCancel) == DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                e.Cancel 
= true;
            }

        }


        
private void SalesMain_FormClosed(object sender, FormClosedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Application.Exit();
        }

        
#endregion


ContractedBlock.gifExpandedBlockStart.gif   
删除#region 删除

    
if(MessageBox.Show("删除的行不可恢复,你确定要删除吗?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
{
    
int ReCount=ds.Tables["CurrentTable"].Rows.Count; 
    
for(int i=0;i<ReCount;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
     
if(this.dataGrid1.IsSelected(i))
ExpandedSubBlockStart.gifContractedSubBlock.gif     
{
      ds.Tables[
"CurrentTable"].Rows["准考证号"]="*";
     }

    
    }

    DataRow[] findRows
=ds.Tables["CurrentTable"].Select("准考证号='*'");
    
for(int i=0;i<findRows.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
     findRows.Delete();
    }

    ds.Tables[
"CurrentTable"].AcceptChanges();
   }


#endregion


 

ContractedBlock.gifExpandedBlockStart.gif
Enter发送Tab#region Enter发送Tab

private   void   TextBox1_KeyUp(object   sender,   KeyEventArgs   e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                        
if   (e.KeyValue.ToString()   ==   "13 ")//确认按下回车键 或者if (e.KeyCode == Keys.Enter)
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        {   SendKeys.Send( "{Tab} ");   }//发送Tab键 
                }


  
//然后使用委托

this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
.
.
.
this.textBox20.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);

#endregion


ContractedBlock.gifExpandedBlockStart.gif
dataGridView显示行号#region dataGridView显示行号

     
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Rectangle rectangle 
= new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                dataGridView1.RowHeadersWidth 
- 4,
                e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex 
+ 1).ToString(),
                dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter 
| TextFormatFlags.Right);

        }


#endregion


ContractedBlock.gifExpandedBlockStart.gif
将DataSet 更新到数据库#region 将DataSet 更新到数据库#region 将DataSet 更新到数据库#region 将DataSet 更新到数据库
private void button3_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
          
            SqlConnection conn
=new SqlConnection(connstr1);
                    
if (ds.GetChanges() != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                
string connstr1 = "server=.;User ID=sa;Password=sa;database=users";
                sqlAccess sa 
= new sqlAccess(connstr1,"select * from a where 1=2");
                DataSet newds
=sa.UpdateDs(ds, "a");
                ds 
= null;
                ds 
= newds.Copy();
            }

            

        }

//sqlAccess.cs
      public class sqlAccess
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

        
//与SQL Server的连接字符串设置 


         
private string _connString;
        
private string _strSql;
        
private SqlCommandBuilder sqlCmdBuilder;
        
private DataSet ds = new DataSet();
        
private SqlDataAdapter da;


        
public sqlAccess(string connString, string strSql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
this._connString = connString;
            
this._strSql = strSql;

        }




        
private SqlConnection GetConn()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                SqlConnection Connection 
= new SqlConnection(this._connString);
                Connection.Open();
                
return Connection;

            }



            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                MessageBox.Show(ex.Message, 
"数据库连接失败");
                
throw;

            }


        }



        
//根据输入的SQL语句检索数据库数据 


        
public DataSet SelectDb(string strSql, string strTableName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                
this._strSql = strSql;
                
this.da = new SqlDataAdapter(this._strSql, this.GetConn());
                
this.ds.Clear();
                
this.da.Fill(ds, strTableName);
                
return ds;//返回填充了数据的DataSet,其中数据表以strTableName给出的字符串命名 

            }



            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                MessageBox.Show(ex.Message, 
"数据库操作失败");
                
throw;

            }


        }




        
//数据库数据更新(传DataSet和DataTable的对象) 


        
public DataSet UpdateDs(DataSet changedDs, string tableName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.da = new SqlDataAdapter(this._strSql, this.GetConn());
                
this.sqlCmdBuilder = new SqlCommandBuilder(da);

                
// this.da.Update(changedDs, tableName);
                da.Update(changedDs.Tables[0]);
                changedDs.AcceptChanges();
                
return changedDs;//返回更新了的数据库表 

            }


            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                MessageBox.Show(ex.Message, 
"数据库更新失败");
                
throw;

            }


        }

    }



#endregion




 

 

转载于:https://www.cnblogs.com/liran/archive/2009/04/08/1431741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值