gpt4 book ai didi

C# 自定义组合框 - 下拉位置

转载 作者:太空狗 更新时间:2023-10-30 00:48:18 25 4
gpt4 key购买 nike

我正在使用 ToolStripControlHostToolStripDropDown 创建一个 ComboBox 控件,它可以在 DropDown 窗口中承载任何类型的控件。例如,下拉窗口可能会显示 ListView 或 TreeView ,甚至是另一个用户控件。

我在下面发布了一个简化的代码,其中下拉列表托管一个带有 ListView 和按钮的用户控件,如下所示:

enter image description here

当控件位于屏幕底部时,下拉窗口将外推屏幕的下边界,就会出现问题。发生这种情况时,下拉菜单最终会隐藏控件。

在这种情况下,我想修复 _dropDown.Show 方法调用以显示下拉窗口,如下所示:

enter image description here

要重复这个问题,只需运行下面的代码并将窗口拖到屏幕底部并打开下拉菜单。

using System;
using System.Windows.Forms;

public class CustomComboBox : UserControl
{
ToolStripDropDown _dropDown;

public CustomComboBox()
{
var textbox = new TextBox();
textbox.Location = new System.Drawing.Point(0, 0);
textbox.Size = new System.Drawing.Size(this.Width - 22, 20);
textbox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
this.Controls.Add(textbox);

var button = new Button();
button.Location = new System.Drawing.Point(this.Width - 22, -1);
button.Size = new System.Drawing.Size(22, 22);
button.Text = "\u2BC6";
button.Anchor = AnchorStyles.Right | AnchorStyles.Top;
button.Click += new System.EventHandler(this.Button_Click);
this.Controls.Add(button);

var dropDownControl = new DropDownControlTest();

var controlHost = new ToolStripControlHost(dropDownControl);

_dropDown = new ToolStripDropDown();
_dropDown.AutoSize = true;
_dropDown.Items.Add(controlHost);
}

void Button_Click(object sender, EventArgs e)
{
_dropDown.Show(this, 0, this.Height);
}
}

public class DropDownControlTest : UserControl
{
public DropDownControlTest()
{
var listview = new ListView();
listview.Location = new System.Drawing.Point(3, 1);
listview.Size = new System.Drawing.Size(400,300);
listview.View = View.Details;
listview.Columns.Add("Col 1",100);
listview.Columns.Add("Col 2",100);
this.Controls.Add(listview);

var button = new Button();
button.Location = new System.Drawing.Point(3, 305);
button.Text = "More...";
this.Controls.Add(button);
}
}

public class Form1 : Form
{
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

public Form1 ()
{
CustomComboBox ccBox = new CustomComboBox();
ccBox.Location = new System.Drawing.Point(10, 10);
ccBox.Height = 20;

this.Text = "Test CustomComboBox";
this.Controls.Add(ccBox);
}
}

最佳答案

您可以使用 ToolStripDropDown.Show Method (Control, Point, ToolStripDropDownDirection)过载以控制下降方向。该代码将需要执行边界检查来决定是将下拉列表放在文本框上方还是下方。

以下是进行边界检查的简单方法,并且仅在单个屏幕配置上进行了测试。

首先,使 textbox 成为类级别的变量。

private TextBox textbox;
public CustomComboBox()
{
//var textbox = new TextBox();
textbox = new TextBox();

显示逻辑如下。

void Button_Click(object sender, EventArgs e)
{
Point textBoxScreenLocation = textbox.PointToScreen(textbox.Location);

// try to position _dropDown below textbox
Point pt = textBoxScreenLocation;
pt.Offset(0, textbox.Height);

// determine if it will fit on the screen below the textbox
Size dropdownSize = _dropDown.GetPreferredSize(Size.Empty);
Rectangle dropdownBounds = new Rectangle(pt, dropdownSize);

if (dropdownBounds.Bottom <= Screen.GetWorkingArea(dropdownBounds).Bottom)
{ // show below
_dropDown.Show(pt, ToolStripDropDownDirection.BelowRight);
}
else
{ // show above
_dropDown.Show(textBoxScreenLocation, ToolStripDropDownDirection.AboveRight);
}
}
}

关于C# 自定义组合框 - 下拉位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48087494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com