英文:
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 = $"{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) { }
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论