} // end tbInfix_Enter /// <summary> /// Attempts to convert text that is in infix expression text box to postfix expression /// </summary> /// <param name="sender">The generate postfix button</param> /// <param name="e">The event instance containing information about the event</param> private void btnGenerate_Click(object sender, EventArgs e) { string infix = tbInfix.Text; Postfix newPostfix = new Postfix(infix); tbPostfix.Text = newPostfix.PostfixExpression; } // end btnGenerate_Click
} // end InfixToPostfixForm_Load #endregion #region Events /// <summary> /// Allows user to open a text file with infix expressions and convert to postfix notation /// </summary> /// <param name="sender">The open file menu option</param> /// <param name="e">The event instance containing the event data</param> private void openInfixDataFileToolStripMenuItem_Click(object sender, EventArgs e) { string path = string.Empty; // Setup an open file dialog OpenFileDialog dlg = new OpenFileDialog( ); dlg.Filter = "text files|*.txt;*.text|all files|*.*"; dlg.InitialDirectory = @"..\..\InfixFiles"; dlg.Title = "Select a Text File with Infix Expressions to Convert"; // Try to get the text file from the dialog if (dlg.ShowDialog( ) == DialogResult.OK) { path = dlg.FileName; } else { MessageBox.Show(this, "No file selected. You must select a text file containing " + "infix expressions to use the Infix to Postfix program.", "No File Selected", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } StreamReader rdr = null; string line = string.Empty; try { rdr = new StreamReader(path); // Read data until end of file while (rdr.Peek( ) != -1) { line = rdr.ReadLine( ); // Create postfix expressions Postfix newPostfix = new Postfix(line); postfixes.Add(newPostfix); } } catch (Exception) { MessageBox.Show(this, "File could not be opened. Please select a valid text " + "file containing infix expressions.", "Error Opening File", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { if (rdr != null) { rdr.Close( ); } } // Update infix expression list box foreach (Postfix pf in postfixes) { listBoxInfix.Items.Add(pf.InfixExpression); } listBoxInfix.Enabled = true; listBoxInfix.SelectedIndex = 0; } // end openInfixDataFileToolStripMenuItem_Click