Computer Science Canada

Opening Files[C Sharp]

Author:  RealityFix [ Tue Nov 17, 2009 9:11 am ]
Post subject:  Opening Files[C Sharp]

What I basically want to do is open a file and make the program grab the files that are in the folder. Oh have some sort of way to use two files at once.

Im making a little program where a person makes a list of players and monsters and I hate using datafiles. I kinda suck at them >.>
So I just think it would be easier to use two seperate files then one file holding monsters and players.

Any tips? or Better ways to pull this off?

Author:  rdrake [ Tue Nov 17, 2009 10:23 am ]
Post subject:  RE:Opening Files[C Sharp]

Check out the following static method.

C#:
System.IO.Directory.GetDirectories(string dir)
.

Personally I'd look into serialization in order to store your settings and states. It's pretty easy.

Author:  RealityFix [ Wed Nov 18, 2009 5:27 pm ]
Post subject:  RE:Opening Files[C Sharp]

Any links to that serialization? I just want the easiest way possible at doing this >.>

Me = crappy at datafiles ;_;

Author:  jbking [ Wed Nov 18, 2009 5:41 pm ]
Post subject:  Re: Opening Files[C Sharp]

RealityFix @ Tue Nov 17, 2009 7:11 am wrote:
What I basically want to do is open a file and make the program grab the files that are in the folder. Oh have some sort of way to use two files at once.

Im making a little program where a person makes a list of players and monsters and I hate using datafiles. I kinda suck at them >.>
So I just think it would be easier to use two seperate files then one file holding monsters and players.

Any tips? or Better ways to pull this off?


Why not use a command line interface to input data? Under the command line interface you could ask how many players and monsters should be generated, name them and then build the lists from some simple input. You don't need no stinkin' no files, IOW.

Author:  rdrake [ Wed Nov 18, 2009 5:41 pm ]
Post subject:  RE:Opening Files[C Sharp]

http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file

Author:  RealityFix [ Thu Nov 19, 2009 1:47 pm ]
Post subject:  RE:Opening Files[C Sharp]

this program has to use datafiles. Its what the whole assignment is about. The user has to be able to save and open the files and such.

Thanks rdrake.

The site uses some functions we haven't been taught yet in C#.

Is there a slight possibility of a easier technique?

Author:  jbking [ Thu Nov 19, 2009 2:15 pm ]
Post subject:  Re: RE:Opening Files[C Sharp]

RealityFix @ Thu Nov 19, 2009 11:47 am wrote:
this program has to use datafiles. Its what the whole assignment is about. The user has to be able to save and open the files and such.


Ah, that wasn't stated in the initial post. Some people do these kinds of things for fun. Twisted Evil

Author:  RealityFix [ Sat Nov 21, 2009 3:59 pm ]
Post subject:  RE:Opening Files[C Sharp]

Well I'm not having fun with this >.> To damn irritating.

Author:  RealityFix [ Mon Nov 23, 2009 3:59 pm ]
Post subject:  RE:Opening Files[C Sharp]

Hate to double post but I get this weird error and i have no idea what to do with it >.>

Quote:

Error 1 Static member 'DungeonsAndDragons.SearchAndSort.Shell(string[], string[], int)' cannot be accessed with an instance reference; qualify it with a type name instead C:\Documents and Settings\Owner\Desktop\DungeonsAndDragons2\DungeonsAndDragons2\DungeonsAndDragons\DungeonsAndDragons\DungeonsAndDragons\Form1.cs 84 21 DungeonsAndDragons


I have no idea what this is T_T

Author:  rdrake [ Mon Nov 23, 2009 4:04 pm ]
Post subject:  RE:Opening Files[C Sharp]

You're accessing a static method on an instance variable instead of the type. Post the code that's giving you the error if you're still stuck.

Author:  RealityFix [ Mon Nov 23, 2009 4:59 pm ]
Post subject:  RE:Opening Files[C Sharp]

The code is a little confusing. I didn't write the search and sort thing I grabbed it off my teacher (Im a lazy bugger when it comes down to it)

but what I think your saying is that because my search thing is a static and my OpenFile method is just a public void that it will not work right?
C#:


//(public void OpenFile)
  try
            {

                openFileDialog1.Reset();
                openFileDialog1.InitialDirectory = Application.ExecutablePath;
                openFileDialog1.Filter = "Text files (*.txt)|*.txt|" + "All files|*.*";
                openFileDialog1.FilterIndex = 1;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //string s = Application.StartupPath;
                    //string f = s + @"\albums.txt";

                    FileName = openFileDialog1.FileName;
                    r = new StreamReader(FileName);

                    string WholeLine;

                    while ((WholeLine = r.ReadLine()) != null)
                    {
                        c++;
                        lines = WholeLine.Split(',');
                        NameMon[c] = lines[0];
                        borrower[c] = lines[1];

                    }

                    //r.Close();    In finally statement below
                    //n = c;

                    //add filename to statusbar
                    toolStripStatusLabel1.Text = "File Loaded";

                   // Search.Shell(NameMon, borrower, n);
                }


C#:

 public static void Shell(string[] albn, string[] borrown, int u)
        {

            int gap = u;
            int maximum, iplusg, ex;

            string temp = "";

            do
            {
                gap = gap / 2;
                maximum = u - gap;

                do
                {
                    ex = 0;

                    for (int i = 1; i <= maximum; i++)
                    {
                        iplusg = i + gap;

                        if (albn[i].CompareTo(albn[iplusg]) > 0)
                        {
                            temp = albn[i];
                            albn[i] = albn[iplusg];
                            albn[iplusg] = temp;
                            temp = borrown[i];
                            borrown[i] = borrown[iplusg];
                            borrown[iplusg] = temp;
                            ex = ex + 1;
                        }
                    }
                }
                while (ex != 0);

            }
            while (gap >= 1);
        }

Author:  rdrake [ Mon Nov 23, 2009 5:06 pm ]
Post subject:  RE:Opening Files[C Sharp]

C#:
Search.Shell(NameMon, borrower, n);
Is "Search" an object or the name of the class where Shell() is?

If it's an object then you're doing it wrong. Call this instead.
C#:
SearchAndSort.Shell()

Author:  RealityFix [ Mon Nov 23, 2009 5:28 pm ]
Post subject:  RE:Opening Files[C Sharp]

C#:

SearchAndSort Search = new SearchAndSort();


Sorry forgot to include the part above the try{}.

Author:  rdrake [ Mon Nov 23, 2009 6:34 pm ]
Post subject:  RE:Opening Files[C Sharp]

Yup, doing it wrong. The method is static, you absolutely have to call it as such.

C#:
SearchAndSort.Shell(NameMon, borrower, n);


Notice how I'm calling the method on the class name, not an instance of that class. Try the above code and it should work just fine.

Also please in the future try to be a bit more careful when including bits of code in your post. Some of your questions are hard to understand due to missing code fragments. You're better off including too much than not enough.

Author:  RealityFix [ Mon Nov 23, 2009 7:20 pm ]
Post subject:  RE:Opening Files[C Sharp]

Thanks!

And I will put more data in my questions in the future.

Author:  RealityFix [ Mon Nov 23, 2009 8:12 pm ]
Post subject:  RE:Opening Files[C Sharp]

Im gonna do another double post here since there is no real point in starting a new thread for such a small question:

When using a ListView how do i add text to it in details mode? I know how to add columns and such but how do i get the data under each one?

E.g. Putting Bob under the Names column, and putting 13 under the Age column?

I looked a lot on google but a lot of there stuff is overly complicated for what I want to do. Im just transferring text from a textbox.

Author:  rdrake [ Mon Nov 23, 2009 11:06 pm ]
Post subject:  RE:Opening Files[C Sharp]

C#:
ListViewItem lvi = new ListViewItem("Bob");
lvi.SubItems.Add("13");
lvi.SubItems.Add("Other stuff");

Author:  RealityFix [ Tue Nov 24, 2009 3:21 pm ]
Post subject:  RE:Opening Files[C Sharp]

Thanks ever so much ^_^

Your probably one of the few mods I've seen on a forum that do something.

I would give karma but I need to post more >.>

I donated some bits to ya instead, its only 20 but thats a pretty good amount for the number of posts that i have.

Author:  RealityFix [ Tue Nov 24, 2009 5:14 pm ]
Post subject:  RE:Opening Files[C Sharp]

God i feel like such a bugger.

Another Question!

C#:

 private void MinorMonsterBox2_Click(object sender, EventArgs e)
        {
            ListViewItem Temp = new ListViewItem(MinorMonsterBox2.SelectedItems.ToString());

            BattleList.Items.Add(Temp.SubItems.ToString());   // This is obviously wrong.
        }


I basically want to transfer one listview item to another one on click. Which I kinda get (the clicking part) but I have no idea how to actually do the transfer properly >.>

Author:  rdrake [ Tue Nov 24, 2009 5:35 pm ]
Post subject:  RE:Opening Files[C Sharp]

SelectedItems returns a collection of ListViewItems.

C#:
// Get the selected items.
ListViewItemCollection items = box1.SelectedItems;

// Remove them from the first box.
for (ListViewItem item in items)
{
    box1.Remove(item);
}

// Put them in the second.
for (ListViewItem item in items)
{
    box2.Add(item);
}
Something like that.

Author:  RealityFix [ Tue Nov 24, 2009 6:17 pm ]
Post subject:  RE:Opening Files[C Sharp]

I get the stuff after the ListViewItemCollection. But when i actually try to do ListViewItemCollection it doesn't exist. I looked through the list of suggestions it gives me but I can't seem to find anything that works >.>

Author:  rdrake [ Tue Nov 24, 2009 7:30 pm ]
Post subject:  RE:Opening Files[C Sharp]

My bad.

C#:
ListView.ListViewItemCollection

Author:  RealityFix [ Wed Nov 25, 2009 3:10 pm ]
Post subject:  RE:Opening Files[C Sharp]

Hmmm thanks but when i try to do

C#:


ListView.ListViewItemCollection Items = MinorMonsterBox2.SelectedItems;



I get a error.

It tried:

C#:

ListView.ListViewItemCollection Items = new ListView.ListViewItemCollection(MinorMonsterBox2);
//ListView.ListViewItemCollection Items = new ListView.ListViewItemCollection(MinorMonsterBox2.SelectedItems --- This didn't work at all.)


and that didn't work the way i wanted.

Author:  rdrake [ Wed Nov 25, 2009 3:32 pm ]
Post subject:  RE:Opening Files[C Sharp]

What's the error?

Author:  RealityFix [ Wed Nov 25, 2009 4:00 pm ]
Post subject:  RE:Opening Files[C Sharp]

Error 1 Cannot implicitly convert type 'System.Windows.Forms.ListView.SelectedListViewItemCollection' to 'System.Windows.Forms.ListView.ListViewItemCollection' C:\Documents and Settings\Owner\Desktop\DungeonsAndDragons2\DungeonsAndDragons2\DungeonsAndDragons\DungeonsAndDragons\DungeonsAndDragons\Form1.cs 366 53 DungeonsAndDragons


its a conversion error.

Author:  rdrake [ Wed Nov 25, 2009 4:03 pm ]
Post subject:  RE:Opening Files[C Sharp]

Change my code to use that class then.

Author:  RealityFix [ Wed Nov 25, 2009 4:50 pm ]
Post subject:  RE:Opening Files[C Sharp]

Im a crappy programmer if you haven't guessed.

C#:


            ListView.ListViewItemCollection Items = new ListView.ListViewItemCollection(MinorMonsterBox2);
            BattleList.Items.Add(Items);


Best thing i could do to almost make it work?

Error:
Error 1 The best overloaded method match for 'System.Windows.Forms.ListView.ListViewItemCollection.Add(string)' has some invalid arguments C:\Documents and Settings\Owner\Desktop\DungeonsAndDragons2\DungeonsAndDragons2\DungeonsAndDragons\DungeonsAndDragons\DungeonsAndDragons\Form1.cs 367 13 DungeonsAndDragons


: