Sir/Ma'am
Have a question regarding arrays. I will do my best to explain what I'm trying to do. Let's stay you have a text file (test.txt) with the following format:
,
--- 1 2 3 4 5 6
,
100 101 102 103 104 105 106
200 201 202 203 204 205 206
300 301 302 303 304 305 306
Now you have a form with three textboxes and a button; the intent is for the user to enter numbers into textboxes 1 and 2 and pushes the button. For this example we will say the user inputs 300 into Textbox1 and 3 into textbox2. Upon pushing the button I would like textbox3 to return a value of 303. Example 2. The user enter 100 into Textbox1 and 6 into texbox2, clicks a button and textbox3 is populated with 106. I hope this makes sense to those of you kind enough to assist. Is something like this possible with C# using streamreader? I was able to come up with the following code, but it only reads a single line and isn't very dynamic. Obviously what I'm trying to do far exceeds my skill and would appreciate some assistance.
// Read the file and display it line by line.
System.IO.
StreamReader file = new System.IO.StreamReader(@"C:\test.txt");
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line); string[] lineStrings = line.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries); if (lineStrings.Length > 0)
{
if (lineStrings[0] == textBox1.Text)
{
textBox3.Text = lineStrings[1];
if (lineStrings[0] == textBox1.Text)
{
textBox3.Text = lineStrings[1];
Console.ReadLine();
Danatas GerviPosted Sep 16, 2009, 11:35 AM
public Form1()
{
InitializeComponent();
// string getted from file (for example)
//string stringFromFile = "100 101 102 103 104 105 106,200 201 202 203 204 205 206,300 301 302 303 304 305 306";
string stringFromFile = File.ReadAllText("c:\\test.txt");
m_Matrix = new Dictionary<string, string[]>();
// comma separator of rows
string[] matrixRows = stringFromFile.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string mRow in matrixRows)
{
// spase separator of sells
string [] matrixCells = mRow.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
m_Matrix.Add(matrixCells[0], matrixCells); // adding the key and all array
// let say, that our data can be long (many rows, but will not be wide - only 7 columns (not 1000)
}
}
is the simplest way.
Danatas GerviPosted Sep 22, 2009, 6:54 AM
Sabrina, it was the code sample for my other post. So I was checking the sytaxis.
matthew kingPosted Sep 16, 2009, 11:23 AM
I will still need to read the text file into the Array...right?
Danatas GerviPosted Sep 16, 2009, 8:14 AM
Not a big deal.
:-)
namespace SelectFromMatrixString
{
public partial class Form1 : Form
{
// string getted from file (for example)
string m_StringFromFile = "100 101 102 103 104 105 106,200 201 202 203 204 205 206,300 301 302 303 304 305 306";
Dictionary<string, string[]> m_Matrix;
public Form1()
{
InitializeComponent();
m_Matrix = new Dictionary<string, string[]>();
// comma separator of rows
string[] matrixRows = m_StringFromFile.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string mRow in matrixRows)
{
// spase separator of sells
string [] matrixCells = mRow.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
m_Matrix.Add(matrixCells[0], matrixCells); // adding the key and all array
// let say, that our data can be long (many rows, but will not be wide - only 7 columns (not 1000)
}
}
private void button1_Click(object sender, EventArgs e)
{
// getting data from user input
string rowKey = textBox1.Text;
string columnNumber = textBox2.Text;
string[] selectedRow = m_Matrix[rowKey];
if (selectedRow != null) // something found
{
int ColNumber;
int.TryParse(columnNumber, out ColNumber);
if (ColNumber < selectedRow.Length) // nessesary checking!
{
textBox3.Text = selectedRow[ColNumber];//displaying the result.
}
}
}
}
}
In this example I have ignored you column numbers – 1,2,3…
May be the keys also should be ignored? If so, you should generate the unique key instead of getting it from matrixCells[0].
matthew kingPosted Sep 15, 2009, 8:26 PM
I probably did a poor job of explaining what I'm trying to accomplish. The numbers I used were purely arbitrary and require more than simple math. What I'm trying to accomplish is have the user enter a fixed value into Textbox1 and Textbox2, then push a button. Textbox one will be used to select the appropriate column, and textbox2 will select the row to obtain a value. Not sure how to accomplish this.
Danatas GerviPosted Sep 15, 2009, 9:23 AM
Why you cannot put to textBox3 simple sum of two values? (Algorithm to implement "100 and 6 result is 106" this is "+")
Why continues number should be in array?
What to do, if 106 or 303 do not exist in array?
What to do, if user inserted 333 and 12?