How can I change the DropDown state of a user control to false when clicking outside of the control in C#?

huangapple go评论55阅读模式
英文:

How can I change the DropDown state of a user control to false when clicking outside of the control in C#?

问题

C#表单中,我有一个用户控件。这个用户控件有一个可以为true或false的DropDown状态。我希望在用户控件外部单击时将DropDown状态更改为false

我的想法是像这样做:

```cs
[![在这里输入图像描述](https://i.stack.imgur.com/VJJdO.png)](https://i.stack.imgur.com/VJJdO.png)

我的代码:

using Common.Cache;
using Common.Helpers;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Presentation.UI.Components
{
    public partial class ComboBoxSearchVendor : UserControl
    {
        private bool _IsDroppedDown = false;
        private string _SelectedValue = null;

        public ComboBoxSearchVendor()
        {
            InitializeComponent();
            Height = TBToggle.Height;
            Theme.DataGridView(Data);
            Data.DataSource = VendorCache.vendors;
            BringToFront();
        }

        public bool IsDroppedDown
        {
            get { return _IsDroppedDown; }
            set { _IsDroppedDown = value; }
        }

        public string SelectedValue
        {
            get { return _SelectedValue; }
            set {
                _SelectedValue = value;
                if (_SelectedValue != null)
                {
                    var vendor = VendorCache.Find(_SelectedValue);
                    if (vendor != null) TBToggle.Text = $"{vendor.Ruc} - {vendor.BusinessName}";
                }
            }
        }

        private void DropDown_Click(object sender, EventArgs e)
        {
            DropDownAndUp();
            TBSearch.Focus();
        }

        private void DropDownAndUp()
        {
            _IsDroppedDown = !_IsDroppedDown;
            if (_IsDroppedDown)
            {
                Height = 200;
                DropDownIcon.IconChar = FontAwesome.Sharp.IconChar.ChevronUp;
                DropDownIcon.BackColor = Color.AliceBlue;
                TBToggle.BackColor = Color.AliceBlue;
            }
            else
            {
                Height = TBToggle.Height;
                DropDownIcon.IconChar = FontAwesome.Sharp.IconChar.ChevronDown;
                DropDownIcon.BackColor = Color.White;
                TBToggle.BackColor = Color.White;
            }
        }

        private void TBSearch_KeyUp(object sender, KeyEventArgs e)
        {
            Data.DataSource = VendorCache.Filter(TBSearch.Text);
        }

        private void Data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow Row = Data.CurrentRow;
                _SelectedValue = Row.Cells["Ruc"].Value.ToString();
                TBToggle.Text = $"{Row.Cells["Ruc"].Value} - {Row.Cells["BusinessName"].Value}";
                TBSearch.Clear();
                DropDownAndUp();   
            } catch (Exception ex)
            {
                Helpers.Log(this, ex);
            }
        }

        private void Data_DataError(object sender, DataGridViewDataErrorEventArgs e) { }
    }
}

在用户控件的设计文件中订阅了事件。

我尝试添加Leave事件,但不起作用:

private void ComboBoxSearchVendor_Leave(object sender, EventArgs e)
{
   if (!ContainsFocus)
       IsDroppedDown = false;
}

<details>
<summary>英文:</summary>
I have a user control within a C# form. This user control has a DropDown state that can be true or false. I want it to change the DropDown state to false when clicked outside of the user control.
My idea is to do something like this:
[![enter image description here](https://i.stack.imgur.com/VJJdO.png)](https://i.stack.imgur.com/VJJdO.png)
My code:
```cs
using Common.Cache;
using Common.Helpers;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Presentation.UI.Components
{
public partial class ComboBoxSearchVendor : UserControl
{
private bool _IsDroppedDown = false;
private string _SelectedValue = null;
public ComboBoxSearchVendor()
{
InitializeComponent();
Height = TBToggle.Height;
Theme.DataGridView(Data);
Data.DataSource = VendorCache.vendors;
BringToFront();
}
public bool IsDroppedDown
{
get { return _IsDroppedDown; }
set { _IsDroppedDown = value; }
}
public string SelectedValue
{
get { return _SelectedValue; }
set {
_SelectedValue = value;
if (_SelectedValue != null)
{
var vendor = VendorCache.Find(_SelectedValue);
if (vendor != null) TBToggle.Text = $&quot;{vendor.Ruc} - {vendor.BusinessName}&quot;;
}
}
}
private void DropDown_Click(object sender, EventArgs e)
{
DropDownAndUp();
TBSearch.Focus();
}
private void DropDownAndUp()
{
_IsDroppedDown = !_IsDroppedDown;
if (_IsDroppedDown)
{
Height = 200;
DropDownIcon.IconChar = FontAwesome.Sharp.IconChar.ChevronUp;
DropDownIcon.BackColor = Color.AliceBlue;
TBToggle.BackColor = Color.AliceBlue;
}
else
{
Height = TBToggle.Height;
DropDownIcon.IconChar = FontAwesome.Sharp.IconChar.ChevronDown;
DropDownIcon.BackColor = Color.White;
TBToggle.BackColor = Color.White;
}
}
private void TBSearch_KeyUp(object sender, KeyEventArgs e)
{
Data.DataSource = VendorCache.Filter(TBSearch.Text);
}
private void Data_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
DataGridViewRow Row = Data.CurrentRow;
_SelectedValue = Row.Cells[&quot;Ruc&quot;].Value.ToString();
TBToggle.Text = $&quot;{Row.Cells[&quot;Ruc&quot;].Value} - {Row.Cells[&quot;BusinessName&quot;].Value}&quot;;
TBSearch.Clear();
DropDownAndUp();   
} catch (Exception ex)
{
Helpers.Log(this, ex);
}
}
private void Data_DataError(object sender, DataGridViewDataErrorEventArgs e) { }
}
}

Events are subscribed to in the user control's design file.

I trying add the Leave event, but not working:

private void ComboBoxSearchVendor_Leave(object sender, EventArgs e)
{
   if (!ContainsFocus)
       IsDroppedDown = false;
}

答案1

得分: 1

尝试从相同的表单中进行操作,订阅用户控件的离开事件。

英文:

Try to do it from the same form, subscribe to the Leave event of the User Control.

huangapple
  • 本文由 发表于 2023年5月30日 05:41:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定