0

My program uses stacks to check if a programming statement or a formula has balanced parenthesis. everything works except for the life of me I can't seem to find a way to Highlight and unbalanced pair of Parens in the same textbox it was entered into when I push my button to check for Parens.

Here is my code for reference:

private void btnCheckParens_Click(object sender, EventArgs e)
{
    Stack leftParens = new Stack();
    Stack rightParens = new Stack();
    string expression = txtParens.Text;
    string ch;
    int indexOfParens; 
    for ( int i = 0; i < expression.Length; i++)
    {
    ch = expression.Substring(i,1);

    if (isParenthesis(ch))
    {
        if (ch == "(")
            leftParens.Push(ch);
        else
            rightParens.Push(ch);

    }

}
    if (!(leftParens.Count == rightParens.Count))
    {
        if (leftParens.Count > rightParens.Count)
        {
            indexOfParens = expression.LastIndexOf("(");
            txtParens.SelectionStart = indexOfParens;
            txtParens.SelectionLength = 1;
        }
        else
            indexOfParens = expression.LastIndexOf(")");
        txtParens.SelectionStart = indexOfParens;
        txtParens.SelectionLength = 1;
    }
    else
        MessageBox.Show("Number of parens are balanced!","success");



    }

static bool isParenthesis(string ch) { bool flag; if ( ch == "(" || ch == ")") flag = true; else flag =false; return flag; }

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
VanyaS
  • 33
  • 9
  • You might find an answer here http://stackoverflow.com/questions/1380610/checking-string-has-balanced-parentheses – Tim Mar 14 '13 at 05:44
  • So, exactly which part is not working? The part where you detect the parentheses, or the part where you highlight/select the characters in the textbox? – Cody Gray - on strike Mar 14 '13 at 05:44
  • Your intention is to highlight a character in a text box is it? And do you have the character to be highlighted or its index? – A J Mar 14 '13 at 05:46
  • Set the focus to textbox first using txtParens.Focus(); – Fendy Mar 14 '13 at 05:48
  • the part that isnt working is the part where I highlight/select the characters in the textbox. and I have the index of the character. – VanyaS Mar 14 '13 at 05:55
  • Thank you Fendy, That was the problem although I do not understand why that is the problem? – VanyaS Mar 14 '13 at 05:59

1 Answers1

1

Fendy posted the solution in a comment:

Set the focus to textbox first using txtParens.Focus();

The reason that you have to do this is because Windows controls do not (by default) display the current selection unless they have the focus. This applies to textboxes, too.

You can verify this for yourself in the Run dialog. When it is first opened, the "Open" textbox has the focus and any text that it contains is selected and highlighted. However, if you press the Tab key to move the focus to one of the buttons along the bottom, the selection highlight immediately disappears. The text in the textbox is still selected (and will be highlighted again if you Tab back), but the selection is not highlighted because the control has lost the focus.

You can change this behavior by modifying the value of the HideSelection property, which you can either do in the designer using the Properties Window or through code:

txtParens.HideSelection = false;

Setting HideSelection to true uses the default behavior: the selected text does not appear highlighted when the control loses focus. Setting it to false ensures that the selected text always remains highlighted, even when the control loses focus.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • How would I modify my code so that not only do I highlight the unbalanced parens ,but also highlight any number of unbalanced parens? – VanyaS Mar 14 '13 at 06:17
  • @IvanSoria The textbox control does not support discontiguous selections. You can select as many characters as you want, but they all have to be together in a row. The `RichTextBox` control doesn't support this either, but you can simulate it using the [`SelectionBackColor` property](http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionbackcolor.aspx). That would, of course, require replacing your current `TextBox` controls with `RichTextBox` controls, and re-writing all of the affected code. – Cody Gray - on strike Mar 14 '13 at 06:27
  • I've never worked with RichtextBox controls, if it isn't too much would you mind showing me how that would look ? – VanyaS Mar 14 '13 at 06:31