How to write selected combobox and textbox text to specific line in text file
Trying to save selected comboBox and textBox text to text file.
however there are multiple values being saved.
To avoid confusion systematically saving them on a specific line of the text file will help identify the values.
tried using the following, but application hangs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Setups : Form
{
public Setups()
InitializeComponent();
//this.TopMost = true;
//this.FormBorderStyle = FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
private void textBox8_TextChanged(object sender, EventArgs e)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
static void lineChanger(string newText, string fileName, int line_to_edit)
string arrLine = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
public void button5_Click(object sender, EventArgs e)
1", comboBox1.Text, textBox8.Text), "sample.txt", 30); //
// Succesfully wrote to file on first line
//StreamWriter sw = new StreamWriter("Test.txt");
//sw.WriteLine($"comboBox1.Text.textBox8.Text");
//sw.Close();
c# winforms
add a comment |
Trying to save selected comboBox and textBox text to text file.
however there are multiple values being saved.
To avoid confusion systematically saving them on a specific line of the text file will help identify the values.
tried using the following, but application hangs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Setups : Form
{
public Setups()
InitializeComponent();
//this.TopMost = true;
//this.FormBorderStyle = FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
private void textBox8_TextChanged(object sender, EventArgs e)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
static void lineChanger(string newText, string fileName, int line_to_edit)
string arrLine = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
public void button5_Click(object sender, EventArgs e)
1", comboBox1.Text, textBox8.Text), "sample.txt", 30); //
// Succesfully wrote to file on first line
//StreamWriter sw = new StreamWriter("Test.txt");
//sw.WriteLine($"comboBox1.Text.textBox8.Text");
//sw.Close();
c# winforms
Did you debug the code? Which line of code makes the application hang?
– Chetan Ranpariya
Nov 14 '18 at 1:50
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
– Jerry Paulman
Nov 14 '18 at 1:57
1
Then you have an indexing error. I assume it happens in thelineChanger
method, set a break point and inspect the array and the index you're trying to access.
– Vulpex
Nov 14 '18 at 2:24
add a comment |
Trying to save selected comboBox and textBox text to text file.
however there are multiple values being saved.
To avoid confusion systematically saving them on a specific line of the text file will help identify the values.
tried using the following, but application hangs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Setups : Form
{
public Setups()
InitializeComponent();
//this.TopMost = true;
//this.FormBorderStyle = FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
private void textBox8_TextChanged(object sender, EventArgs e)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
static void lineChanger(string newText, string fileName, int line_to_edit)
string arrLine = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
public void button5_Click(object sender, EventArgs e)
1", comboBox1.Text, textBox8.Text), "sample.txt", 30); //
// Succesfully wrote to file on first line
//StreamWriter sw = new StreamWriter("Test.txt");
//sw.WriteLine($"comboBox1.Text.textBox8.Text");
//sw.Close();
c# winforms
Trying to save selected comboBox and textBox text to text file.
however there are multiple values being saved.
To avoid confusion systematically saving them on a specific line of the text file will help identify the values.
tried using the following, but application hangs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Setups : Form
{
public Setups()
InitializeComponent();
//this.TopMost = true;
//this.FormBorderStyle = FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
private void textBox8_TextChanged(object sender, EventArgs e)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
static void lineChanger(string newText, string fileName, int line_to_edit)
string arrLine = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
public void button5_Click(object sender, EventArgs e)
1", comboBox1.Text, textBox8.Text), "sample.txt", 30); //
// Succesfully wrote to file on first line
//StreamWriter sw = new StreamWriter("Test.txt");
//sw.WriteLine($"comboBox1.Text.textBox8.Text");
//sw.Close();
c# winforms
c# winforms
edited Nov 14 '18 at 1:13
John
12.8k32242
12.8k32242
asked Nov 14 '18 at 1:06
Jerry PaulmanJerry Paulman
13
13
Did you debug the code? Which line of code makes the application hang?
– Chetan Ranpariya
Nov 14 '18 at 1:50
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
– Jerry Paulman
Nov 14 '18 at 1:57
1
Then you have an indexing error. I assume it happens in thelineChanger
method, set a break point and inspect the array and the index you're trying to access.
– Vulpex
Nov 14 '18 at 2:24
add a comment |
Did you debug the code? Which line of code makes the application hang?
– Chetan Ranpariya
Nov 14 '18 at 1:50
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
– Jerry Paulman
Nov 14 '18 at 1:57
1
Then you have an indexing error. I assume it happens in thelineChanger
method, set a break point and inspect the array and the index you're trying to access.
– Vulpex
Nov 14 '18 at 2:24
Did you debug the code? Which line of code makes the application hang?
– Chetan Ranpariya
Nov 14 '18 at 1:50
Did you debug the code? Which line of code makes the application hang?
– Chetan Ranpariya
Nov 14 '18 at 1:50
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
– Jerry Paulman
Nov 14 '18 at 1:57
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
– Jerry Paulman
Nov 14 '18 at 1:57
1
1
Then you have an indexing error. I assume it happens in the
lineChanger
method, set a break point and inspect the array and the index you're trying to access.– Vulpex
Nov 14 '18 at 2:24
Then you have an indexing error. I assume it happens in the
lineChanger
method, set a break point and inspect the array and the index you're trying to access.– Vulpex
Nov 14 '18 at 2:24
add a comment |
3 Answers
3
active
oldest
votes
A better solution would be to create a class that has all the settings and then serialize/deserialize it using JSON.
Use nuget to add the JSON package to your app. Example code to serialize: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
add a comment |
An application that hang and an out-of-range exception are 2 different things. Be precise in your question if you want help.
Also, your lineChanger
function would obviously fails if the file you read does not already contains at least 30 lines when you click on button 5.
Hard coded number like that is always a bad idea in production code. If at some point, the file content is modified, it would be very hard to ensure that everything is properly fixed in the code.
If you really want to use such text file, then at the very least:
- use an enumeration or constants define a single place for the purpose of each line.
- use a class to manage your configuration so that only that class knows the structure of the file.
But as mentioned in another answer, it would be preferable to use another format like JSON. Almost every other format except binary data would be preferable to a text file (assuming that the content is settings for your application).
Having said that, if you really want lineChanger
, then you should add a test like that:
if (line_to_edit >= arrLine.Length)
List<string> list(arrLine);
while (List.Length < line_to_edit) list.Add(string.Empty);
list.Add(newText);
arrLine = list.ToArray();
else … old code from question…
However, if you read other lines from the file or often read lines, it might become very slow to open/read/update/write/close the file each time a line is modified. Another reason why it would be a good idea to have a class to manage settings so that you load them once when opening the dialog and once when closing the dialog (assuming that the dialog is displayed a relatively short period of time and it is not critical to save changes immediately).
add a comment |
I think your question is too broad and needs more details.
You need to think of separating your form from your data. Create a data class to hold the values you want to be stored or read and have the UI interact with this class using data binding. Then your data class will always contain the most up-to-data information.
When needed, you can serialize your data file with TextStream
, BinaryFormatter
, XmlSerializer
or JSON
, depending on if the data needs to be accessed by other programs or locations. You can even use the built-in Settings
to save the data before the application exits and to be read back in when it starts up again.
So please edit the question to clarify what you want to do with this data and add an example of what it looks like as a whole (all the lines in your text file now). Then we can answer more specifically.
This is great. Will try and update my question and make it more precise
– Jerry Paulman
Nov 14 '18 at 20:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291744%2fhow-to-write-selected-combobox-and-textbox-text-to-specific-line-in-text-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
A better solution would be to create a class that has all the settings and then serialize/deserialize it using JSON.
Use nuget to add the JSON package to your app. Example code to serialize: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
add a comment |
A better solution would be to create a class that has all the settings and then serialize/deserialize it using JSON.
Use nuget to add the JSON package to your app. Example code to serialize: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
add a comment |
A better solution would be to create a class that has all the settings and then serialize/deserialize it using JSON.
Use nuget to add the JSON package to your app. Example code to serialize: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
A better solution would be to create a class that has all the settings and then serialize/deserialize it using JSON.
Use nuget to add the JSON package to your app. Example code to serialize: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
answered Nov 14 '18 at 2:30
SSSSSS
4,45711336
4,45711336
add a comment |
add a comment |
An application that hang and an out-of-range exception are 2 different things. Be precise in your question if you want help.
Also, your lineChanger
function would obviously fails if the file you read does not already contains at least 30 lines when you click on button 5.
Hard coded number like that is always a bad idea in production code. If at some point, the file content is modified, it would be very hard to ensure that everything is properly fixed in the code.
If you really want to use such text file, then at the very least:
- use an enumeration or constants define a single place for the purpose of each line.
- use a class to manage your configuration so that only that class knows the structure of the file.
But as mentioned in another answer, it would be preferable to use another format like JSON. Almost every other format except binary data would be preferable to a text file (assuming that the content is settings for your application).
Having said that, if you really want lineChanger
, then you should add a test like that:
if (line_to_edit >= arrLine.Length)
List<string> list(arrLine);
while (List.Length < line_to_edit) list.Add(string.Empty);
list.Add(newText);
arrLine = list.ToArray();
else … old code from question…
However, if you read other lines from the file or often read lines, it might become very slow to open/read/update/write/close the file each time a line is modified. Another reason why it would be a good idea to have a class to manage settings so that you load them once when opening the dialog and once when closing the dialog (assuming that the dialog is displayed a relatively short period of time and it is not critical to save changes immediately).
add a comment |
An application that hang and an out-of-range exception are 2 different things. Be precise in your question if you want help.
Also, your lineChanger
function would obviously fails if the file you read does not already contains at least 30 lines when you click on button 5.
Hard coded number like that is always a bad idea in production code. If at some point, the file content is modified, it would be very hard to ensure that everything is properly fixed in the code.
If you really want to use such text file, then at the very least:
- use an enumeration or constants define a single place for the purpose of each line.
- use a class to manage your configuration so that only that class knows the structure of the file.
But as mentioned in another answer, it would be preferable to use another format like JSON. Almost every other format except binary data would be preferable to a text file (assuming that the content is settings for your application).
Having said that, if you really want lineChanger
, then you should add a test like that:
if (line_to_edit >= arrLine.Length)
List<string> list(arrLine);
while (List.Length < line_to_edit) list.Add(string.Empty);
list.Add(newText);
arrLine = list.ToArray();
else … old code from question…
However, if you read other lines from the file or often read lines, it might become very slow to open/read/update/write/close the file each time a line is modified. Another reason why it would be a good idea to have a class to manage settings so that you load them once when opening the dialog and once when closing the dialog (assuming that the dialog is displayed a relatively short period of time and it is not critical to save changes immediately).
add a comment |
An application that hang and an out-of-range exception are 2 different things. Be precise in your question if you want help.
Also, your lineChanger
function would obviously fails if the file you read does not already contains at least 30 lines when you click on button 5.
Hard coded number like that is always a bad idea in production code. If at some point, the file content is modified, it would be very hard to ensure that everything is properly fixed in the code.
If you really want to use such text file, then at the very least:
- use an enumeration or constants define a single place for the purpose of each line.
- use a class to manage your configuration so that only that class knows the structure of the file.
But as mentioned in another answer, it would be preferable to use another format like JSON. Almost every other format except binary data would be preferable to a text file (assuming that the content is settings for your application).
Having said that, if you really want lineChanger
, then you should add a test like that:
if (line_to_edit >= arrLine.Length)
List<string> list(arrLine);
while (List.Length < line_to_edit) list.Add(string.Empty);
list.Add(newText);
arrLine = list.ToArray();
else … old code from question…
However, if you read other lines from the file or often read lines, it might become very slow to open/read/update/write/close the file each time a line is modified. Another reason why it would be a good idea to have a class to manage settings so that you load them once when opening the dialog and once when closing the dialog (assuming that the dialog is displayed a relatively short period of time and it is not critical to save changes immediately).
An application that hang and an out-of-range exception are 2 different things. Be precise in your question if you want help.
Also, your lineChanger
function would obviously fails if the file you read does not already contains at least 30 lines when you click on button 5.
Hard coded number like that is always a bad idea in production code. If at some point, the file content is modified, it would be very hard to ensure that everything is properly fixed in the code.
If you really want to use such text file, then at the very least:
- use an enumeration or constants define a single place for the purpose of each line.
- use a class to manage your configuration so that only that class knows the structure of the file.
But as mentioned in another answer, it would be preferable to use another format like JSON. Almost every other format except binary data would be preferable to a text file (assuming that the content is settings for your application).
Having said that, if you really want lineChanger
, then you should add a test like that:
if (line_to_edit >= arrLine.Length)
List<string> list(arrLine);
while (List.Length < line_to_edit) list.Add(string.Empty);
list.Add(newText);
arrLine = list.ToArray();
else … old code from question…
However, if you read other lines from the file or often read lines, it might become very slow to open/read/update/write/close the file each time a line is modified. Another reason why it would be a good idea to have a class to manage settings so that you load them once when opening the dialog and once when closing the dialog (assuming that the dialog is displayed a relatively short period of time and it is not critical to save changes immediately).
answered Nov 14 '18 at 3:18
Phil1970Phil1970
1,373189
1,373189
add a comment |
add a comment |
I think your question is too broad and needs more details.
You need to think of separating your form from your data. Create a data class to hold the values you want to be stored or read and have the UI interact with this class using data binding. Then your data class will always contain the most up-to-data information.
When needed, you can serialize your data file with TextStream
, BinaryFormatter
, XmlSerializer
or JSON
, depending on if the data needs to be accessed by other programs or locations. You can even use the built-in Settings
to save the data before the application exits and to be read back in when it starts up again.
So please edit the question to clarify what you want to do with this data and add an example of what it looks like as a whole (all the lines in your text file now). Then we can answer more specifically.
This is great. Will try and update my question and make it more precise
– Jerry Paulman
Nov 14 '18 at 20:37
add a comment |
I think your question is too broad and needs more details.
You need to think of separating your form from your data. Create a data class to hold the values you want to be stored or read and have the UI interact with this class using data binding. Then your data class will always contain the most up-to-data information.
When needed, you can serialize your data file with TextStream
, BinaryFormatter
, XmlSerializer
or JSON
, depending on if the data needs to be accessed by other programs or locations. You can even use the built-in Settings
to save the data before the application exits and to be read back in when it starts up again.
So please edit the question to clarify what you want to do with this data and add an example of what it looks like as a whole (all the lines in your text file now). Then we can answer more specifically.
This is great. Will try and update my question and make it more precise
– Jerry Paulman
Nov 14 '18 at 20:37
add a comment |
I think your question is too broad and needs more details.
You need to think of separating your form from your data. Create a data class to hold the values you want to be stored or read and have the UI interact with this class using data binding. Then your data class will always contain the most up-to-data information.
When needed, you can serialize your data file with TextStream
, BinaryFormatter
, XmlSerializer
or JSON
, depending on if the data needs to be accessed by other programs or locations. You can even use the built-in Settings
to save the data before the application exits and to be read back in when it starts up again.
So please edit the question to clarify what you want to do with this data and add an example of what it looks like as a whole (all the lines in your text file now). Then we can answer more specifically.
I think your question is too broad and needs more details.
You need to think of separating your form from your data. Create a data class to hold the values you want to be stored or read and have the UI interact with this class using data binding. Then your data class will always contain the most up-to-data information.
When needed, you can serialize your data file with TextStream
, BinaryFormatter
, XmlSerializer
or JSON
, depending on if the data needs to be accessed by other programs or locations. You can even use the built-in Settings
to save the data before the application exits and to be read back in when it starts up again.
So please edit the question to clarify what you want to do with this data and add an example of what it looks like as a whole (all the lines in your text file now). Then we can answer more specifically.
answered Nov 14 '18 at 12:12
ja72ja72
18.4k350105
18.4k350105
This is great. Will try and update my question and make it more precise
– Jerry Paulman
Nov 14 '18 at 20:37
add a comment |
This is great. Will try and update my question and make it more precise
– Jerry Paulman
Nov 14 '18 at 20:37
This is great. Will try and update my question and make it more precise
– Jerry Paulman
Nov 14 '18 at 20:37
This is great. Will try and update my question and make it more precise
– Jerry Paulman
Nov 14 '18 at 20:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291744%2fhow-to-write-selected-combobox-and-textbox-text-to-specific-line-in-text-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you debug the code? Which line of code makes the application hang?
– Chetan Ranpariya
Nov 14 '18 at 1:50
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
– Jerry Paulman
Nov 14 '18 at 1:57
1
Then you have an indexing error. I assume it happens in the
lineChanger
method, set a break point and inspect the array and the index you're trying to access.– Vulpex
Nov 14 '18 at 2:24