[基于回复的编辑版本]
我只希望同时填充文本框和datetimepicker才能启用button1。我可以为文本做,但不能为datetimepicker做。这是我的完整代码。仅供参考:我的datetimepicker最初被格式化为空,以便用户自己选择日期。
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;
namespace Inspection
{
public partial class Vdetails : Form
{
public Vdetails()
{
InitializeComponent();
Vnew.Enabled = false;
}
//Allow user input date
private void DTafes_ValueChanged(object sender, EventArgs e)
{
DTafes.CustomFormat = @"";
}
private void DTfa_ValueChanged_1(object sender, EventArgs e)
{
DTfa.CustomFormat = @"";
}
private void DTfe_ValueChanged(object sender, EventArgs e)
{
DTfe.CustomFormat = @"";
}
//Condition to allow button to go next
private void ActivateButton()
{
Button1.Enabled = (Vehicle.Text != "" && Distance.Text != "");
}
private void Vehicle_TextChanged(object sender, EventArgs e)
{
ActivateButton();
}
private void Distance_TextChanged(object sender, EventArgs e)
{
ActivateButton();
}
}
我如何添加代码以及如何添加代码以允许填充datetimepicker。希望有人能帮助我。
答案 0 :(得分:0)
DateTimePicker总是有一个值,但是您可以收听ValueChanged事件
private bool dateChanged;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
dateChanged = false;
button1.Enabled = false;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateChanged = true;
SetEnabledness();
}
private void SetEnabledness()
{
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text) && dateChanged;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SetEnabledness();
}
如果您有多个DateTimePicker,则可以使用HashSet代替bool(只需确保将所有ValueChangedEvents连接到dp_ValueChanged例如
private HashSet<DateTimePicker> dateChanged = new HashSet<DateTimePicker>();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
dateChanged.Clear();
button1.Enabled = false;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker2.Format = DateTimePickerFormat.Custom;
dateTimePicker3.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = @" ";
dateTimePicker2.CustomFormat = @" ";
dateTimePicker3.CustomFormat = @" ";
}
private void dp_ValueChanged(object sender, EventArgs e)
{
var dp = (DateTimePicker) sender;
dp.Format = DateTimePickerFormat.Short;
dateChanged.Add(dp);
SetEnabledness();
}
private void SetEnabledness()
{
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text) && dateChanged.Count == 3;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SetEnabledness();
}
答案 1 :(得分:0)
dateTimePicker没有textchanged事件。 dateTimePicker内永远不会有空字符串。 (也许可以通过代码来设置它,但是不能通过用户输入来设置它)。当用户更改日期时,可以使用dateTimePicker的valueChanged-event进行操作。
由于文本永远不会为空,因此您不必在ActiveButton函数中对其进行检查。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ActiveateButton()
{
this.button1.Enabled = textBox1.Text != string.Empty;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ActiveateButton();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
ActiveateButton();
}
}