我有一个链接到MySQL数据库的表单。 我希望在表单上有3个 dateTimePickers 以允许三个日期,但是,如果没有选择一个,我希望该Picker显示为空白,而不是当前日期。
我正在使用以下代码,当我更改日期时,我收到错误“System.Windows.Forms.dll”中出现类型'System.StackOverflowException'的未处理异常选择器。
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (txtdateTimePicker1.Text != "1/1/2000 12:00:00 AM")
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
dateTimePicker1.Text = txtdateTimePicker1.Text;
}
else
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
dateTimePicker1.Text = "";
}
}
故障排除提示告诉我:确保没有无限循环或无限递归。
基本上,文本框 “txtdateTimePicker1” 在表单加载时填充日期值,如果它读取某个日期,则 “dateTimePicker1 “ 显示空值。
我认为循环发生是因为它查看 txtdateTimePicker1 的值,然后更改 dateTimePicker1 的值,从而导致它再次触发ValueChanged事件,但是我现在知道任何其他方式来正确触发事件。有没有办法使用While循环?有些话说:
While (txtdateTimePicker1.Text == "1/1/2000 12:00:00 AM")
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
dateTimePicker1.Text = "";
}
但
if (txtdateTimePicker1.Text != "1/1/2000 12:00:00 AM")
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
dateTimePicker1.Text = txtdateTimePicker1.Text;
}
我对While循环很新,我确实研究了一些,我知道它们如何工作,但我真的不知道语法。
或者...因为在初始化表单时填充了文本框,有没有办法可以触发代码我需要一种不同的方式?
此外,我知道我可能不会在这里使用最佳实践方法,这将在稍后出现。我需要尝试使其功能正常,除了这一个事件,我的表单的其余部分才能正常工作。请不要因为没有使用正确的方法而责备我,相反,请帮我解决手头的问题。
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 MySql.Data.MySqlClient;
namespace _2014BillOfMaterials
{
public partial class BillofMaterialsRevisions : Form
{
public BillofMaterialsRevisions(string s)
{
if (s != "- Select a Job -")
{
string server = "192.168.1.149";
string database = "####";
string userid = "iuapp";
string password = "iuapp";
string str;
str = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + userid + ";" + "PASSWORD=" + password + ";";
MySqlConnection con = null;
con = new MySqlConnection(str);
MySqlConnection myconn = new MySqlConnection(str);
MySqlDataAdapter jobNumber = new MySqlDataAdapter("SELECT * FROM jobnumbers where jobNumber = '" + s + "'", myconn);
DataTable dtJobNumber = new DataTable("jobnumbers");
DataSet dtJobNumber2 = new DataSet();
jobNumber.Fill(dtJobNumber);
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM jobnumbers where jobNumber = '" + s + "'", myconn);
adapter.Fill(dtJobNumber2);
textBox2.Text = dtJobNumber2.Tables[0].Rows[0][6].ToString();
textBox1.Text = dtJobNumber2.Tables[0].Rows[0][8].ToString();
textBox3.Text = dtJobNumber2.Tables[0].Rows[0][10].ToString();
textBox4.Text = dtJobNumber2.Tables[0].Rows[0][0].ToString();
txtdateTimePicker1.Text = dtJobNumber2.Tables[0].Rows[0][5].ToString();
txtdateTimePicker2.Text = dtJobNumber2.Tables[0].Rows[0][7].ToString();
txtdateTimePicker3.Text = dtJobNumber2.Tables[0].Rows[0][9].ToString();
lblJobNumber.Text = s + " Revisions";
label10.Text = s;
}
else
{
//Show Error MessageBox
MessageBox.Show("Please Select a Job.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void BillofMaterialsRevisions_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string server = "192.168.1.149";
string database = "####";
string userid = "iuapp";
string password = "iuapp";
string str;
str = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + userid + ";" + "PASSWORD=" + password + ";";
MySqlConnection myconn2 = new MySqlConnection(str);
MySqlConnection con = null;
con = new MySqlConnection(str);
con.Open();
string date1 = dateTimePicker1.Value.ToString("yyyy-MM-dd");
string date2 = dateTimePicker2.Value.ToString("yyyy-MM-dd");
string date3 = dateTimePicker3.Value.ToString("yyyy-MM-dd");
var cmd = new MySqlCommand("Insert into jobnumbers(id, jobNumber, IssuedDate, IssuedInitials, RevADate, RevAInitials, RevBDate, RevBInitials) SELECT DISTINCT '" +
textBox4.Text + "', '" +
label10.Text + "', '" +
date1 + "', '" +
textBox2.Text + "', '" +
date2 + "', '" +
textBox1.Text + "', '" +
date3 + "', '" +
textBox3.Text +
"' " +
"ON DUPLICATE KEY UPDATE id = '" + textBox4.Text + "' " +
", jobNumber = '" + label10.Text + "' " +
", IssuedDate = '" + date1 + "' " +
", IssuedInitials = '" + textBox2.Text + "' " +
", RevADate = '" + date2 + "' " +
", RevAInitials = '" + textBox1.Text + "' " +
", RevBDate = '" + date3 + "' " +
", RevBInitials = '" + textBox3.Text + "'", con);
cmd.ExecuteNonQuery();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (txtdateTimePicker1.Text == "1/1/2000 12:00:00 AM")
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
dateTimePicker1.Text = txtdateTimePicker1.Text;
}
else
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
dateTimePicker1.Text = "";
}
}
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
if (txtdateTimePicker2.Text != "" || txtdateTimePicker2.Text != "1/1/2000 12:00:00 AM")
{
dateTimePicker2.Format = DateTimePickerFormat.Short;
dateTimePicker2.Text = txtdateTimePicker2.Text;
}
else
{
dateTimePicker2.Format = DateTimePickerFormat.Custom;
dateTimePicker2.CustomFormat = " ";
dateTimePicker2.Text = "";
}
}
private void dateTimePicker3_ValueChanged(object sender, EventArgs e)
{
if (txtdateTimePicker3.Text != "" || txtdateTimePicker3.Text != "1/1/2000 12:00:00 AM")
{
dateTimePicker3.Format = DateTimePickerFormat.Short;
dateTimePicker3.Text = txtdateTimePicker3.Text;
}
else
{
dateTimePicker3.Format = DateTimePickerFormat.Custom;
dateTimePicker3.CustomFormat = " ";
dateTimePicker3.Text = "";
}
}
}
}
答案 0 :(得分:1)
不允许将"" (Empty)
设置为Text
属性,因为Text
属性应该是有效的 DateTime对象。查看其documentation。
解决此问题的可能性是禁用DateTimePicker
控件并在需要时将其启用。
使用:
dateTimePicker1.Enabled = True; // enables it
dateTimePicker1.Enabled = False; // disable it
希望它有所帮助!
答案 1 :(得分:0)
我所做的是创建了一个Checkbox,它更改了dateTimePicker和相关textBoxes的CustomFormat属性和Text Value,并且我已经将我的数据库中字段的数据类型从DATE更改为Varchar,这似乎正常工作。此特定应用程序必须以某种方式工作,并且这些表不会用于需要日期为特定数据类型的大量报表。
我意识到还有很多其他更好的方法可以实现这一目标,但是我有点时间限制,所以现在这样做。
NeverHopeless给了我一个解决方案,在不同情况下会有效,但对于这个项目来说,这还不够。
以下是Checkboxes的代码:
public void CheckCheckBoxes2()
{
if (checkBox2.Checked == false)
{
dateTimePicker2.Format = DateTimePickerFormat.Custom;
dateTimePicker2.CustomFormat = " ";
dateTimePicker2.Text = null;
dateTimePicker2.Enabled = false;
textBox2.Text = null;
textBox2.Enabled = false;
}
else
{
dateTimePicker2.Format = DateTimePickerFormat.Short;
dateTimePicker2.Enabled = true;
textBox2.Enabled = true;
}
}