This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
xBlizzDevious's profile picture

College/Uni project in C# - Any help?

Started by xBlizzDevious, 24 February 2014 - 04:50 PM
xBlizzDevious #1
Posted 24 February 2014 - 05:50 PM
Hey guys,

I'm doing a project in my college class (technically university) and was wondering if you guys here know C# and would be able to help with issues I get as I'm still a bit of a noob programmer and some of you have been really helpful with my ComputerCraft programmin issues.

So yeah, haven't got any issues yet, but we're not very far into the module. I'm sure we'll get many issues soon enough! I always do when I'm programming.
Engineer #2
Posted 24 February 2014 - 06:11 PM
Use stackoverflow.com and Google if nobody who knows c# replies on this topic.
I'm not a C# decv :P/>
xBlizzDevious #3
Posted 24 February 2014 - 06:50 PM
Use stackoverflow.com and Google if nobody who knows c# replies on this topic.
I'm not a C# decv :P/>

We use stackoverflow and Google a lot, but it's not the same as personally asking a question on a forum.


EDIT:
OK, well we've surprised ourselves with how well we're doing so far (we've been programming for maybe an hour or two), but we've come across two issues…

Does anyone know how we could check what key is pressed when it is pressed so that we can run code on the letter that the user has pressed?

Also, how does C# work in respect to navigating directories? IE. If we have the program in a folder and we want the program to access images, where would be best suited to store the files in relation to the program and how would we access the images from within the program? Using
picArray = "D:\aFolder\project\images\fileName";
works, but is not suitable for moving around. Placing it in the project's folder and using
picArray = "\project\images\file";
or
picArray = "\images\file";
doesn't work either. How would we go about getting to the right location with that?
Edited on 24 February 2014 - 06:22 PM
Bomb Bloke #4
Posted 24 February 2014 - 09:10 PM
Disclaimer: I know little about C (I couldn't even get "Hello World" to compile and so had to skip it). I've not done anything in C# at all.

It appears Console.ReadKey() is what you're after for keyboard input. It should be fairly similar to doing os.pullEvent("char") in Lua.

You're probably already aware that ".." stands for "parent directory", but it's also worth knowing that "." stands for "current directory". Hence you can probably get away with:

picArray = ".\images\file";

This doesn't work in all languages (eg Lua), so you might have to concatenate with something like Directory.GetCurrentDirectory():

picArray = Directory.GetCurrentDirectory() + "\images\file";
xBlizzDevious #5
Posted 25 February 2014 - 05:26 AM
Disclaimer: I know little about C (I couldn't even get "Hello World" to compile and so had to skip it). I've not done anything in C# at all. It appears Console.ReadKey() is what you're after for keyboard input. It should be fairly similar to doing os.pullEvent("char") in Lua. You're probably already aware that ".." stands for "parent directory", but it's also worth knowing that "." stands for "current directory". Hence you can probably get away with:
picArray = ".\images\file";
This doesn't work in all languages (eg Lua), so you might have to concatenate with something like Directory.GetCurrentDirectory():
picArray = Directory.GetCurrentDirectory() + "\images\file";

Ah, thanks. It seems that when using the test button in Visual Studio, it runs in a bin folder then a debug folder which I found out by printing to the screen the Directory.GetCurrentDirectory() function. So for now, we've changed the directory to "../../images/fileName" and that works.

Just testing the readKey thing now.

EDIT: Console.ReadKey doesn't and won't work in our situation as we're using WinForms and not a console. Don't worry though, we've found an alternative.
Edited on 25 February 2014 - 04:52 AM
Csstform #6
Posted 25 February 2014 - 07:51 AM
I probably know less C# than you, but the way you have ended up doing resources is the way that I used them. I haven't touched the language in a while, however.
TechMasterGeneral #7
Posted 25 February 2014 - 08:38 AM
I know a small bit of C#(how to do messageboxes and input from buttons and text fields) thats about it…
xBlizzDevious #8
Posted 25 February 2014 - 09:31 AM
I probably know less C# than you, but the way you have ended up doing resources is the way that I used them. I haven't touched the language in a while, however.
I know a small bit of C#(how to do messageboxes and input from buttons and text fields) thats about it…

Looks like we've got most of the basics sorted already. What we're struggling with now is getting the keys we press into a text box.

Right; a little background:
What we're making is a simple game to help people to type. Search "Typing game" in Google and you'll find hundreds of games that are something along the lines of what we're trying to make. However, we're having trouble reading letters into labels that are all the same name with a different number at the end. Here's the code I have for when you press a key and it executes up until the line shown:


public void Form1_KeyPress(object sender, KeyPressEventArgs e)
//The commented MessageBox.Show() lines are all debugging lines that I've left in for now
		{
			//MessageBox.Show("You've reached the KeyPressEvent function");
			for (int i = 0; i < wordArray.Length; i++)
			{
				//MessageBox.Show("You've reached the for loop within me: " + i);
				if (e.KeyChar.ToString().ToUpper().Equals(wordArray[i].ToString()))
				{
					//MessageBox.Show(e.KeyChar.ToString().ToUpper());
					Label letter = this.Controls.Find("letter" + i, true).FirstOrDefault() as Label; //Code breaks on this line but there's no error in Syntax.**
					letter.Text = e.KeyChar.ToString().ToUpper();
				}
			}
		}

** The form has 4 labels labelled "letter" then 1-4. Ie. letter1, etc.


Any ideas what's causing the issue and how to fix it?

EDIT: Don't worry guys! Made it work. The text boxes are labelled letter1 to 4 as stated, but i starts on 0 and goes up to 3.
Edited on 25 February 2014 - 09:12 AM