Need help with opening/saving with multiple textboxes.

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1111 Views - Last Post: 09 August 2011 - 10:22 PM Rate Topic: -----

#1 kytro360  Icon User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 11:41 AM

I want to save the text from several textboxes to a textfile. Then once I save it I want to open it and all the text go to their appropiate textbox.


Here is the open code Im working with:

private void button6_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.Title = "Open a File";
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Text Files|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
                using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
                {
                    textBox1.Text = reader.ReadToEnd();
                    textBox2.Text = reader.ReadToEnd();
                }
            }
        }


And here is the save code Im working with:

private void button7_Click(object sender, EventArgs e)
        {
           
            StreamWriter sw = new StreamWriter("C:\\test.txt");

          
            sw.WriteLine(textBox1.Text);
            sw.WriteLine(textBox2.Text);

            sw.Close();


        }

This post has been edited by kytro360: 09 August 2011 - 11:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with opening/saving with multiple textboxes.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,102
  • Joined: 27-December 08

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 11:43 AM

What is wrong with your code? Please specifically describe any problems or errors you are encountering.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 11:48 AM

When you debug(if you have even tried it), what happens? What are you expecting it to do vs. what's actually happening?
Was This Post Helpful? 0
  • +
  • -

#4 kytro360  Icon User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 11:58 AM

I debugged it. When I click save it saves the file. I open the file from my computer (not my program) to see if it really saved. Only the first textbox got saved not the second one. There was just a space then the text of the first box.

I then wanted to test what would happen if it opened. So I opened it and it was the same as the text file. But the text of the first box went to the second box and it was empty in the first box.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 12:44 PM

See FAQ #5
See FAQ #16



FAQ (Frequently Asked Questions - Updated Aug 2011

  • Q: My program doesn't work. I didn't show you any code or actually tell you the error message. How do I fix it?
    Spoiler


  • Q: How do I get Form 'A' to make a change or talk to Form 'B'


  • Q: I'm trying to make a password/login form and...
    A: Password handling tutorial


  • Q: I'm making a client/server chat program and .....
    A:Client/server chat tutorial


  • Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?


  • Q: I don't understand XML Serialization so I can save my information/class


  • Q:How do I get sound in my program?
    A: Adding sound to your C# application


  • Q: How do I use the serial port?
    A: Serial port communication


  • Q: How do I deploy my program / Make an installer?
    A: C# application deployment project


  • Q: How do I do compression or .zip files?
    A:

  • Q: How do I do multi-threading? Having a problem with cross-threading...


  • Q: I am making a calculator as my school homework project and ...
    A: Calculator tutorial


  • Q: I've been struggling with this for days/weeks and I can't figure it out and my professor is worthless and can't teach. Can someone here explain it to me?
    Spoiler


  • Q: How do I save some information like preferences, settings or other data?


  • Q: I want my textbox to offer suggestions as the user types.
    A: Creating an auto-complete textbox


  • Q: I want to write a program that does x, y and z with features for A, B, and C. {Notice there is no question in this question}
    Spoiler


  • Q: I need to code to do x... Someone tell me the code for y...
    Spoiler


  • Q: What is the best programming language? What programming language should I learn?
    Spoiler


  • Q: How do I get my program to programmatically simulate a mouse click or button press?
    A: Simulate mouse and keystroke (even to another application)


  • Q: I'm not really sure what I want to do with my future? Do you guys like programming? I think I kinda like math and games and computers? What should I do or study?
    A: Get experience while you find yourself


  • Q: How do I use PInvoke and the Win32 api calls to get functions not available in .NET framework?
    A: Using the Win32 API Tutorial

  • Q: How can I have both a WinForms Windows app, and still have a Console window open?
    Spoiler


  • Q: I want to do x, y, z with the HTML that comes out of a website ...
    A: Check out the HTML Agility Pack. Yes its free.


  • Q: How do I make a textbox just accept numbers and not letters?
    A: Read this thread

  • Q: What is the best programming language or O.S. I should be learning on?
    Spoiler

Was This Post Helpful? 0
  • +
  • -

#6 kytro360  Icon User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 12:52 PM

Geez @tlhIn`toq why do yo always have to attack my thread with FAQ's. There was clearly a question if not why would I post this thread? I wanted to know what was wrong with my code...
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 12:56 PM

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)

Next, let's talk about this a moment:
private void button6_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.Title = "Open a File";
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Text Files|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
                using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
                {
                    textBox1.Text = reader.ReadToEnd();
                    textBox2.Text = reader.ReadToEnd();
                }
            }
        }



Setting an initial directory of 'C:' potentially could cause a problem because you are assuming there is a 'C:' drive. I've had computers where the boot drive was F: and there was no C: drive. Or a network booking computer.

Next you are not confirming the file exists after the user clicks the 'ok' button in the OpenFileDialog. What if they type in a path that isn't valid? You should take a look at this tutorial and see how to confirm the existence of the file first. THis is why we make these tutorials for you guys to learn from.
http://www.dreaminco...openfiledialog/

What you've done here to read the file is a horrible way to read text files. I know I have replied to at least one of your posts with my standard set of tutorials and resources which has links to the proper way to do this.

Quote

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.

Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:10 PM

View Postkytro360, on 09 August 2011 - 01:52 PM, said:

Geez @tlhIn`toq why do yo always have to attack my thread with FAQ's. There was clearly a question if not why would I post this thread? I wanted to know what was wrong with my code...


Please go back and look at the responses to your post before mine.
You'll notice that the first two responses were also asking you to clarify what the problem and question was because you didn't ask anything. All you basically said was "I need help" and supplied code. So I pointed you to a thread about supplying more detail so the rest of us can help you.

It was a VERY basic code block that would have benefited from a few minutes of debugging. So I pointed you to the tutorials on how to debug.


I'm sorry if you feel I am always attacking your posts. I don't want you to feel attacked when you come here so I am going to stop responding to any of your posts.

Let me leave with this comment however. Several times, several experts have tried to gently guide you toward working through a book or two from cover to cover before you start trying to make your own projects. I have guided you to resources for books and on-line tutorials so you can learn the C# language and concepts before you charge off on your own trying to build something from scratch.

None of the gentle guiding seems to be reaching you. You aren't taking anyone's advice. You aren't working through any of the tutorials. You seem be only accessing these things on a hit or miss basis when you need that one particular scrap of information. You're refusal to learn first and worry about creativity after is going to be the downfall of your coding education. Several experts here have tried to guide you to the path that works and has worked for tens of thousands of coders for the last couple generations before you were even born. You just don't want to do it. That's your choice and no skin off my nose either way. I just think its a shame to waste all that incentive you obviously have. You seem to truly want to be a coder. If only you had the determination and patience to educate yourself on the basics, and a little less ego so you wouldn't think you were too good to start at the beginning like the rest of us.

I truly believe you have the potential to be good at this. But if you never accept taking advice and you never take the time to learn it will never happen.

I wish you the best of luck. As promised, I don't want you to feel unwelcome here so I will stop responding to your future questions so you don't feel I'm attacking you.
Was This Post Helpful? 4
  • +
  • -

#9 kytro360  Icon User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:31 PM

Its not that I dont want you to stop helping me out its just that I dont like it when you pull out the FAQ like I dont know crap. I have already a basic understanding of C# after watching several series of C# programming on YouTube.

Thanks for the links

P.S: I used to code in Visual Basic so the transition was easier when I moved to C# thats why Im moving faster

This post has been edited by kytro360: 09 August 2011 - 01:33 PM

Was This Post Helpful? -5
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,102
  • Joined: 27-December 08

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:33 PM

You get better responses by asking questions the smart way. When you pose ambiguous or unclear questions, you don't always get the type of responses you are looking for. The FAQs are designed as a resource so you can find what you need. If we don't have enough information to help you, that is a logical place to point you since you know what you need and we don't.
Was This Post Helpful? 2
  • +
  • -

#11 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 164
  • Joined: 01-August 08

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:35 PM

If you are just getting into programming in C#, I recommend reading this book. It helped me out a lot and I hope it can help you.
Was This Post Helpful? 1
  • +
  • -

#12 kytro360  Icon User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:38 PM

Okay then let me rephrase my question. How can you save the text of multiple textboxes then open it and all the text going back to its appropiate box.
Was This Post Helpful? 0
  • +
  • -

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:39 PM

View Postkytro360, on 09 August 2011 - 04:31 PM, said:

Its not that I dont want you to stop helping me out its just that I dont like it when you pull out the FAQ like I dont know crap. I have already a basic understanding of C# after watching several series of C# programming on YouTube.

Thanks for the links

P.S: I used to code in Visual Basic so the transition was easier when I moved to C# thats why Im moving faster


Honestly, you don't really have a basic understanding of C#. You might be able to handle the syntax, but going by your threads you have created, you don't really have a basic grasp of C#.

Then I would suggest that you slow down, because you don't need to be moving faster. Would you try building an engine just because you looked at the outside of one?
Was This Post Helpful? 2
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:41 PM

View Postkytro360, on 09 August 2011 - 02:38 PM, said:

Okay then let me rephrase my question. How can you save the text of multiple textboxes then open it and all the text going back to its appropiate box.



You mean differently than was already shown to you in these tutorial links that were already provided to you?


View PosttlhIn`toq, on 09 August 2011 - 01:56 PM, said:

Quote

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.


What more are you expecting than for us to give you the place to read and learn how to do it right?

It was already said to you that you were doing it way badly and that you should follow the MSDN tutorials for reading and writing files.

I don't see any updated code from you showing that you have tried to use this information or proper technique.

So what do you want us to tell you? Is there another way to say "Do it the right way - the way we are linking to you" ?

This post has been edited by tlhIn`toq: 09 August 2011 - 01:44 PM

Was This Post Helpful? 1
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Need help with opening/saving with multiple textboxes.

Posted 09 August 2011 - 01:43 PM

View Postkytro360, on 09 August 2011 - 04:38 PM, said:

Okay then let me rephrase my question. How can you save the text of multiple textboxes then open it and all the text going back to its appropiate box.


You already know how to write to a file, so let's build off of it.

First, you would need to save to the file which textbox the text came from. Possibly using the Name property of the textbox. This would give you the textbox that the text came from, and then you could get the Text of that textbox.

Second, the easiest way would to put both pieces of data(textbox identifier and text) in the same line in the file. To do this, you would need some type of separator between them so you could tell them apart.

Third, since you know the design of each line(textbox identifier, some separator, then text), you can read each line of the file, determine which textbox it goes to, and set the Text of that textbox.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2