For a university project I was tasked with creating a crossword game in windows forms using c#, I have 25 textboxes laid out in a grid and named with their indexes(e.g. txtCell00, txtCell01).
Image of the form layout with the textbox names and layout
To check the guesses I want to loop through all the cells and compare them to an array of the correct answers, but I don't know how to loop through all the cells without having 25 nested if statements. I wanted to know if there was a way to use string manipulation in the actual code
e.g.
for(int i = 0; i <= 4; i++)
{
for(int m = 0; m <= 4; m++){
if(txtCell[i][m].Text == ans[i][m])
{
}
}
}
I am probably being stupid and there is probably a better alternative so I would greatly appreciate any help you could give.
Related
I am learning the programming basics in C# and am studying the use of "for" function.
When working on an exercise I realised there's something wrong with my console - it seems as if it did not display top 6 rows.
I.e. with such a code:
for (int i = 1; i <= size; i++)
{
for (int j = 1; j <= size; j++)
{
Console.Write("{0,-3}", j);
}
Console.WriteLine();
}
Console.ReadKey();
My console spits out only smth like this:
[1
Dear masterminds, wwyd?
In order to increase the buffer size for the console as #Caius Jard suggested you need to press the right mouse button on the top console bar.
Press Properties
Change settings until you will see it will work for you. In the screenshot, you can find Windows default settings that I'm using.
You might need to re-run your program to notice some changes.
I just cant find a Solution to give out even Numbers without using the Modulo-Operator. I need to use a for-loop since its required by my School Assignment.
Something as Simple as Possible since im just a Beginner ._.
There are various ways to do this, but I don't know what modulo has to do with it.
For example:
for(int i = 0; i <= 100; i += 2)
{
// Only even numbers in i
}
im just started learning c# and i have this little thing.
i dont really know how to call it but here it goes:
there are like 5 textboxes, and i want to set the value of each textblock with a for loop.
they are called t_1, t_2, t_3 and so on.
for (int i=0; i<5; i++)`
{
("t_" + i).Text = i;`
}
this gives this error:
Error 1 'string' does not contain a definition for 'Text' and no
extension method 'Text' accepting a first argument of type 'string'
could be found (are you missing a using directive or an assembly
reference?)
while the textboxes do have .Text properties.. what am i missing?
(btw im using MS VSExpress 2012 for windowsphone)
thanks in advance :)
EDIT:
thank you all very much for all the effort! :D
you guys really helped me out, and im back on track learning! :D
I'm afraid that you can do that so directly. Create a list of your textboxes like
var list = new List<TextBox> { t_1, t_2, t_3, t_4, t_5 };
and then you will be able to do
for (int i=0; i<5; i++)
{
list[i].Text = i.ToString();
}
I also advise you to replace
for (int i=0; i<5; i++)
with
for (int i = 0; i < list.Count; i++)
so that you can just add new controls without having to change the loop.
EDIT:
Sudhakar answer is also a usable solution, but to clarify benefits of my, here are the facts:
using literals in code is a way to earn a nice bug ... imagine more devs working in the same solution and one of them changing a name of a component so that it better express its purpose
code readability - list[i].Text = i; versus reference boxing and Find("t_" + i,true)[0] ... a fairly significant difference in readability
The Textboxes are Properties of an overlying object, maybe a ASP.Net WebPage, XAML or WinForm.
To be able to itereate through them you have to look at that object first, and change its structure so that the Textboxes are in an enumerable list.
I would suggest a dictionary with string indexers, probably fits you most.
You can't call a variable like a value.
Use this:
for (int i=0; i<5; i++)
{
if (i==1) t_1.Text = i.tostring()
else if (i==2) t_2.Text = i.tostring()
... so on
}
May i ask if the following code i wrote is correct? Basically, i want to create an 2D array, and within each array, i want to have a list of vertices. So if tCount and pCount is 10, i would have 100 different lists storing vertices. The adding of the vertices will be done elsewhere after certain operations are done to determine which list the vertice should be added to.
List<Vertice>[,] lists = new List<Vertice>[tCount, pCount];
for (int i = 0; i < tCount; i++) {
for (int o = 0; o < pCount; o++) {
lists[i,o] = new List<Vertice>(); } }
Pardon me for posting such a simple question, because i have posted a similar qns in another forum and the replies i received kind of confused me. But thanks for reading!
Mitch Wheat- There was no error so far, but i am also not sure if the list is created properly..
Damith- I've seen that before but i do not really understand the code, so i was wondering if the code i typed was correct or not. I feel safer using a code that i understand, although i do understand the benefits of using a 1-liner.
Oberdan Nunes- I need it to be in a 2D array, as it is related to another 2D array of data which i have.
Okay, I have this piece of code:
int arrPanelsRequired = 4;
string airport;
int arrPanelsMade = 0;
for (; arrPanelsMade <= arrPanelsRequired; arrPanelsMade++)
{
Panel arrPanelXXX = new Panel();
and I need to replace the XXX with the value of arrPanelsMade. I found several ways to name an object after a variable's value, however, I wasn't able to apply any of them here :(
I don't think it would be a good idea in general, let alone in C#. I can see many arr prefixes, which leads me to the use of an array. I think you might be looking for something like this:
int arrPanelsRequired = 4;
Panel[] panels = new Panel[arrPanelsRequired];
for (int arrPanelsMade = 0; arrPanelsMade < arrPanelsRequired; arrPanelsMade++)
{
panels[arrPanelsMade] = new Panel();
}
What you are trying to do is bad practice. It passes in weakly-typed languages, but in strongly-typed languages such as C#, variables need to be explicitly declared. This helps the compiler to catch errors that can be caused by typos or general development mistakes. For example, if you have a variable arrPanel089, how can you/the compiler know if it has been declared? Strong typing can seem like a constraint when coming from weakly-typed languages, but it is really a great tool that saves you a ton of headache as the developer.
If you want to create a set of panels, store them in an array/list/etc and access them by their index. If you want to give them a key, store them in a dictionary which allows you to access values by key instead.
You should add the panels to a List or array, and access them later by index:
arrPanel = new Panel[arrPanelsRequired];
for( int idx = 0 ; idx < arrPanelsRequired ; idx++)
arrPanel[idx] = new Panel();
You have a couple of things going on here that raise red flags. I'll digress from your question just a bit, because I think you're probably going down the wrong road in a couple of respects.
Drop the "arr" prefix, and just call your variables what they are — don't try to emulate Hungarian Notation unless you've got good reason to do so.
Consider using a while loop instead of a for loop if you're not going to perform your initialization in the loop header.
Now on to your question. No, the language doesn't work like that. Use a List<Panel> class instead. That will allow you to add more panels dynamically without having to predefine how many you want, such as in an array.