开发者

Problem with button when text entered into the textbox

开发者 https://www.devze.com 2023-03-16 22:59 出处:网络
I am trying to m开发者_C百科ake the save button visible when text is entered into the text box by using the following code:

I am trying to m开发者_C百科ake the save button visible when text is entered into the text box by using the following code:

    if (tbName.TextModified == true)
    {
        btnCTimetablesOk.Visible = true;
    }
     else
    {
        btnCTimetablesOk.Visible = false;
    }

but it gives error at tbname.textmodified

is there any other way to visible the button when we enter the text in text box

this is error i am getting "the event textbox.textmodified can only appear on the left hand side of += or -="


Try using the textbox's Enter and Leave events to show/hide your button:

private void textBox1_Enter(object sender, System.EventArgs e)
{
    btnCTimetablesOk.Visible = true;
}

private void textBox1_Leave(object sender, System.EventArgs e)
{
    btnCTimetablesOk.Visible = false;
}

Then modify your textbox to use these new methods.

Problem with button when text entered into the textbox


If I'm reading your text correctly, you want the save button to be visible when the textbox has text in it and invisible when the text box is blank. If that's the case, you can use the Leave event (which occurs when the textbox loses focus) and a simple if statement:

private void textBox1_Leave(object sender, System.EventArgs e)
{
  if(textBox1.Text != "")
    btnCTimetablesOk.Visible = true;
  else
    btnCTimetablesOk.Visible = false;
}

You can also put this conditional block in any other methods kicked off by events that change the text of the box.

Also, you might want to consider using Enabled instead of Visible, it'll leave the button on the form but will gray out the text and clicking will do nothing.


I'm going to take a stab in the dark here and assume that the button is related to the textbox and you probably want someone to be able to type something in the textbox then click the button. You probably don't want the user to have to type something, then tab out or click somewhere else to make the button visible then click the button.

tbName_TextChanged(object sender, EventArgs e)
{
    btnCTimetablesOk.Visible = !String.IsNullOrEmpty(tbName.Text)
}

Btw you're getting that error because TextModified isn't a boolean property, it's an event, like TextChanged or Leave or Enter. You can assign an event handler to it but you can't just check it like that.

As an aside I personally hate systems hungarian for winforms controls. I'd much rather have a timetablesOkButton than a btnCTimeablesOK button. That way if you also have a timetablesNameTextBox you can see at a glance that the button and the textbox match. Of course it may not be up to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号