System.AccessViolationException: Attempted to read or write protected memory.

i get this exception when i change a multiline textbox to single line:
Visual studio actually throws the exception at the Program.cs at the Application.Run(new Form1()); line, but i found that the issue is caused by the line valTxtBox.Multiline = false; because if i remove it i dont get the exception
What is going on here?
cs 
valTxtBox.TextChanged += (_, _2) =>
{
    if (valTxtBox.Text.Contains('\n'))
    {
        disableTxtBoxAutoClose = true;
        valTxtBox.Multiline = true;
        valTxtBox.Height = 20 + 100;
        valTxtBox.Focus();
        disableTxtBoxAutoClose = false;
    }
    else
    {
        disableTxtBoxAutoClose = true;
        valTxtBox.Multiline = false; <<-PROBLEM CAUSED BY THIS?
        valTxtBox.Focus();
        valTxtBox.Text = boxText;
        disableTxtBoxAutoClose = false;
    }
Was this page helpful?