I want to make my own calendar with C#.
Because of that i want to create as much panels as the month has days.
I tried to do it with a for loop, but it doesn't work.
I think the problem is, that the name of the object never changes.
But how can I change the name of the object every time it loops through?
public void createPanel()
{
if (DateTime.Now.Month == 1 || DateTime.Now.Month == 3 || DateTime.Now.Month == 5 || DateTime.Now.Month == 7 || DateTime.Now.Month == 8 || DateTime.Now.Month == 10 || DateTime.Now.Month == 12)
{
for (int i = 0; i == 31; i++)
{
int locationX = 12;
int locationY = 74;
//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 == 7 || i == 14 || i == 21)
{
locationY += 106;
locationX = 12;
}
locationX += 206;
}
}
for (int i = 0; i == 31; i++)
is wrong. You will never reach the inside of the for loop, since i is equal to 0 and expected to be 31.
Maybe you meant:
for (int i = 0; i <= 31; i++)
I'm assuming there's lots of other issues with your code, but fix this first and maybe open new and more specific questions.
Instead of hard coding the months in your if statement, you might want to use this instead:
DateTime.DaysInMonth(year, month);
So your loop should look something like:
var numDays = DateTime.DaysInMonth(selectedYear, selectedMonth);
for (int i = 0; i < numDays; i++)
{
// your code here
}
You can get the selected year and month from DateTime.Today, or just let the user select it for themselves.
Related
How do i use operands in this code? What can i do to resolve this problem? Any suggestions or links to tutorials would be appreciated.
Operator '%' cannot be applied to operands of type 'string' 'int'
int i = 0;
double[] arr1 = new double[20];
for (i = 0; i < 20; i++)
{
Console.Write("Enter a number (0=stop): ");
var year = Console.ReadLine();
if (year == "0") break;
arr1[i] = int.Parse(year);
while (year != 0)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
Console.WriteLine($"{year} is a leap year.");
}
else if (year < 0)
{
Console.WriteLine($"Year must be positive!");
}
else
{
Console.WriteLine($"{year} is not a leap year.");
}`
You are close. You are already parsing the string to an int. Just use that instead of the string year when doing your calculations. Also, I'm not sure what you're trying to do with that while loop but I don't think you need it. It seems to just cause your program to go in an infinite loop because while is evaluating year but there is no opportunity to change the year value within the while loop.
void Main()
{
int i = 0;
double[] arr1 = new double[20];
for (i = 0; i < 20; i++)
{
Console.Write("Enter a number (0=stop): ");
var line = Console.ReadLine();
int numYear = int.Parse(line);
arr1[i] = numYear;
string message = "" ;
if (line != "0")
{
if (numYear < 0)
{
message = "Year must be positive!";
}
else if ((numYear % 4 == 0) && (numYear % 100 != 0)) || (numYear % 400 == 0))
{
message = $"{numYear} is a leap year.");
}
else
{
message = $"{numYear} is not a leap year.");
}
Console.WriteLine(message);
}
}
}
I am currently having problems with this loop. It becomes an infinite loop and i get a stack overflow error. This is for a interest rate trade swap application. i is the length of the trade and l is the increasing index.
private void button1_Click(object sender, EventArgs e)
{
int outp = 0;
int i = int.Parse(tradeLength.Text);
string month = "January";
for (int l = 1; l <= i; l++)
{
Console.WriteLine("I iterated " + l + " Amount of times");
if (l == 1)
{
month = "January";
}
if (l == 2)
{
month = "February";
}
if (l == 3)
{
month = "March";
}
if (l == 4)
{
month = "Aprll";
}
if (l == 5)
{
month = "May";
}
if (l == 6)
{
month = "June";
}
if (l == 7)
{
month = "July";
}
if (l == 8)
{
month = "August";
}
if (l == 9)
{
month = "September";
}
if (l == 10)
{
month = "October";
}
if (l == 11)
{
month = "November";
}
if (l == 12)
{
month = "December";
}
else
{
month = "Null";
l = 1;
}
The cause is the final else:
if (l == 12) {
month = "December";
}
else { // <- if l != 12 (e.g. l == 1) restart the loop
month = "Null";
l = 1;
}
you want else if:
if (l == 1)
{
month = "January";
}
else if (l == 2)
{
...
}
...
else if (l == 12)
{
...
}
else {
month = "Null";
l = 1;
}
Edit: Another problem (see FKEinternet's comment) is a user input: if i is greater than 12 l never reaches it. You have to either validate the user input:
int i = int.Parse(tradeLength.Text);
if (i > 12)
i = 12; // or ask for other value
or use modular arithmetics:
for (int index = 1; index <= i; index++) {
int l = index % 12 + 1;
if (l == 1)
{
month = "January";
}
else if (l == 2)
...
else if (l == 12)
...
else
{
month = "Null";
l = 1;
}
}
It is not a very good idea to set the loop variable inside the loop. Like #stuartd pointed out, in your else line you set the loop variable to 1 and causing the loop to start all over again. Remove the l=1 line in your else block.
I presume you want to go to next year when i > 12. The way your code is made, when this happens, you get to loop forever, because "l" never reaches a number bigger than 12, it becomes 1 when it hits 13 and starts over.
To fix this, instead of
if (l == 1)
you want to use
if ((l % 12) == 1)
so your entire loop would be like this:
for (int l = 1; l <= i; l++)
{
Console.WriteLine("I iterated " + l + " Amount of times");
if ((l % 12) == 1)
{
month = "January";
}
if ((l % 12) == 2)
{
month = "February";
}
if ((l % 12) == 3)
{
month = "March";
}
if ((l % 12) == 4)
{
month = "Aprll";
}
if ((l % 12) == 5)
{
month = "May";
}
if ((l % 12) == 6)
{
month = "June";
}
if ((l % 12) == 7)
{
month = "July";
}
if ((l % 12) == 8)
{
month = "August";
}
if ((l % 12) == 9)
{
month = "September";
}
if ((l % 12) == 10)
{
month = "October";
}
if ((l % 12) == 11)
{
month = "November";
}
if ((l % 12) == 0)
{
month = "December";
}
{
PS = this is really not the right way to do this, I'm just using your own code and making the least amount of mods for it to work as intented. Good luck!
1/ DateTime before = DateTime.Now;
2/ shellSort(List1);
3/ DateTime after = DateTime.Now;
4/ Console.WriteLine(after - before);
5/
6/ before = DateTime.Now;
7/ insertionSort(List2);
8/ after = DateTime.Now;
9/ Console.WriteLine(after - before);
I am trying to compare the run time of two different sorting algorithms. List1 here is equal to List2. I was expecting shell sort to be faster than insertion sort but although first WriteLine differs, it usually prints something like this = 00:00:00.0035037. The second one however, either prints 00:00:00 or something smaller than the first print. I thought maybe the insertion sort was better suited for List's current state however even when i swap the line 7 and line 2 i still get the same result. What is causing this? Why is the second executed function runs faster? Or am i using the Dates completely wrong?
Edit : I used Stopwatch instead of DateTime Class as advised in another post. The result is pretty much the same. The second one usually runs faster but every now and then it's slower than the first one. I also used a pre-written shellsort code to see if my implementation was bad but that was also a dead end.
As requested, shellsort and insertionsort implementations
static void shellSort(List<int> numbers) // Implementation i found online
{
int i, j, increment, temp;
increment = 3;
while (increment > 0)
{
for (i = 0; i < numbers.Count ; i++)
{
j = i;
temp = numbers[i];
while ((j >= increment) && (numbers[j - increment] > temp))
{
numbers[j] = numbers[j - increment];
j = j - increment;
}
numbers[j] = temp;
}
if (increment / 2 != 0)
increment = increment / 2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}
public static void insertionSort(List<int> numbers)
{
int i = 0;
while (i != numbers.Count)
{
int k = i;
while (k != 0 && numbers[k] < numbers[k - 1])
{
int temp = numbers[k - 1];
numbers[k - 1] = numbers[k];
numbers[k] = temp;
k--;
}
i++;
}
}
Also this was my implementation of shellsort
public static void shellSort(List<int> Liste)
{
int n = Liste.Count;
int gap = (Liste.Count - 1) / 2;
while (gap > 0)
{
int i = 0;
for(int k = gap; k < n; k++) {
int p = i;
int m = k;
while (p >= 0)
{
if (Liste[p] > Liste[m])
{
int temp = Liste[p];
Liste[p] = Liste[m];
Liste[m] = temp;
m = p;
}
else
break;
p = p - gap;
}
i++;
}
gap = gap / 2;
}
}
Hy guys, I'm trying to delete a Line previously created by button pressed. I also want the line to be deleted by button pressed. There are a large number of lines on my canvas, so I've tried selecting it by name to delete it but I failed. Also I tried by Uid and did not work.
Here is the code for the line created:
private void butonAdaugare_Click(object sender, RoutedEventArgs e)
{
Linie1.Y2 = Linie1.Y2 + 21;
Linie2.Y2 = Linie2.Y2 + 21;
Linie3.Y2 = Linie3.Y2 + 21;
Linie4.Y2 = Linie4.Y2 + 21;
Line linienoua = new Line();
linienoua.Uid = "LinieNoua" + i;
linienoua.X1 = 10;
linienoua.Y1 = Linie4.Y2;
linienoua.X2 = 670;
linienoua.Y2 = Linie4.Y2;
linienoua.Visibility = System.Windows.Visibility.Visible;
linienoua.Stroke = System.Windows.Media.Brushes.Black;
linienoua.StrokeThickness = 1;
canvas1.Children.Add(linienoua);
i++;
foreach (Line l in canvas1.Children.OfType<Line>())
{
for (int j = 2; j < 15; j++)
{
if (l.Name == "V" + j)
l.Y2 = l.Y2 + 21;
}
}
}
And this is the removal part by Uid:
private void butonStergere_Click(object sender, RoutedEventArgs e)
{
if (Linie1.Y2 > 101 && Linie2.Y2 > 101 && Linie3.Y2 > 101 && Linie4.Y2 > 101)
{
Linie1.Y2 = Linie1.Y2 - 21;
Linie2.Y2 = Linie2.Y2 - 21;
Linie3.Y2 = Linie3.Y2 - 21;
Linie4.Y2 = Linie4.Y2 - 21;
}
foreach (Line l in canvas1.Children.OfType<Line>())
{
if (l.Uid == "LinieNoua" + i)
canvas1.Children.Remove(l);
for (int j = 2; j < 15; j++)
{
if (l.Name == "V" + j && l.Y2 > 91)
l.Y2 = l.Y2 - 21;
}
}
i--;
}
Any thoughts on this ?
Without a good, minimal, complete code example, it is hard to provide an answer with a high level of confidence. You have left too much information out of your question to know for sure what the problem is.
However, from the code you did post, there does seem to be one obvious problem: the value of i is being decremented only after the code to remove the line, but it seems that variable contains the index value for the next line to be added, not the most recently-added one.
It seems likely to me that if you simply move the decrement to occur prior to the loop that finds and removes the object, it would work:
private void butonStergere_Click(object sender, RoutedEventArgs e)
{
if (Linie1.Y2 > 101 && Linie2.Y2 > 101 && Linie3.Y2 > 101 && Linie4.Y2 > 101)
{
Linie1.Y2 = Linie1.Y2 - 21;
Linie2.Y2 = Linie2.Y2 - 21;
Linie3.Y2 = Linie3.Y2 - 21;
Linie4.Y2 = Linie4.Y2 - 21;
}
i--;
Line lineToRemove = null;
foreach (Line l in canvas1.Children.OfType<Line>())
{
if (l.Uid == "LinieNoua" + i)
{
lineToRemove = l;
}
for (int j = 2; j < 15; j++)
{
if (l.Name == "V" + j && l.Y2 > 91)
l.Y2 = l.Y2 - 21;
}
}
if (lineToRemove != null)
{
canvas1.Children.Remove(lineToRemove);
}
}
<edit>
From your description in the comments, it sounds like when you made my originally suggested change, the code simply crashed (i.e. "the program exits directly", as you put it). Since you didn't provide a complete code example, there is no way for me to actually test any proposed solution, but I strongly suspect the crash you are seeing is due to the fact that the code attempts to modify (remove an element from) the canvas1.Children collection, which is the same collection that is being enumerated.
I have edited the proposed code change to avoid performing that illegal operation. Instead, the element to remove is simply noted in a local variable, and it is removed at the end, after the enumeration of all of the children has completed.
</edit>
For future reference, this seems like an excellent scenario for a debugger. If you were to simply set a breakpoint in the butonStergere_Click() method and step through each iteration of the loop, looking for the object you expect to be removed, it would be readily apparent when you find that object why the code itself wasn't identifying it.
Most bugs happen because we as a programmer have made an assumption about something that turns out to not be true. The best way to find bugs then is to use a debugger and examine the validity of each and every assumption we made, until we find the discrepancy.
I have a code here and I would like that it will display the first 10 and if I click on that, it will display again the second batch. I tried this first with my first for-code and it work now I'm working with arrays it seems it didn't accept it
The one I commented dont work? is this wrong?
Thanks
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i] = potenzen[i-1] * 2;
//if (potenzen % 10 == 0)
// Console.ReadLine();
}
foreach (long elem in potenzen)
{
Console.WriteLine(" " + elem);
}
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i]=potenzen[i-1]*2;
Console.WriteLine(potenzen[i-1]);
if (i % 10 == 0)
Console.ReadLine();
}
is more in line with what you want. An improvement would be to separate your data-manipulation logic from your data display logic.
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
potenzen[i]=potenzen[i-1]*2;
for (int i = 0; i < potenzen.Length; ++i)
{
Console.WriteLine(potenzen[i]);
if (i % 10 == 0)
Console.ReadLine();
}
Of course, you could do this without an array
long potenzen = 1;
for (int i = 1; i < 32; ++i)
{
Console.WriteLine(potenzen);
potenzen = potenzen * 2;
if (i % 10 == 0)
Console.ReadLine();
}
You need:
if (i % 10 == 0)
and not:
if (potenzen % 10 == 0)
Applying the modulus operator to an array of longs is dubious.
potenzen is an array so you maybe try
if (i % 10 == 0)
or maybe
if (potenzen[i] % 10 == 0)
You're taking an array mod 10 -- at best, in an unsafe language, you'd be doing the modulo operation on a memory address.
This should work fine if you just change the line to:
// if you don't want to pause the first time you run it, replace with:
// if (i > 0 && i % 10 == 0) {
if (i % 10 == 0) {
Console.ReadLine();
}
Try changing it to:
long [] potenzen = new long[32];
potenzen[0] = 1;
Console.WriteLine(potenzen[0]);
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i]=potenzen[i-1]*2;
Console.WriteLine(potenzen[i]);
if (i % 10 == 0)
{
var s = Console.ReadLine();
// break if s == some escape condition???
}
}
Right now, you're never printing, unless you completely finish your first for loop. My guess is that you're not allowing the full 32 elements to complete, so you're never seeing your results -
This will print them as they go.