c# - 当 DropDownStyle 为 Simple 时,ToolStripCombobox 显示在屏幕左上角

标签 c# winforms toolstrip toolstripdropdown toolstripcombobox

我有一个 ToolStripCombobox,当我将其 DropDownStyle 设置为 Simple 时。我第一次打开菜单时,它显示在屏幕的左上角。 但是,当我第二次选择同一项目时,它会显示在正确的位置。

有没有办法阻止代码在屏幕左上角显示列表?

预先感谢您的帮助。

第一次

Result 1

第二次

Result 2

最佳答案

要解决这个问题,请将此代码放入以下形式的 Load 事件中:

var item = toolStripComboBox1;
var createControl = item.Control.Parent.GetType().GetMethod("CreateControl",
    System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
createControl.Invoke(item.Control.Parent, new object[] { true });

这是一个奇怪的错误,我不知道为什么 DisplayStyle 设置为 SimpleToolStripComboBox 会遭受此错误,但是将 DisplayStyle 设置为 DropDownDropDownList 则不会出现此错误。

使用上面的代码,我强制在显示之前创建所有者ToolStripDropDownMenu

关于c# - 当 DropDownStyle 为 Simple 时,ToolStripCombobox 显示在屏幕左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40390885/

相关文章:

c# - 在 ToolStripDropDownMenu 中隐藏 ImageMargin 和 CheckMargin

c# - 窗口失去焦点后更新 ToolstripDropdownButton 时出现问题

.net - WPF 中的 ToolStripDropDownButton 等价物?

c# - 外部更新数据库时刷新网络表单

c# - FluentValidation NullReferenceException - 防止检查空值的方法?

c# - 将 C# 转换为 Java,如何处理 IEnumerable?

c# - CefSharp ChromiumWebBrowser 将初始化并加载传递给构造函数的页面,但调用 MyBrowser.Load ("URL") 返回空白屏幕

c# - TypeDescriptor 和子元素

c# - 抛出 'System.Windows.Forms.AxHost+InvalidActiveXStateException' 类型的异常

时间:2019-01-17 标签:c#Windowsforms 'Object reference not set to an instance of an object'