some (n00b) questions  
Author Message
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

ok sorry if it are stupid questions :( but atm theyre important to me :P

1) how do i create random numbers

2) im making a game atm and with the random numbers i want that everytime the number in the label changes(with a timer), there is a picture in the picturebox(so for example when nr1 is in the label i want a pict of a dog in the pictbox, when nr2 of a cat) without using whole time if label1.text is 1 then dog pict appears, if label1.text is 2 then cat pict appears...

3) is there maybe also some nice learning guide or something for C# (not the video one, i want to learn it at my own speed ;) (yes the video one goes to slow))

thx in advance



Visual Studio Express Editions5  
 
 
ahmedilyas





PostPosted: Visual C# Express Edition, some (n00b) questions Top

to create Random numbers, you would use the Random class. Example:

Random theRandomGen = new Random();

int theRandomNumberChosen = theRandomGen.Next(min, max)

if you want to change the picture in the picturebox on a timer then simply implement the tick/timeelapsed event of the timer (double click the timer) and pretty much change the picture there, of course setting your interval of the timer as required.



 
 
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

Random theRandomGen = new Random();

int theRandomNumberChosen = theRandomGen.Next(1, 100);

i got this in timer1_tick atm

but i wanted the number in the label each time the timer ticks so how can i put theRandomNumberChosen into a label

and for the picture, i didnt understand your answer or u didnt understand my question i dont know, but your answer didnt rly help me :< so what i want is: we got a ticking random number in the label, each time there is a new number(between 1 and 100) the picture in the picturebox changes and every number has a picture to it so for example nr 1 has a dog, 2 a cat, 3a horse etc etc how can this be coded easily (not typing if label.text is 1 dog appears, label.text is 2 cat appears, label.text is 3 horse appears, doing this 100 times would be stupid isnt it )


 
 
ahmedilyas





PostPosted: Visual C# Express Edition, some (n00b) questions Top

to put the value of the random number chosen to the label:

this.theLabel.Text = theRandomNumberChosen.ToString()

as for the picture well, you just simply load the picture you want based on the number, that is, if you have a picture filename of a number, example:

this.thePictureBox.Image = Image.FromFile(theRandomNumberChosen.ToString() + ".jpg");

this would load a picture on the picturebox assuming we have a file called, say the number chosen was 2, "2.jpg"

does this help



 
 
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

i'll test it out immediatly ^^ but i think thats what im looking for

some new little question (:P)

how do i 'int MyInt = 0;' (for example) for whole the form (so it can be used in all private voids)


 
 
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

It was a bit stupid of me but from where does it get the picture files It said he couldn't find the files xD so do how can i 'show' where that he need to get the picture files
 
 
ahmedilyas





PostPosted: Visual C# Express Edition, some (n00b) questions Top

well that was just an example, of course you would modify that.

so from what I understand, please do correct me, you wish to display a picture based on the number randomly generated

if so, create the pictures with the filenames which is a number (1.jpg, 2.jpg, 3.jpg etc.....) and place them in the same directory as the application, in this case the bin folder in your project folder, then it would load the image based on the number chosen and assuming the image exists.

It's best practice overall to make sure that the file exists before doing anything with the handling of a file, using the System.IO.File.Exists() class/method



 
 
ahmedilyas





PostPosted: Visual C# Express Edition, some (n00b) questions Top

you would need to declare it as a global variable in the application, so place the decleration at the top of the class, just after your "Public class theClass":

..

..

Public class myClass

{

private int theInt = 0;

..

..



 
 
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

i copyed those numbered files into my bin folder and it didnt help anything :( still the same filenotfound error
 
 
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

well i tryed to find that too, but i can only find a public partial class and when i put it under that i get a lot of errors
 
 
ahmedilyas





PostPosted: Visual C# Express Edition, some (n00b) questions Top

should be in the bin\debug folder.

to find out where your application is running from within the application (generally speaking) use the Application.StartupPath to find out the current directory the application is running from but within the IDE/VS, it will be in your project folder\bin\debug folder



 
 
ahmedilyas





PostPosted: Visual C# Express Edition, some (n00b) questions Top

should be:

 

public partial class myClass

{

   private int theInt = 0;

..

..

}

 

what errors do you get I believe you need to read more about global variables and scope:

some examples:

http://www.functionx.com/csharp/Lesson08.htm

http://www.dotnet247.com/247reference/msgs/12/62458.aspx

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=303362&SiteID=1

 



 
 
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

the thing with the variable all over the form is done :)

it was because im quite new to C# ^^

and for the errors was that for the variable all over the form


 
 
RubenPieters





PostPosted: Visual C# Express Edition, some (n00b) questions Top

should be in the bin\debug folder.

to find out where your application is running from within the application (generally speaking) use the Application.StartupPath to find out the current directory the application is running from but within the IDE/VS, it will be in your project folder\bin\debug folder

putted the files in the debug folder and it worked, thx alot m8 :)