【发布时间】:2010-09-15 20:53:26
【问题描述】:
我正在使用 C# 和 Visual Studio 2005。
我在运行时在 FlowlayoutPanel 中创建了多个 Texbox。它工作正常,但是当我尝试处理空文本框并像下面这样放置消息时。
void tbb_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Enter)
{
listBox2.Visible = false;
button4.Visible = false;
if (tbb.Text!="")
{
bb.Visible = true;
bb.Focus();
}
else
{
//tbb.Visible = false;
tbb.Dispose();
bb.Dispose();
textBox2.Visible = true;
textBox2.Focus();
}
}
}
上面的代码工作正常并且在运行时处理得很好。 数据保存代码为:
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Enter)
{
if (bb.Text == "")
{
MessageBox.Show("Sorry Empty.Row");
this.flowLayoutPanel1.Controls.Clear();
label13.Text = "";
textBox1.Text = "";
textBox2.Text = "";
maskedTextBox1.Text = "";
maskedTextBox1.Enabled = true;
maskedTextBox1.Focus();
textBox1.Enabled = true;
}
else
{
string connstr = "server=.;initial catalog= maa;uid=mah;pwd=mah";
SqlConnection con = new SqlConnection(connstr);
con.Open();
SqlCommand cmd1 = new SqlCommand("insert into debankA(companyID,transID,date,bank,totdepo,narrat) values " +
"(@companyID,@transID,Convert(datetime,'" + maskedTextBox1.Text.ToString() + "',103),@bank,@totdepo,@narrat)", con);
cmd1.Parameters.Add("@bank", SqlDbType.VarChar).Value = textBox1.Text;
cmd1.Parameters.Add("@totdepo", SqlDbType.Decimal).Value = label13.Text;
cmd1.Parameters.Add("@narrat", SqlDbType.VarChar).Value = textBox2.Text;
cmd1.Parameters.Add("@companyID", SqlDbType.Int).Value = label6.Text;
cmd1.Parameters.Add("@transID", SqlDbType.Int).Value = textBox4.Text;
cmd1.ExecuteNonQuery();
string pparticulars = null;
double? depo = null;
string messs = "Record Save Successfully";
foreach (Control ctl in this.flowLayoutPanel1.Controls)
{
if (ctl.Name.Contains("tbb") && ctl is TextBox)
{
pparticulars = ctl.Text;
}
if (ctl.Name.Contains("bb") && ctl is TextBox)
{
double ddepo = 0;
if (double.TryParse(ctl.Text, out ddepo))
depo = ddepo;
if (pparticulars != null && depo != null)
{
SqlCommand cmd = new SqlCommand("insert into debankB(particulars,deposit,companyID,transID)values" +
"(@particulars,@deposit,@companyID,@transID)", con);
cmd.Parameters.Add("@particulars", SqlDbType.VarChar).Value = pparticulars;
cmd.Parameters.Add("@deposit", SqlDbType.Decimal).Value = depo;
cmd.Parameters.Add("@companyID", SqlDbType.Int).Value = label6.Text;
cmd.Parameters.Add("@transID", SqlDbType.Int).Value = textBox4.Text;
cmd.ExecuteNonQuery();
pparticulars = null;
depo = null;
MessageBox.Show(messs);
messs = null;
this.flowLayoutPanel1.Controls.Clear();
label13.Text = "";
textBox1.Text = "";
textBox2.Text = "";
maskedTextBox1.Text = "";
maskedTextBox1.Enabled = true;
maskedTextBox1.Focus();
textBox1.Enabled = true;
}
尽管我已经处理了两个空文本框消息,但始终只显示我上面设置的“empty.records”。
这意味着空文本框不会被释放。但如果这是真的,那么当我运行应用程序并创建可用数据的文本框时,它保持不变并且空文本框不可见。在进入时处理。
我不明白是什么问题。如果文本框在运行时被释放,那么它如何显示为空?
【问题讨论】:
-
请重新格式化您的代码示例并确保它们有效(很多缺少
})。 -
@Oded 先生,这是短代码。你的意思是整个编码定位。
-
@Oded 如果你需要,我会提交。告诉我。
-
你有
ifs 有开放{,没有}和else块。至少发布一些可以编译的东西。阅读有关提出好问题的信息 - tinyurl.com/so-hints -
@Oded:相信我,你不想看到真正的代码。