Hey Guys!!!!

I am searching everywhere but i did'nt find actual answer from internet or may b I am wrong..
I am making a program that stores data.. my all program works perfectly but there is one problem
i cannot solve.. i am new to WINDOWS FORM (Object ORIENTED) and i directly jumped from console
version to WINDOWS version.. coming to topic ...

I have LISTBOX1 and EDIT and SAVE button when i select name in List box and press EDIT button
all information is retrieved and set in textboxes.. but i want that the selected name in LISTBOX
make a global variable and so that when i want variable i get from global variable ?? how can i do this
here is my code../

private: System::Void edit_button(System::Object^  sender, System::EventArgs^  e) {
			GetData();
		 }

	private: System::Void GetData() {
				String^ SelItem;
				SelItem = listBox1->SelectedItem->ToString();
			StreamReader^ ReadFile = gcnew StreamReader("addressbook.txt");
			String^ LineData;
			while (ReadFile->Peek() >= 0) {
			LineData = ReadFile->ReadLine();
			if (LineData->Contains(SelItem) == true) 
				{ 
					textBox1->Text = LineData->ToString(); 
					LineData = ReadFile->ReadLine();
					textBox2->Text= LineData->ToString();
					LineData = ReadFile->ReadLine();
					textBox3->Text= LineData->ToString();
					LineData = ReadFile->ReadLine();
					textBox4->Text= LineData->ToString();
					LineData = ReadFile->ReadLine();
					textBox5->Text= LineData->ToString();
			}
		}
		ReadFile->Close();
	 }

	private: System::Void save_edited(System::Object^  sender, System::EventArgs^  e) {
            here i want SelItem how can i get this from edit_button 
              i dont want listbox1 selected item .. i want SelItem string value that was created in
              edit_button ... how can i pass this variable to save_edited button ?? 
 
        }

Thanks In advance.. and my english is not too much perfect.!!!

Scroll further up in your Form1.h file and you'll find a bunch of variables being declared (your listbox and buttons, etc., it will say private: System::Windows::Forms::Button^ edit_button; or the like). Amidst those put your private: String ^ SelItem; .

It won't be "global" but I don't think that you want that either, it will be local to your class.

Then just use it within those methods (being aware that since these are message handlers they are not called in any particular order, so the user could click save_edited before edit_button).

thats great ..!!! work perfectly .. but i know this is not the way to code perfect.. but i am at beginner level .. even i don't know what is class members.. and blah blah all i know ,, 2 dimension array .. string .... loops :)
but thanks for guiding me ..

i have one more question .. there is one thread but i donot want to hijack other user thread.

question is i have text file
and as you see my above coding is like reading line then write what was read... and it reads 4 times
my address book save data like this.

Name: Jhon Cena
Father Name: Ninja Turtle
Age: 22
Mob No: 654654
Email: microsoft dot com

my code find 'Name:' in text file
and if it is found. then the code read 4 lines more to get other information

if i can save my information like this

Name:Jhon Cena Father Name: Ninja Turtle Age: 22 Mob No: 654654 Email: microsoft dot com

now how can i read this above line and put them into text boxes
i read about 'strtok' but my concept was not much clear... can you guide me little??

thanks again :)

I suggest you read up on such things as this:
ReadFile

Just keep doing newlines in the file, but since you known the structure of what you're getting, for example,

You need 4 variables from each entry, then you loop getline and count which row you're on, take that and divide by how many variables you have and insert each value in your struct.

#define FAMILYMEMBERS  5
#define PERSON_COUNTVARIABLES 4

typedef struct
{
 char *name, *email;
 int age, mobno;
}Person;

int main()
{
string line;
int linecount = 0;
Person Family[FAMILYMEMBERS-1]; //5 people in the family

  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline(myfile, line);
      linecount++;

      int member = (int)floor((float)linecount/PERSON_COUNTVARIABLES); //PERSON_COUNTVARIABLES is how many variables (lines) to check for each person
      if(member > FAMILYMEMBERS-1)
       break;
      switch(countvar)
      {
        case 0:
           Family[member].name = line;
           break;
        case 1:
           Family[member].email = line;
           break;
        case 2:
           Family[member].age = atoi(line);
           break;
        case 3:
           Family[member].mobno = atoi(line);
           break;
      }
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

 return 0;
}

Where the text file example.txt could say something like

"John Doe\n"
"JohnDoe@unknown.com\n"
"20\n"
"555222222\n"
"Jane Doe\n"
"JaneDoe@unknown.com\n"
"16\n"
"555111111\n"
"Bob Dole\n"
"BobDole@forpresident.com\n"
"41\n"
"555133712\n"
"Mom\n"
"Mom@momscooking.com\n"
"28\n"
"555123456\n"

This is not the best approach, but it's simple.
It isn't dynamic.

hmmmmmm i'll try this too ... but what if per person whole information is saved in one line
and i have 4 person information ... and i want to to read each line so that i get rid of 4 loops ....
i am just figuring out because my code looks awkward .. i am trying to do my best..

thanks ///

but i know this is not the way to code perfect

Unfortunately the whole system of packing all of this stuff into the header file makes it inherently harder to "code perfect," so it's not your fault. Keeping it at the class level is at least organizing it into one object even though it's a mishmash of all your controls and stuff.

Rather than use the unmanaged code as ShadowScripter suggested (which is perfectly valid under different circumstances but can be a bear in the managed .NET code under which the OP is working), you can use a System::IO::StreamReader object (see here) instead with its ReadLine method.

I think it would be easier to keep the individual data on separate lines in the text file (as in ShadowScripter's example) and just do each readline into a separate variable. I think it's great you want to save space in your file but this makes life much easier. You could conjoin ReadLine with the String ^ split() method of the line you've read in but you'd have to keep track of which was your "Name:" "Age:" labels and what was real text. Unless you require your text file to be human readable, you could get rid of all of the labels and use space as your delimiter.

thanks for the guide .. i will look shadowscripter codes and try to understand too .. :)
and thanks for the last help .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.