【发布时间】:2019-03-08 12:29:51
【问题描述】:
我想集成一个自动检查功能,以防止用户忘记保存更改,并带有文本框“您要保存更改吗”是-否
如果是 -> 保存 如果没有 -> 返回
这是我没有检查的代码
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
OleDbConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new
OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data
Source=" + @Application.StartupPath + "\\Database1.mdb");
fill_lb();
}
private void fill_lb()
{
listBox1.Items.Clear();
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [table1] ORDER BY firstn";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
listBox1.Items.Add(dr["firstn"].ToString());
}
conn.Close();
}
private void listBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [table1] WHERE firstn='" +
listBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox_fn.Text = dr["firstn"].ToString();
textBox_ln.Text = dr["lastn"].ToString();
}
conn.Close();
}
private void button_savenew_Click(object sender, EventArgs e)
{
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO [table1] ([firstn],[lastn])
values ([@firstn],[@lastn])";
cmd.Parameters.AddWithValue("@firstn", textBox_fn.Text);
cmd.Parameters.AddWithValue("@lastn", textBox_ln.Text);
cmd.ExecuteNonQuery();
fill_lb();
conn.Close();
}
private void button_modify_Click(object sender, EventArgs e)
{
if (conn.State != ConnectionState.Open) { conn.Close();
conn.Open(); }
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "UPDATE [Table1] SET [firstn]=[@firstn],
[lastn]=[@lastn] WHERE firstn = '" +
listBox1.SelectedItem.ToString() + "'";
cmd.Parameters.AddWithValue("@firstn", textBox_fn.Text);
cmd.Parameters.AddWithValue("@lastn", textBox_ln.Text);
cmd.ExecuteNonQuery();
fill_lb();
conn.Close();
}
private void button_new_Click(object sender, EventArgs e)
{
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
}
}
}
我做了什么没有成功:
Bool modified = false
private void textBox_fn_TextChanged(object sender, EventArgs e)
{
modified = true;
}
private void textBox_ln_TextChanged(object sender, EventArgs e)
{
modified = true;
}
private void listBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
if (modified.Equals(true))
{
DialogResult dialogr = MessageBox.Show("Do you want to
save change ?","", MessageBoxButtons.YesNo);
switch (dialogr)
{
case DialogResult.Yes:
button_savenew.PerformClick();
modifie = false;
break;
case DialogResult.No:
return;
}
}
modified = false;
textBox_fn.Text = string.Empty;
textBox_ln.Text = string.Empty;
}
这不起作用,因为每次我点击列表框时它都会要求保存
我能做什么?
【问题讨论】:
-
也许您应该在表单的
OnClosing事件中而不是在列表的SelectedIndexChanged中执行检查 -
是的,我可以在 OnClosing 添加一个检查,但是,如果我正在编辑一个条目并且我错过了从列表框中单击另一个条目,我也想执行该检查。
标签: c# database winforms listbox