C# Setting image to multiple controls is slow - c#

I am trying to clone the game "Minesweeper" in c#.
When I create a field, I want the background of the button control to be the same as in the original game "Minesweeper".
However, when I create my field and I add all the images it takes at least 1-5 seconds to completely load the field.
Code for creating the field:
for (int i = 0; i < fieldsize; i++)
{
for (int j = 0; j < fieldsize; j++)
{
panel1.controls.add(new Button(){backgroundimage = properties.resources.field})
}
}
Any help is appreciated!

Related

Button won't change visibility as intended

I have a function that has a multidimentional array that holds n*n numbers, and a vector that holds my programs Buttons.
Right now, my program is able to shuffle the numbers in the array and assign each value to a button, "refreshing the values of each button" everytime I press the button.
I now have a problem when I've tried to hide a specific number (eg.0) each time I "refresh" the button, but it seems it doesn't make the previous button visible when I press the button, which leads to having all the buttons hidden eventually.
This is the code I have right now:
public void Refresh(int NumberToHide)
{
foreach (Button b in buttons)
b.Visibility = Visibility.Visible;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (buttons[(i * 4 + j)].Content.ToString() == NumberToHide.ToString())
{
buttons[(i * 4 + j)].Visibility = Visibility.Hidden;
}
else
{
buttons[(i * 4 + j)].Content = myArray[i, j].ToString();
}
}
}
}
Shouldn't the foreach at the start of the function make every button visible?
Why is it that the buttons that are not visible, don't become visible everytime I call the function?
Thanks for the help!
The problem seems to come from the fact that you either hide an element or assign a Content. Since your visibility check is based on that content, previously invisible elements don't change. I can't reproduce it since a lot of your code is missing, but based on your description I'd check for the new content instead of the current:
public void Refresh(int numberToHide)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
var button = buttons[i * 4 + j];
button.Content = myArray[i, j];
button.Visibility = myArray[i, j] == numberToHide ?
Visibility.Hidden : Visibility.Visible;
}
}
}

Changing the z-index (order) of shapes and connections of Telerik RadDiagram

I am using Telerik RadDiagram for building a diagram. The gray lines are over the shapes (requirements, development etc) in my diagram. Is there any way to put them behind the shapes?
// ... getting ready the shape objects ...
for (int i = 0, j = 0; i < monthCount + 1; i++, j = j + 2)
{
// ... getting ready the shape and connection objects ...
RadDiagram1.ShapesCollection.Add(shapeForLines[j]);
RadDiagram1.ShapesCollection.Add(shapeForLines[j + 1]);
RadDiagram1.ConnectionsCollection.Add(monthLine[i]);
RadDiagram1.ShapesCollection.Add(labelMonth[i]);
}
for (int i = 0; i < 7; i++)
{
if (i < 6) { RadDiagram1.ShapesCollection.Add(shape[i]); }
if (i < 7) { RadDiagram1.ShapesCollection.Add(label[i]); }
}
This worked for me: while the Default.aspx loads a Java-Script-Method is called and there the Connections are brought to back:
function diagram_load(sender) {
diagram = sender.get_kendoWidget();
//.....
diagram.toFront(diagram.Shape);
diagram.toBack(diagram.connections);
} // diagram_load(sender)
try to set ZIndex property while you create shapes. i set this in WinForms, maybe you can find it too

Unreachable Code Detected C#

Im trying to Post an unlimited number of likes but looping the cookies and proxies based on the how many cookies are stored in the array. Apparently i++ is unreachable code. What is the reason for that?
public void PostLikes()
{
PostLike postLike = new PostLike();
for (int i =0;i<this.cookies.ToArray().Length;i++)
{
for (int j = 0; ; j++)
{
postLike.PostLike(this.cookies[i], this.importedProxies[i], this.proxyUsernameTextbox, this.proxyPasswordTextbox, this.postsIds[j]);
}
}
}
The real problem is that:
for (int j = 0; ; j++)
Produces an infinite loop, assuming you don't have any other control statements inside (e.g. break, return, goto, throw)
You probably meant to do something like this:
for (int j = 0; j < this.postsIds.Length; j++)
{
...
}
And don't do this
for (int i =0;i<this.cookies.ToArray().Length;i++)
because
this.cookies.toArray().Length
its evaluating in every iteration of the for loop, you are making 'this.cookies' to array every time so just you get its length? :) You are increasing the complexity of the method
for (int j = 0; ; j++)
This is a dead loop, so i++ won't be reached

how to display values stored in multi dimenssional array?

I have created a multidimensional array.I wants to display it in the .aspx page. please help.my code is here.It doesnt shows the array values
string[,] array_questions = new string[dt.Rows.Count, dt.Columns.Count];
for (i = 0; i < dt.Rows.Count; i++)
{
for (j = 0; j < dt.Columns.Count; j++)
{
array_questions[i, j] = dt.Rows[i][j].ToString();
//TextBox1.Text = array_questions[i,j];
}
}
Console.Write(array_questions[i, j] + " ");
you can also use Response.write
Console.Write is used for console applications.
If you want to display the data on the aspx webpage you need to create a webcontrol (for example label) and set its text property with an appropriate value.
If you want to write the values ​​in the table in the console visual studio, call the method
Console.Out.Write() and not Console.Write().
Otherwise call Response.Write() to write on the page
// display the array
for (int i = 0; i < array_questions.GetLength(0); i++)
{
for (int j = 0; j < array_questions.GetLength(1); j++)
{
Response.Write("i="+i+" "+"j="+j);
Response.Write("<br>");
}
}

Flling a two-dimensional array with buttons

I have a litte problem here. I have a two-dimensional array which is [5,5] big.
I have a windows form with 25 buttons. Now I want to store the buttons in the object array, but my problem is, how do I tell the program to know which button to put in the array? Is it possible in some kind of this way:
//_array[i] = button(i);
This is my first time storing objects in an array and I dont know how to do that.
EDIT: The buttons have all the standard names (button1,button,button3...)
EDIT2: I know how to it by hand (_array[x,y] = button1) but I want to know how to it ith a for-loop.
You could filter the controls by using IEnumerable.OfType():
//get all buttons and order them by name
var buttons = Controls.OfType<Button>().OrderBy(x => x.Name).ToList();
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
_array[i, j] = buttons[i*5+j];
You can access the buttons using their name as index of the Controls collection:
for (int i = 0; i < 5; i++) {
for (int k = 0; k < 5; k++) {
_array[i, k] = Controls["Button" + (5 * i + k + 1).ToString()];
}
}
Try this:
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
_array[i,j] = Controls.Item["Button" + (i*5 +j).ToString()];

Categories