1.创建winfrom项目 ,在load事件中写方法(只看load事件里的代码就可以)
ps:如需要项目的源码,可去我的资源中下载
from1窗体代码
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test111111111111111
{
public partial class Form1 : Form
{
RegistryKey key = Registry.LocalMachine; 获取到 当前登陆用户的 注册表启动项 必须声明为全局
public static string KeyID = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//读写注册列表
RegistryKey software = key.OpenSubKey("software\\test12", true);
//没有注册表信息
if (software == null) //判断如果没有
{
Form2 fm2 = new Form2();
MessageBox.Show("机器尚未注册,请先注册");
fm2.ShowDialog();//调取from2窗体
software = key.CreateSubKey("software\\test12");//创建注册列表文件
software.SetValue("KeyID", KeyID.ToString());//向注册列表文件中写入数据
}
//有注册表信息
else
{
KeyID = software.GetValue("KeyID", "0").ToString();//如果不为null将注册列表的数据读出来
}
label1.Text = KeyID;//将读出的数据给label1
//this.Width = 0;//让窗体消失
//this.Height = 0;
//this.Hide();
}
private void timer1_Tick(object sender, EventArgs e)
{
//label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
// if (label2.Text.Contains("11:35:00"))
//{
// Thread.Sleep(1000);
// this.Width = 527;//让窗体显示
// this.Height = 357;
//this.Show();
//}
}
}
}
from2窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace test111111111111111
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load_1(object sender, EventArgs e)
{
string constr = "Server =.; Database =test; User ID = sa ; Password = 123;";
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from demo1", con);
DataSet ds = new DataSet();
sda.Fill(ds, "dept");
部门
comboBox1.DataSource = ds.Tables["dept"];
comboBox1.DisplayMember = "dept";
comboBox1.ValueMember = "dept";
}
private void button1_Click_1(object sender, EventArgs e)
{
//获取comboBox1选定的值
string aa = comboBox1.SelectedValue.ToString();
//MessageBox.Show(aa);
if (aa != "" && aa != null)
{
Form1.KeyID = aa;//将从数据查到的数据给from1中的KeyID,然后写到注册列表
this.Close();
}
else
{
MessageBox.Show("请选择部门信息");
return;
}
}
}
}