Diamonddrake 397 Master Poster

It seems that the error always occurs on a line that consists of my main richtextbox control, and occasionally visual studio creates a listing for it in its autos, section that says there was an accessviolation to the rich text box control. anyhow here is the code.

private void linesAlphabeticallyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            statusBarText.Text = "Sorting Alphabetically, Please be Patient.";
        //    ProgressBar.Visible = true;
            statusBar.Refresh();
        //    ProgressBar.Value = 0;
        //    ProgressBar.Maximum = getLineCount(txtMain.Text, false) * 2;

            myundo.CheckPoint(txtMain.Text);

            //  List<String> myalphalines = txtMain.Text.Split('\n').ToList();
            //  myalphalines.Sort();
            //  txtMain.Text = String.Join("\n", myalphalines.ToArray());

            List<string> myLines = new List<string>();
            using (StringReader r2 = new StringReader(txtMain.Text))
            {
                string txtLine;
                while ((txtLine = r2.ReadLine()) != null)
                {
                    myLines.Add(txtLine);
               //     ProgressBar.Value += 1;
                }
            }
            myLines.Sort();
            using (StringWriter sw = new StringWriter())
            {
                foreach (string s in myLines)
                {
                    sw.WriteLine(s);
              //      ProgressBar.Value += 1;
                }
                txtMain.Text = sw.ToString();
                txtMain.Modified = true;
             //   ProgressBar.Visible = false;
                statusBarText.Text = "";
            }

            myLines = null;
            if (Settings.Default.showLineCount == true)
                this.currentLineCountSSL.Text = getLineCount(txtMain, false).ToString();


            Cursor.Current = Cursors.Default;


        }

        

        private void linesAlphToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            myundo.CheckPoint(txtMain.Text);

            statusBarText.Text = "Sorting Alphanumerically, Please be Patient.";
     //       ProgressBar.Visible = true;
            statusBar.Refresh();
      //      ProgressBar.Value = 0;
       //     ProgressBar.Maximum = getLineCount(txtMain.Text, false) * 2;


            List<string> myANLines = new List<string>();

            NumericComparer ns = new NumericComparer();

           using (StringReader rAn = new StringReader(txtMain.Text))
            {
                string txtLine;
                while ((txtLine = rAn.ReadLine()) != null)
                {
                    myANLines.Add(txtLine);
     //               ProgressBar.Value += 1;
                }
            }
            string[] sortArray = myANLines.ToArray();
            myANLines = null;

          //  string[] sortArray = txtMain.Text.Split('\n').ToArray(); …
Diamonddrake 397 Master Poster

I am not following you with that statement.

and I have finished the program. It is fully functional, with the exception of frequent crashes for the outOfMemoryException that gets thrown.

it never gave me any trouble until I added a progress bar on the statusstrip for the sorting methods. might there be anything related to that?

Diamonddrake 397 Master Poster

as it stands, one function adds the lines to a list, and calls the list<string>.Sort(); method. another adds the lines to a list, converts the list to an array, and sorts the array using a custom Icomparer class, not neccessary i believe, I think the list.sort method accepts an Icomparer argument. I will look into that. but as for my most critical sort method. I add all the lines to one list, then I add only the selected characters from the first line, and matching colums of characters for each additional line to another list, then I convert both lists to an array using .toArray(); and then sort the array using the second one as a key.

is there a better way to do that? i tried using an arraylist, but its has to be converted to an array to be sorted as well, at least through the methodology that I have studied.

also
I use the "using" method to create my string readers and writers to ensure that they go out of scope and are destroyed

is there something more I should do?

Diamonddrake 397 Master Poster

I have written a text editor that sorts lines, I plan to use it with some large files. say about a million lines, ranging between 10 and 75 MB. I am currently testing my app using about a 50mb file with about 900,000 lines. the program adds all the lines from the text box to an array and sorts them, then writes them back, sorted, to the text box. it also copies the text box to a stack for an undoer. other sort methods in my program create multiple arrays and use one as a key to sort the other, and the slowest sorting method using an IComparer class to sort in natural order.

my dilemma is that I get OutOfMemoryExceptions frequently. I have read up the best I could and I don't understand what to do about it. all I can find is something about the compact framework and 64K methods, but Im not using the compact framework. is there something I can do to better handle the large files. is there a better way to implement and undoer, or would dropping the undoer really fix the problem?

what is there to do about this error?

Diamonddrake 397 Master Poster

There is a lot of drama to it. after lost of msdn research it turns out that they are both registry entries, but they are in different places, and more over, vista requires a that a special manifest be created to prompt the user account control to ask the user( if they are an admin) to temporarily up the user level to actual administrator rights. that example doesn't work in vista, does in xp though. And it seems that although you can (after jumping through hoops) set vista file associations programmatically, you can't unassociate them programmatically, or at least it doesn't appear possible through my research.

any documentation or examples to prove my research wrong would be a blessing.

Diamonddrake 397 Master Poster

Thanks, Awesome app btw.

I searched the registry and found that there was an entry with the name of my app pointing to the wrong file path, and for some reason the file association wizard wouldn't update it.

It works well now.

I am wondering though, I would like to set file associations programmatically. I understand how to do it in XP, but it doesn't work in vista. I can't find any good tutorials on vista file associations through C#.

any information on that one?
I may just have to start a new thread....

Thanks for the help.

Diamonddrake 397 Master Poster

I did leave a little piece out, but I rewrote it using a different method. It turns out that the code would have worked the way it was, windows vista screwed up my associations. even though I select for windows to open a text file with my .exe file, vista attached its self to one instance of it and copied it to the app data hidden user folder. so even though i fixed the code, several times, using multiple methods that should have worked. vista was opening the wrong executable. one i deleted the one vista made, it went crazy. now, even if I publish the program completely and install it with an installer, vista refuses to even try and associate files to my program. even through the control panel applet. does anyone know how to fix that?

and what i settled on doing, although i don't really see any benefit to it, is checking to see if there is any arguments at the entry point and then having 2 constructors for my main form, one with no parameters, and one with a string parameter that contains the file name. I am not 100% sure works correctly, i have to test it on another computer.

any clue about the vista's file association applet refusing my program?

Diamonddrake 397 Master Poster

string[] args = Environment.GetCommandLineArgs();

Im not sure the usage of this. it appears to set the command line arguments to an array of strings called args, isn't that what the overloaded Main method does? I guess I could write a loop to print all the strings in the array to the output pannel and see what windows is doing. I will try that. but I still am not sure about this string[] args = Environment.GetCommandLineArgs(); code. is there a benifit to it over the overladed main method?

thanks,

Diamonddrake 397 Master Poster

The title is confusing, I know.
What I mean is, I created a note pad (like) program with line sorting functions. I want to be able to associate .txt files in explorer with the program. Currently I modified the Main function to accept command like arguments. and I modified my constructor for my main form to load the 1st command line argument as a file name, and open it. so if you drag and drop a file onto the icon, it opens. and works. but if you associate the txt file with the program, the program opens, but the document doesn't. I can't find any articles on this, because Im not sure exactly what to search for.

just for clarification. here is my code.

here's the Entry point

static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new fclsMain(args));
        }
    }

here is the overloaded constructor

public fclsMain(string[] args)
        {
            InitializeComponent();
            if (args.Length == 0)
            {
                currentFile = "";
                this.Text = "SortPad: New Document";
            }
            if (args.Length != 0)
            {
                {
                    currentFile = args[0];

                    System.IO.StreamReader txtReader;
                    txtReader = new System.IO.StreamReader(currentFile);
                    txtMain.Text = txtReader.ReadToEnd();
                    txtReader.Close();
                    txtReader = null;
                    txtMain.SelectionStart = 0;
                    txtMain.SelectionLength = 0;


                    txtMain.Modified = false;
                    FileInfo file = new FileInfo(currentFile);

                    this.Text = "SortPad: " + file.Name;
                    }

                       catch {
                           currentFile = "";
                           this.Text = "SortPad: New Document";

                       }


                }
            }
           
        }

What am I doing wrong?
Is there something that windows adds to the argument when opening a document?

Diamonddrake 397 Master Poster

Awesome!
I wonder how to make this fire a certain intervals, for example on the press of the space bar, or something. hrm.

Diamonddrake 397 Master Poster

the code you posted creates a stream reader object and uses it to add each line of the system.ini settings file to a list.

What is your goal? Elaborate.

Diamonddrake 397 Master Poster

I found the answer,
basically I created 2 arrays, I read each line of text from my text box, copied it in its entirety into my first array, then I used a substring to find just the chosen column and copied that character to the 2nd array, then i called the Array.sort(array, array); method sorting the substring array, then wrote the array with the full lines to the text box not nice and sorted by the selected substing of each line, simple, Very efficient, and perfect.

Thanks for your suggestions LizR, but I guess I just needed to better investigate the usage of the methods I already knew about.

This thread is sloved.

Diamonddrake 397 Master Poster

i looked into using a dictionary, but I couldn't seem to taper its usage to match my intentions. and I don't know where to begin on writing my own sort routine. I am working on a method to compare substring values in an array containing all the lines. i will post some code when I have something that makes any sense at all.

any pointers or suggestions would be appreciated.
Thanks

Diamonddrake 397 Master Poster

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Substring(0,4) == "$GSV")
{
Textbox1.Test = line.
}
}
}

I Know this is an old post, and I'm sure the problem has been handled, but actually a better way would not be so save it to a file and then re open it with streamReader, the original question said they started with the text in a text box, so best would be a stringreader

using (StringReader sr = new StringReader(Textbox1.Text))
   { string line;
      while((line =sr.ReadLine())!= null)
        {if (line.Substring(0,4) == "$GSV")
          {
              Textbox2.Text = line;
          }
         }
    }
Diamonddrake 397 Master Poster

It does work, but it sorts by the entire line, Lets say I have a txt file like this:

110125614, 55614, 1289489 685481 685489 3 35478956
694894894, 85989, 7478498 651856 547878 1 68994898
547846514, 35247, 5248948 658748 848655 2 68489894
524754754, 32348, 5856445 489585 598788 0 91156419

I would like the sort function to ignore all the characters except one a specific number of characters from the margin for example the bold character in my example above so that the output would be:

524754754, 32348, 5856445 489585 598788 0 91156419
694894894, 85989, 7478498 651856 547878 1 68994898
547846514, 35247, 5248948 658748 848655 2 68489894
110125614, 55614, 1289489 685481 685489 3 35478956

see, I would like the lines not to change, just be ordered my a certain character in each line and I'm lost at how to go about it.

Diamonddrake 397 Master Poster

I have Gotten this far, I can put each line in order alphanumerically, but its the entire line, If anyone could point out how I could make the lines order alphanumerically by a specific character from the left margin, or at least point me in the right direction I would appreciate it. Thanks.

List<string> myLines = new List<string>();
            using (StringReader r2 = new StringReader(txtMain.Text))
            {
                string txtLine;
                while ((txtLine = r2.ReadLine()) != null)
                {
                    myLines.Add(txtLine);
                }
            }
            myLines.Sort();
            using (StringWriter sw = new StringWriter())
            {
                foreach (string s in myLines)
                {
                    sw.WriteLine(s);
                }
                txtMain.Text = sw.ToString();
            }
Diamonddrake 397 Master Poster

Does anyone remember how ms dos had a sort program that you could sort the rows in a huge text document based on just one character by its position from the left margin?

I am trying to recreate that effect in a notepad clone. I am sort of new to C# but not to programming, a friend of mine works as a repair man at a factory and the jobs his machines run are in text database files, that each new line is a row of data, one of the values is a number representing rather or not that row's data was ran and accepted, I would like to write a simple program sort function that would sort the rows by that one value so all the unaccepted rows would all go to the top, without affecting the text in the row. so that that text could be easily cut and pasted in a new document to be ran by the machines again.

sorry for the long back story,

I found some code that is suppose to read a file and sort it using a list view, but that's not exactly what I am going for, and I couldn't get it to work right.

I am willing to do whatever necessary but so far trial and error just hasn't done anything for me.

thanks,
DiamondDrake