How can I delete all Panels from my calendar? - c#

I'm programming an calendar with C# right now.
If i call my calendar, it creates as much panels as the current month has days. But if I want to increase the current month by one, the panels from the current month are stilt there.
So i have to delete all my panels as soon as I change the month.
But how can i do it in this case ?
Thanks for the help.
Code eplain:
First I call the createPanel method, to create panels for the current month.
Next if I click the MonthAdd method, I want to delete all my created panels.
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 System.Globalization;
namespace Einteilungs_Plan
{
public partial class Kalender : Form
{
public DateTime TodayDate { get; set; }
int counting = 0;
public Kalender()
{
InitializeComponent();
//Kalenderwochen initialisieren
monat(counting);
createPanel(true);
}
public string monat(int adding)
{
string monat = DateTime.Now.AddMonths(adding).ToString("MMMM");
tbMonat.Text = monat;
return monat;
}
private void btnAddMonth_Click(object sender, EventArgs e)
{
counting++;
if(counting < 12)
{
monat(counting);
switch (counting)
{
case 0:
int number = 10;
break;
case 1:
break;
default:
break;
}
}
else
{
counting--;
}
}
private void btnRemoveMonth_Click(object sender, EventArgs e)
{
counting--;
if (counting > -1)
{
monat(counting);
}
else
{
counting++;
}
}
public void createPanel(bool remove)
{
var numDays = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
int locationX = 12;
int locationY = 74;
for (int i = 0; i <= numDays; i++)
{
//Create Panel
Panel test = new Panel();
//Fill Panel
test.Name = "panel" + i;
test.Width = 200;
test.Height = 100;
test.BackColor = Color.White;
test.Location = new System.Drawing.Point(locationX, locationY);
this.Controls.Add(test);
test.Show();
if(i == 6 || i == 13 || i == 20 || i == 28)
{
locationY += 106;
locationX = -194;
}
locationX += 206;
}
}
public void Kalender_Shown(object sender, EventArgs e)
{
}
private void Kalender_Load(object sender, EventArgs e)
{
}
private void btnNeuerEintrag_Click(object sender, EventArgs e)
{
Formular formular = new Formular();
formular.Show();
formular.Focus();
}
private void btnHinzufügen_Click(object sender, EventArgs e)
{
Formular formular = new Formular();
formular.Show();
formular.Focus();
}
}
}

...
for (int i = 0; i <= numDays; i++)
{
//Create Panel
test[i] = new Panel();
}
...
and then
this.Control.Remove(test[i]);

I'm not sure if I undestood you well but first simple solutiona that comes into my mind is that while creating panels you can keep them in some List for example and before generating new month you can call
foreach (var p in panels)
this.Controls.Remove(p);

Related

Simon Game in C#

I've been creating the game Simon in a windows form using C#. I'm having a problem in the labels that blink to show the pattern. When one label is required to blink twice (because it appears in the pattern twice) it will only blink once. Also, in general the labels will sometimes not blink in the correct order they are meant to (i.e the second in the pattern blinks before the first). Any assistance in how to fix this or in general how to improve on my code would be great. I have only been using C# for the last few weeks and it's part of a university project.
Have attached the code and a picture of what the windows form looks like.
Windows Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Simon2
{
public partial class Form1 : Form
{
List<int> sequence = new List<int>();
Random rnd = new Random();
int number = 0;
public Form1()
{
InitializeComponent();
sequence.Add(rnd.Next(0, 4));
hey();
}
void hey()
{
foreach (int colour in sequence)
{
switch (colour)
{
case 0: {
timer1.Enabled = true;
break;
}
case 1: {
timer2.Enabled = true;
break;
}
case 2: {
timer3.Enabled = true;
break;
}
case 3: {
timer4.Enabled = true;
break;
}
}
}
}
void pattern(int colour)
{
if (sequence[number] == colour)
{
label1.Text = ("Score: " + sequence.Count);
sequence.Add(rnd.Next(0, 4));
number = 0;
hey();
}
else
{
MessageBox.Show("Fail!");
Application.Exit();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Red1.BackColor == Color.Transparent)
{
Red1.BackColor = Color.Red;
timer1.Interval = 300;
}
else
{
Red1.BackColor = Color.Transparent;
timer1.Interval = 300;
timer1.Stop();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Blue1.BackColor == Color.Transparent)
{
Blue1.BackColor = Color.Blue;
timer2.Interval = 300;
}
else
{
Blue1.BackColor = Color.Transparent;
timer2.Interval = 300;
timer2.Stop();
}
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Yellow1.BackColor == Color.Transparent)
{
Yellow1.BackColor = Color.Yellow;
timer3.Interval = 300;
}
else
{
Yellow1.BackColor = Color.Transparent;
timer3.Interval = 300;
timer3.Stop();
}
}
private void timer4_Tick(object sender, EventArgs e)
{
if (Green1.BackColor == Color.Transparent)
{
Green1.BackColor = Color.Lime;
timer4.Interval = 300;
}
else
{
Green1.BackColor = Color.Transparent;
timer4.Interval = 300;
timer4.Stop();
}
}
private void Red_Click(object sender, EventArgs e)
{
pattern(0);
}
private void Blue_Click(object sender, EventArgs e)
{
pattern(1);
}
private void Yellow_Click(object sender, EventArgs e)
{
pattern(2);
}
private void Green_Click(object sender, EventArgs e)
{
pattern(3);
}
}
}
I am not familiar with the game itself, my understanding is that one light after the other has to light up.
My suggestion: Use Thread.sleep (UI will not be responsive while this does it's thing), instead of timers, directly in the switch:
switch (colour){
case 0: {
Red1.BackColor = Color.Red;
Thread.Sleep(500);
Red1.BackColor = Color.Transparent;
break;
}
edit:
a better way would be to use a while loop which checks if a certain amount of ms elapsed and put Application.DoEvents(); in there

How to remove PictureBoxes dynamically?

I am trying to make pictureboxes fall continuously in the form.
Here is the code that I tried.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Rain_dropz
{
public partial class Form1 : Form
{
PictureBox[] RD = new PictureBox[500];
int ndrop = 0;
public Form1()
{
InitializeComponent();
}
public void dropIt()
{
for (int i = 0; i < ndrop; i++)
{
RD[i].Top += 10;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int l = rnd.Next(1,545);
RD[ndrop] = new PictureBox();
RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
RD[ndrop].Size = new Size(5, 5);
RD[ndrop].Location = new Point(l, 0);
this.Controls.Add(RD[ndrop]);
ndrop++;
dropIt();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
}
}
I think it is better to delete the picture boxes which disappear from the form. How to do that?
You can remove it by removing the picturebox from form controls list.
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int l = rnd.Next(1,545);
RD[ndrop] = new PictureBox();
RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
RD[ndrop].Size = new Size(5, 5);
RD[ndrop].Location = new Point(l, 0);
RD[ndrop].LocationChanged += pb_LocationChanged;
this.Controls.Add(RD[ndrop]);
ndrop++;
dropIt();
}
void pb_LocationChanged(object sender, EventArgs e)
{
// FORM_LASTBOUND is the Y-Axis point after which you wanted to remove the picturebox.
if ((sender as PictureBox).Top > FORM_LASTBOUND)
{
this.Controls.Remove(sender as PictureBox);
}
}

Refreshing a chart control

This one is driving me a bit crazy. Any help gratefully received. It's a simple program to receive temperature data from an Arduino based temp sensor, and display it in a graph control on a form. The program works fine, and parses the temp data frame a treat. However..... the graph object doesn't refresh, and the whole point is to show the data over time. I thought that the chart1.DataBind command that I put in forced a refresh. I am using Visual Studio 2013 Express. Any thoughts as to what I am doing wrong very much appreciated.
// Start of program.
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 serial_port_with_events_attempt_4
{
public partial class Form1 : Form
{
string RxString;
int RxRead;
int i;
int RxDec1;
int RxDec2;
int RxDec3;
float RxFloat;
float RxFloat2;
string locnString;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM8";
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
textBox1.ReadOnly = false;
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.ReadOnly = true;
}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxRead = serialPort1.ReadByte();
this.Invoke(new EventHandler(DisplayText));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void DisplayText(object sender, EventArgs e)
{
if (RxRead == 126)
{
textBox1.AppendText(Environment.NewLine);
i = 0;
}
if (i< 23)
{
if (i == 7)
{
if (RxRead == 51) // 51 in this position means that the temp sensor is the one in the wooden box
{
textBox1.AppendText("Temperature in Nick's office = ");
locnString = ("Nick's office");
}
}
if (i == 17)
{
RxDec1 = RxRead - 48; // Read the tens unit
}
if (i == 18)
{
RxDec2 = RxRead - 48; // Read the units
}
if (i == 20)
{
RxDec3 = RxRead - 49; // read the decimal
}
if (i == 22)
{
RxFloat = ((RxDec1 * 10) + RxDec2);
RxFloat2 = RxDec3;
RxFloat2 = RxFloat2 / 10;
RxFloat = RxFloat + RxFloat2;
RxString = RxFloat.ToString();
if (RxFloat < 30 && RxFloat >20)
{
// Put the value in the main text box if it is not corrupt, (checking if the range is reasonable
textBox1.AppendText(RxString); // Frig about to get the reads in the right format and added together
// Add a new line into the temperature database
temperature1DataSetTableAdapters.Temp1TableAdapter temp1tableadapter = new temperature1DataSetTableAdapters.Temp1TableAdapter();
temp1tableadapter.Insert(DateTime.Now, RxFloat, locnString);
}
// Delete any old data.
temperature1DataSetTableAdapters.TempTableAdapter temp2tableadapter = new temperature1DataSetTableAdapters.TempTableAdapter();
temp2tableadapter.DeleteTempQuery();
// The above two lines work, but I need to amend to select on date TODO
chart1.DataBind();
}
}
i=i+1;
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'temperature1DataSet.Temp' table. You can move, or remove it, as needed.
this.tempTableAdapter.Fill(this.temperature1DataSet.Temp);
}
}
}
Cheers,
Nick James

How do I display a shaded area in a Winform .Net 4.0 Chart Rangebar (C#)

I am displaying a Winform .Net Chart rangebar graph showing tickets worked on during the day for an employee. I would like the shade the area from 08:00 to 17:00 light green if I could. I tried to put a range series on, but it won't allow that. I think I could use the postpaint method to do it, but I can't figure out how to find the positions to draw a rectangle.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.Objects;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Windows.Forms.DataVisualization.Charting;
namespace CWHelper
{
public partial class DailyTimeEntryDisplay : CWHelper.BaseForm
{
Object dataLock = new object();
public DailyTimeEntryDisplay(string member_ID)
{
InitializeComponent();
mMember_ID = member_ID;
}
string mMember_ID;
private void DailyTimeEntryDisplay_Load(object sender, EventArgs e)
{
DateTime minDate = new DateTime(1900, 1, 1);
chart1.ChartAreas[0].AxisY.Minimum = minDate.ToOADate();
chart1.ChartAreas[0].AxisY.Maximum = minDate.AddDays(1).ToOADate();
chart1.ChartAreas[0].AxisY.Interval = 1;
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "HH:mm";
chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 1;
chart1.ChartAreas[0].AxisY.LabelStyle.Angle = 45;
chart1.ChartAreas[0].AxisY.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours;
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.RangeBar;
chart1.Series[0].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
chart1.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
double rangeStart = new DateTime(1900,1,1,8,0,0).ToOADate();
double rangeEnd = new DateTime(1900,1,1,17,0,0).ToOADate();
double[] range = new double[] {rangeStart,rangeEnd};
DisplayData();
}
private void DisplayData()
{
try
{
DateTime sd = dateTimePicker1.Value;
DateTime? dt = new DateTime(sd.Year, sd.Month, sd.Day);
cwwebapp_drsEntities dc = new cwwebapp_drsEntities();
var q = dc.GetTimeEntry(mMember_ID, dt);
var ql = q.ToList();
var chartData = (from x in ql
select new { SR_Service_RecID = x.SR_Service_RecID != 0 ? x.SR_Service_RecID.ToString() : "Time Entry:",
Time_Start = x.Time_Start.ToOADate(),
Time_End = x.Time_End.ToOADate(),
tooltip = x.Company_Name + " : " + x.Notes,
summary = x.Summary
}).ToList();
chart1.Series[0].Points.DataBind(chartData, "SR_Service_RecID", "Time_Start,Time_End", "Tooltip=tooltip,Label=summary");
}
catch (Exception ex)
{
Debug.Print(ex.Message);
if (ex.InnerException != null)
{
Debug.Print(ex.InnerException.Message);
}
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DisplayData();
}
private void LeftButton_Click(object sender, EventArgs e)
{
dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-1);
}
private void RightButton_Click(object sender, EventArgs e)
{
dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1);
}
private void ReloadButton_Click(object sender, EventArgs e)
{
DisplayData();
}
}
}
IMO, you don't need any PostPaint method. .Net Charting allows you to do that. Please try the following code:
private void DailyTimeEntryDisplay_Load(object sender, EventArgs e)
{
chart1.Palette = ChartColorPalette.None;
chart1.PaletteCustomColors = new Color[] { Color.LightGreen };
//DateTime minDate = new DateTime(1900, 1, 1);
//chart1.ChartAreas[0].AxisY.Minimum = minDate.ToOADate();
//chart1.ChartAreas[0].AxisY.Maximum = minDate.AddDays(1).ToOADate();
chart1.ChartAreas[0].AxisY.Interval = 1;
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "HH:mm";
chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 1;
chart1.ChartAreas[0].AxisY.LabelStyle.Angle = 45;
chart1.ChartAreas[0].AxisY.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours;
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Auto;
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.RangeColumn; // "RangeColumn" instead of "RangeBar"
chart1.Series[0].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
chart1.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time; // "Time" instead of "DateTime"
DisplayData();
}

code improvement - naming textboxes generated form numericupdown & button click

I have a problem with a code that is regarding adding controls with numericUpDown ( for example- if numericUpDown value equals to 3, user recievs 3 textboxes).
Thanks to stackoverflow users I improved my code.
Before improvement it looked like this:
private void button1_Click(object sender, EventArgs e)
{
if (numericUpDown1.Value == 1)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
}
else if (numericUpDown1.Value == 2)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
txtbx2.AutoSize = true;
Controls.Add(txtbx2);
txtbx2.Location = new Point(70, 130);
}
else if (numericUpDown1.Value == 3)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
txtbx2.AutoSize = true;
Controls.Add(txtbx2);
txtbx2.Location = new Point(70, 130);
txtx3.AutoSize = true;
Controls.Add(txtbx3);
txtbx3.Location = new Point(70, 160);
}
}
After improvement:
private void button1_Click(object sender, EventArgs e)
{
int y = 100;
int x = 70;
for (int i = 0; i < numericUpDown1.Value; i++)
{
var txtbx = new TextBox();
txtbx.AutoSize = true;
Controls.Add(txtbx);
txtbx.Location = new Point(x, y);
// Increase the y-position for next textbox.
y += 30;
}
}
Now the problem is that I don't know how assign names to genarated textboxes.
(before the improvement I could name them - txtbx1, txtbx2, txtbx3...)
Code to improve:
private void button3_Click(object sender, EventArgs e)
{
try
{
double a, b, c, sum;
a = double.Parse(txtbx1.Text);
b = double.Parse(txtbx2.Text);
c = double.Parse(txtbx3.Text);
sum = a + b + c;
label1.Text = sum.ToString();
}
catch (Exception ex)
{
MessageBox.Show("error");
}
Please note that I'm a beginner, learning c# by watching youtube tutorials ;) I do realize that my question might be silly but I couldn't handle this problem by myself.
In advance thank you for your time and help.
If you need to access them afterwards, you have some options.
I'll guess that your objective is to set label1's text to the sum of the values contained in the specified textbox(es).
On the ValueChanged event of your NumericUpDown, check the delta and consequentely add or remove the required number of TextBoxes to your Form's Controls. To obtain the delta, you'll need to store the previous value of the NumericUpDown, and then subtract it from the current value. (If it was 5, and now it's 4, 4 - 5 = -1. A textbox has been removed).
private int _oldNUDvalue = 0; //or assign it to the default value
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int delta = (int)numericUpDown1.Value - _oldNUDvalue;
if (delta < 0)
{
for (int i = -delta; i > 0; i--)
{
var tbox = Controls.Find("ntextBox" + (_oldNUDvalue - i), false)[0];
Controls.Remove(tbox);
}
}
else if (delta > 0)
{
for (int i = _oldNUDvalue; i < _oldNUDvalue + delta; i++)
{
var tbox = new TextBox();
tbox.Location = new Point(15, 55 + (30 * i));
tbox.Name = "ntextBox" + i;
Controls.Add(tbox);
}
}
_oldNUDvalue = (int)numericUpDown1.Value;
}
If, however, you only have a maximum number of 3, you could take a slightly different approach. My solution works with n-textboxes.
Finally, to get the TextBoxes' values from code, you have three approaches:
Loop through your Form's controls, check for TextBoxes with their name starting with "ntextBox", and add their values together
Use LINQ to do the same
Access them singularly via "Controls.Find("ntextBoxX", false)", where X is the number of course.
I'll show the LINQ approach as I like it better.
int sum = Controls.Cast<Control>().Sum(c => c.Name.StartsWith("ntextBox") ? int.Parse(c.Text) : 0);
I haven't tested the code, but it should work. Tell me if there are any problems.
EDIT: Tested and it works. For the sake of completeness, I'll add some event logic to the TextBox-adding loop, to make sure that their input is actually numeric.
tbox.TextChanged += HandleNTextBoxInput; // add this line
And elsewhere, add this method:
void HandleNTextBoxInput(object sender, EventArgs e)
{
string text = ((TextBox)sender).Text;
if (!Regex.IsMatch(text, "^[1-9][0-9]*$")) //Text is NOT numeric. Remove [1-9] if you want to keep leading zeros.
{
//Set the Text to 0, for example. Or show a message box. Or whatever.
((TextBox)sender).Text = "0";
}
}
As I mentioned in a comment- this code seems to be maybe too advanced for me.
I have no problem with adding the controls, bu still there is a problem how to get the sum from a button click to a textbox.
I probably made some simple mistakes, or something is missing but I really don't know how to fix this problem.
My code:
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 System.Text.RegularExpressions;
namespace testprogram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) // I'm guessing something is missing over here
{
int sum = Controls.Cast<Control>().Sum(c => c.Name.StartsWith("ntextBox") ? int.Parse(c.Text) : 0);
}
void HandleNTextBoxInput(object sender, EventArgs e)
{
string text = ((TextBox)sender).Text;
if (!Regex.IsMatch(text, "^[1-9][0-9]*$")) //Text is NOT numeric. Remove [1-9] if you want to keep leading zeros.
{
//Set the Text to 0, for example. Or show a message box. Or whatever.
((TextBox)sender).Text = "0";
}
}
private int _oldNUDvalue = 0; //or assign it to the default value
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
{
int delta = (int)numericUpDown1.Value - _oldNUDvalue;
if (delta < 0)
{
for (int i = -delta; i > 0; i--)
{
var tbox = Controls.Find("ntextBox" + (_oldNUDvalue - i), false)[0];
Controls.Remove(tbox);
}
}
else if (delta > 0)
{
for (int i = _oldNUDvalue; i < _oldNUDvalue + delta; i++)
{
var tbox = new TextBox();
tbox.Location = new Point(15, 55 + (30 * i));
tbox.Name = "ntextBox" + i;
tbox.TextChanged += HandleNTextBoxInput;
Controls.Add(tbox);
}
}
_oldNUDvalue = (int)numericUpDown1.Value;
}
}
}
}

Categories

Resources