4 Replies - 376 Views - Last Post: 05 April 2010 - 01:15 PM Rate Topic: -----

#1 Fretwise  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 15-February 09

Few quick questions involving list and textboxes

Posted 05 April 2010 - 11:13 AM

I am currently working on a project in visual c# 2008 and I have came across a few things I am wanting to do but am having trouble figuring out (and I know they are going to be silly overlooks like most things). Currently I am using a button to open a file and adding the words into different list's (Colors, Shapes, Sizes, etc). The main issue I am having is, I want to count the number of elements with another button, but it seems like I cannot access anything in the list created by the 'open file' button. Here is some partial coding to get a idea (also if there is a better way to add the strings to the different lists, please feel free to let me know):

Geo GeometricDatabase = new Geo();
string line;

Microsoft.Win32.OpenFileDialog fileDlg = new Microsoft.Win32.OpenFileDialog();
     fileDlg.ShowDialog();
     MessageBox.Show(fileDlg.FileName);

System.IO.StreamReader TextReader;
     TextReader = new System.IO.StreamReader(fileDlg.FileName);
          while ((line = TextReader.ReadLine()) != null)
          {
                string[] words = line.Split(',');
                      GeometricDatabase.AddColor(words[0]);
                      GeometricDatabase.AddShape(words[1]);
                      GeometricDatabase.AddSize(words[2]);
                     
          }



So basically, its opening a text file (which I have formated in a certain way) it reads each line separately taking the words and putting them into an array, then takes the words from the generic array and adds them to the specific list. Now the trouble comes when in the same window but different button I want to count the number of elements in say Color. If I add just:
GeoListBox.Text = GeometricDatabase.GetCount().ToString();


I recieve the error "The name 'GeometricDatabase' does not exist in the current context", which is what I would expect. Adding the same thing as the above button:
Geo GeometricDatabase = new Geo();
GeoListBox.Text = GeometricDatabase.GetCount().ToString();



Gives me a count of zero, so I am assuming there is a way to carry over the GeometricDatabase without creating a new one?

My next question is, what is the best way to post these list's in a large textbox in a organized fashion. I could do:
GeoListBox.Text = GeometricDatabase.getshape(1) + "\n" + GeometricDatabase.getcolor(1);


I would have to do this for every single element and list which would get very tedious and long. Any suggestions? The goal is to make it look something similar to this (having the codebox represent the textbox in the program)
Square
Green
24

Rectangle
Red
58

Triangle
Blue
36



Also note: I have tried using the count to find the number of elements then make a for loop to just print it, but unfortunately I will just keep erasing the previous index's data and replace it with the next. I know these are relatively long and probably pretty silly, but just overlooking them at the moment. Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Few quick questions involving list and textboxes

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Few quick questions involving list and textboxes

Posted 05 April 2010 - 11:42 AM

View PostFretwise, on 05 April 2010 - 01:13 PM, said:

I am currently working on a project in visual c# 2008 and I have came across a few things I am wanting to do but am having trouble figuring out (and I know they are going to be silly overlooks like most things). Currently I am using a button to open a file and adding the words into different list's (Colors, Shapes, Sizes, etc). The main issue I am having is, I want to count the number of elements with another button, but it seems like I cannot access anything in the list created by the 'open file' button. Here is some partial coding to get a idea (also if there is a better way to add the strings to the different lists, please feel free to let me know):

Geo GeometricDatabase = new Geo();
string line;

Microsoft.Win32.OpenFileDialog fileDlg = new Microsoft.Win32.OpenFileDialog();
     fileDlg.ShowDialog();
     MessageBox.Show(fileDlg.FileName);

System.IO.StreamReader TextReader;
     TextReader = new System.IO.StreamReader(fileDlg.FileName);
          while ((line = TextReader.ReadLine()) != null)
          {
                string[] words = line.Split(',');
                      GeometricDatabase.AddColor(words[0]);
                      GeometricDatabase.AddShape(words[1]);
                      GeometricDatabase.AddSize(words[2]);
                     
          }



So basically, its opening a text file (which I have formated in a certain way) it reads each line separately taking the words and putting them into an array, then takes the words from the generic array and adds them to the specific list. Now the trouble comes when in the same window but different button I want to count the number of elements in say Color. If I add just:
GeoListBox.Text = GeometricDatabase.GetCount().ToString();


I recieve the error "The name 'GeometricDatabase' does not exist in the current context", which is what I would expect. Adding the same thing as the above button:
Geo GeometricDatabase = new Geo();
GeoListBox.Text = GeometricDatabase.GetCount().ToString();



Gives me a count of zero, so I am assuming there is a way to carry over the GeometricDatabase without creating a new one?


The concept you are missing here is the scope of the variable. In the first block of code, the GeometricDatabase object is created inside of a method/event. The scope of that variable is limited to that method and that method only. It's not accessible outside of the method.

Your solution is to make it a class level variable. For example...

// this is outside of the event handler
Geo GeometricDatabase = new Geo();

private void btnOpenFile_Click(object sender, EventArgs e)
{
     string line;

     Microsoft.Win32.OpenFileDialog fileDlg = new Microsoft.Win32.OpenFileDialog();
     fileDlg.ShowDialog();
     MessageBox.Show(fileDlg.FileName);

     System.IO.StreamReader TextReader;
     TextReader = new System.IO.StreamReader(fileDlg.FileName);
          while ((line = TextReader.ReadLine()) != null)
          {
                string[] words = line.Split(',');
                      GeometricDatabase.AddColor(words[0]);
                      GeometricDatabase.AddShape(words[1]);
                      GeometricDatabase.AddSize(words[2]);
                     
          }
}



by placing the declaration outside of the event handler, the variable is now a class level variable that can be used by any(or all) of the methods/events of the form.

This post has been edited by eclipsed4utoo: 05 April 2010 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 Fretwise  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 15-February 09

Re: Few quick questions involving list and textboxes

Posted 05 April 2010 - 11:54 AM

Thanks for the help! I knew it was going to be something silly. By moving it outside the event seemed to work until I ran the program. I am now getting a StackOverFlowException according to the debugger (program crashes immediately when ran) claiming the Geo GeometricDatabase = new geo(); is a infinite or recursive loop.

-Fixed: Just changed it to:
static Geo GeometricDatabase = new Geo();



Any clues on the text box?

This post has been edited by Fretwise: 05 April 2010 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Few quick questions involving list and textboxes

Posted 05 April 2010 - 12:07 PM

View PostFretwise, on 05 April 2010 - 01:54 PM, said:

Thanks for the help! I knew it was going to be something silly. By moving it outside the event seemed to work until I ran the program. I am now getting a StackOverFlowException according to the debugger (program crashes immediately when ran) claiming the Geo GeometricDatabase = new geo(); is a infinite or recursive loop.

-Fixed: Just changed it to:
static Geo GeometricDatabase = new Geo();



just putting static in front should not be a solution for this problem. We would need to see more(if not all) of your code to see what the issue is.


View PostFretwise, on 05 April 2010 - 01:54 PM, said:

Any clues on the text box?


not really sure. can you post the code that you tried? I was trying to go figure out what issue you were having but couldn't really understand it.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Few quick questions involving list and textboxes

Posted 05 April 2010 - 01:15 PM

Forgot to ask....why are you using the Microsoft.Win32.OpenFileDialog instead of the System.Windows.Forms.OpenFileDialog?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1