0

I have a TextBox in my . When a user starts typing in it I will like to set the AcceptButton property to call another function. However it is calling another function which is called by a Button in my ToolStrip. To elaborate, here is my code below:

private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
    this.AcceptButton = searchBtn;
}

private void searchBtn_Click_1(object sender, EventArgs e)
{
    if (locNameTxtBx.Text != "")
    {
        List<SearchLocation> locationsArray = new List<SearchLocation>();
        var location = locNameTxtBx.Text;
        SearchLocation loc = new SearchLocation();
        loc.Where = location;
        locationsArray.Add(loc);
        mapArea.VE_FindLocations(locationsArray, true, true, null);
        mapArea.VE_SetZoomLevel(14);
    }
    else
    {
        MessageBox.Show("Please Enter Location");
    }
}

searchBtn is a Button in the ToolStrip. So, when I try to run this code, I get this error

Cannot implicitly convert type 'System.Windows.Forms.ToolStripButton' to 'System.Windows.Forms.IButtonControl'. An explicit conversion exists (are you missing a cast?)

I have tried casting it as a ToolstripButton like this:

private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
    this.AcceptButton = (ToolStripButton)searchBtn;
}
Abhishek
  • 2,925
  • 4
  • 34
  • 59
floormind
  • 1,868
  • 5
  • 31
  • 85
  • It says a tool strip button is not a compatible type with an accept button. – Cole Tobin Dec 07 '12 at 15:32
  • does that mean this cant be done ? @ColeJohnson – floormind Dec 07 '12 at 15:33
  • Why not just define a function with the code in it and have the buttons both call it. – Cole Tobin Dec 07 '12 at 15:34
  • See this question http://stackoverflow.com/questions/2524753/what-does-the-c-sharp-compiler-mean-when-it-prints-an-explicit-conversion-exist – Steve Dec 07 '12 at 15:36
  • @ColeJohnson i think the last guy who edited my question made it out to be as i wish for 2 buttons to call one method.. what i really want to do is assign the acceptbutton property to the textbox i have just edited the question again. – floormind Dec 07 '12 at 15:40

1 Answers1

0

You could use two delegates and set the delegate to use in the locNameTxtBx_TextChanged method.

    private delegate void ToUseDelegate();

    ToUseDelegate delegateIfNoText = delegate{
       MessageBox.Show("Please Enter Location");
    }

    ToUseDelegate delegateIfText = delegate{
       List<SearchLocation> locationsArray = new List<SearchLocation>();
       var location = locNameTxtBx.Text;
       SearchLocation loc = new SearchLocation();
       loc.Where = location;
       locationsArray.Add(loc);
       mapArea.VE_FindLocations(locationsArray, true, true, null);
       mapArea.VE_SetZoomLevel(14);
    }

    ToUseDelegate delToUse = delegateIfNoText;

    private void locNameTxtBx_TextChanged(object sender, EventArgs e)
    {
       this.AcceptButton = searchBtn;
       if (locNameTxtBx.Text != ""){
          delegateToUse = delegateIfNoText;
       } else {
          delegateToUse = delegateIfText;
       }
    }

    private void searchBtn_Click_1(object sender, EventArgs e)
    {
       delegateToUse();
    }
TryinHard
  • 4,078
  • 3
  • 28
  • 54
Tobias
  • 2,945
  • 5
  • 41
  • 59