I have around 150 lines of code that I managed to optimize.
But, because my two methods and the six if-statements almost are identical, could there be room for improvement? Maybe it could be done with only one button and one listbox?
As I'm out of ideas and could learn more myself, I'd like to know what could be done to improve this code.
public Form1()
{
InitializeComponent();
timer1.Interval = 100;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
lblClock.Text = DateTime.Now.ToString("HH:mm:ss");
}
// ADD values BUTTON
private void button8_Click(object sender, EventArgs e)
{
AddValues();
}
// REMOVE values BUTTON
private void button7_Click(object sender, EventArgs e)
{
RemoveValues();
}
private void AddValues()
{
// ADD hours
if (!box_Hour.Items.Contains(DateTime.Now.Hour))
{
box_Hour.Items.Add(DateTime.Now.Hour);
}
// ADD minutes
if (!box_Minute.Items.Contains(DateTime.Now.Minute))
{
box_Minute.Items.Add(DateTime.Now.Minute);
}
// ADD seconds
if (!box_Second.Items.Contains(DateTime.Now.Second))
{
box_Second.Items.Add(DateTime.Now.Second);
}
}
private void RemoveValues()
{
// REMOVE hours
if (box_Hour.Items.Contains(DateTime.Now.Hour))
{
box_Hour.Items.Remove(DateTime.Now.Hour);
}
// REMOVE minutes
if (box_Minute.Items.Contains(DateTime.Now.Minute))
{
box_Minute.Items.Remove(DateTime.Now.Minute);
}
// REMOVE seconds
if (box_Second.Items.Contains(DateTime.Now.Second))
{
box_Second.Items.Remove(DateTime.Now.Second);
}
}
button7
, change that to descriptive names. – svick Aug 31 '13 at 11:09