Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 如何设置toolstriptextbox accept按钮来调用toolstripbutton调用的函数?_Winforms_C#_Textbox_Toolstripbutton_Acceptbutton - Fatal编程技术网

Winforms 如何设置toolstriptextbox accept按钮来调用toolstripbutton调用的函数?

Winforms 如何设置toolstriptextbox accept按钮来调用toolstripbutton调用的函数?,winforms,c#,textbox,toolstripbutton,acceptbutton,Winforms,C#,Textbox,Toolstripbutton,Acceptbutton,我有一个文本框。当用户开始输入时,我想设置AcceptButton属性来调用另一个函数。但是,它正在调用另一个函数,该函数由我的ToolStrip中的按钮调用。下面是我的代码: private void locNameTxtBx_TextChanged(object sender, EventArgs e) { this.AcceptButton = searchBtn; } private void searchBtn_Click_1(object sender, EventArgs

我有一个
文本框
。当用户开始输入时,我想设置
AcceptButton
属性来调用另一个函数。但是,它正在调用另一个函数,该函数由我的
ToolStrip
中的
按钮调用。下面是我的代码:

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");
    }
}

您可以使用两个委托,并将委托设置为在
locNameTxtBx\u TextChanged
方法中使用

    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();
    }
private delegate void-ToUseDelegate();
TOUSEDLEGATE delegateIfNoText=委托{
MessageBox.Show(“请输入位置”);
}
tuseDelegate delegateIfText=委托{
列表位置数组=新列表();
var location=locNameTxtBx.Text;
SearchLocation loc=新的SearchLocation();
loc.Where=位置;
地点补充(loc);
mapArea.VE_FindLocations(locationsArray,true,true,null);
mapArea.VE_SetZoomLevel(14);
}
ToUseDelegate delToUse=delegateIfNoText;
私有void locNameTxtBx_TextChanged(对象发送方,事件参数e)
{
this.AcceptButton=searchBtn;
如果(locNameTxtBx.Text!=“”){
delegateouse=delegateIfNoText;
}否则{
delegateouse=delegateIfText;
}
}
私有无效搜索点击1(对象发送者,事件参数e)
{
授权的();
}

它说工具条按钮与接受按钮不兼容。这是否意味着不能这样做@ColeJohnson为什么不定义一个包含代码的函数,让两个按钮都调用它呢?看这个问题@ColeJohnson我想最后一个编辑我的问题的人得出的结论是,我希望两个按钮调用一个方法。。我真正想做的是将acceptbutton属性分配给我刚刚再次编辑问题的文本框。
    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();
    }