4 Replies - 568 Views - Last Post: 20 April 2011 - 11:50 AM

#1 texastom87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 09

How do I read in a text file and dispay it in a menu option selected?

Posted 13 April 2011 - 08:52 PM

Ok, so I uploaded a .zip file of my project. I want to be able to display the contents of Story.txt in a menu after selecting Story from the main menu. It's hard to explain, but when you run the code, you'll know what I mean [I hope].

By the way, some basic controls for the menu are:
Enter - Select
Esc - Confirm if you want to quit
Up/Down Arrows - Move up and down

Made in Visual Studio 2010, C#, XNA

If you have any questions, I'll try to provide more in depth of what I mean.
Attached is a picture, so you can visualize what I'm trying to accomplish. I tried to toy with SpriteFont but had no success. Thanks in advance!

Attached File  Lab4Game.zip (6.65K)
Number of downloads: 45

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: How do I read in a text file and dispay it in a menu option selected?

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,462
  • Joined: 02-June 10

Re: How do I read in a text file and dispay it in a menu option selected?

Posted 14 April 2011 - 06:15 AM

Nobody is going to download and open your entire .zipped project. We just don't do that here.

Please follow the guidelines posted everywhere and post the code block that is relevant to your specific problem, along with an accurate description of the problem or error messages. We will try to help you troubleshoot your code.

There are links to read and write text file tutorials in my standard newbie response below.


Standard resources, references and suggestions for new programmers.

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

Free editions of Visual Studio 2010

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

C# resources. Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes

Working with environmental variables

Debugging tutorial
Debugging tips
Great debugging tips

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.


These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on

Was This Post Helpful? 0
  • +
  • -

#3 cyan1de  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 45
  • Joined: 12-February 09

Re: How do I read in a text file and dispay it in a menu option selected?

Posted 15 April 2011 - 08:47 AM

Add a new item to your project, select text file, create storyText.txt. Under its properties for the field 'Build Action' select 'Embedded Resource'. There is many ways to access the file, if you want to create an object (InputReader) to access it use this code in a new class.

using System;
using System.Reflection;
using System.IO;

namespace ProjectsNamespace {
    
    class InputReader{

        Assembly myAssembly = Assembly.GetExecutingAssembly(); //Get executing environment to locate txt file

        public String ReadFromFile(String fileLocation) //Pass to the object "storyText"
        {
            String fileAppend = "ProjectsNamespace.Resources." + fileLocation + ".txt";
            string line = "";

            using (StreamReader stream = new StreamReader((myAssembly.GetManifestResourceStream(fileAppend)))) //stream text file
            {
                while (!stream.EndOfStream) //Read stream till EOF
                    line += stream.ReadLine();         
            }
            return line;
        }
    }
}



This post has been edited by cyan1de: 15 April 2011 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 angrydwarfz  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 60
  • Joined: 07-December 08

Re: How do I read in a text file and dispay it in a menu option selected?

Posted 17 April 2011 - 02:11 AM

@cyan1de: you're forgetting that since he's using XNA, he has access to the content pipeline, which simplifies thing A LOT!

You will need to add the story to a simple XML content file and load it into the game into a string variable (which you can then draw as you wish). Just add a new file to your content project, and select the XML file option. A new asset file will be generated and opened immediately. It should look like this:


<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
//a random comment which you can remove should be here
  <Asset Type="System.String"></Asset>
</XnaContent>



Just add the text of your story between the <Asset> tags, and in-game do the following:

string story = content.Load<string>("nameOfXmlAssetFileWithNoExtension");


Was This Post Helpful? 2
  • +
  • -

#5 Kilorn  Icon User is offline

  • XNArchitect
  • member icon




Reputation: 1325
  • View blog
  • Posts: 3,504
  • Joined: 03-May 10

Re: How do I read in a text file and dispay it in a menu option selected?

Posted 20 April 2011 - 11:50 AM

Alternatively, another way, albeit inefficient, would be to just create numerous strings containing the information that would normally be in the file and then use the drawString method to draw it to the screen at the appropriate time. With that said, personally I'd go with the XML file as stated by angrydwarfz.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1