
Code

开机自动启动并最小化到系统托盘#region 开机自动启动并最小化到系统托盘


/**////开机自动启动:

///拖一个CheckBox

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

RegistryKey R_local = Registry.LocalMachine;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (R_run.GetValue("BirthdayTipF") == null)

{
checkBox1.Checked = false;
}
else

{
checkBox1.Checked = true;
}
R_run.Close();
R_local.Close();

//CheckChanged事件:

private void checkBox1_CheckedChanged(object sender, EventArgs e)

{
string R_startPath = Application.ExecutablePath;
if (checkBox1.Checked == true)

{
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

{
try

{
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)

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


/**////最小化到系统托盘

//拖一个NotifyIcon

//设置form的showInTastbar属性为false

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

private void Form1_SizeChanged(object sender, EventArgs e)

{
if (this.WindowState == FormWindowState.Minimized)

{
this.Hide();
this.notifyIcon1.Visible = true;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)

{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}

#endregion


关闭窗体时执行#region 关闭窗体时执行
private void SalesMain_FormClosing(object sender, FormClosingEventArgs e)

{
if (MessageBox.Show("您确定要退出系统吗?", "退出系统", MessageBoxButtons.OKCancel) == DialogResult.OK)

{
}
else

{
e.Cancel = true;
}
}

private void SalesMain_FormClosed(object sender, FormClosedEventArgs e)

{
Application.Exit();
}
#endregion


删除#region 删除

if(MessageBox.Show("删除的行不可恢复,你确定要删除吗?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)

{
int ReCount=ds.Tables["CurrentTable"].Rows.Count;
for(int i=0;i<ReCount;i++)

{
if(this.dataGrid1.IsSelected(i))

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

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

#endregion



Enter发送Tab#region Enter发送Tab

private void TextBox1_KeyUp(object sender, KeyEventArgs e)

{
if (e.KeyValue.ToString() == "13 ")//确认按下回车键 或者if (e.KeyCode == Keys.Enter)

{ 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


dataGridView显示行号#region dataGridView显示行号

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)

{
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


将DataSet 更新到数据库#region 将DataSet 更新到数据库#region 将DataSet 更新到数据库#region 将DataSet 更新到数据库
private void button3_Click(object sender, EventArgs e)

{
SqlConnection conn=new SqlConnection(connstr1);
if (ds.GetChanges() != null)

{

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

{

//与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)

{

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

}



private SqlConnection GetConn()

{

try

{
SqlConnection Connection = new SqlConnection(this._connString);
Connection.Open();
return Connection;

}


catch (Exception ex)

{

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

}

}


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


public DataSet SelectDb(string strSql, string strTableName)

{

try

{

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)

{

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

}

}



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


public DataSet UpdateDs(DataSet changedDs, string tableName)

{

try

{
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)

{

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

}

}
}


#endregion




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