可滚动面板中的组合框导致问题
我有一个将 AutoScroll
设置为 true
的面板。在其中,我以编程方式添加 ComboBox
控件。如果我添加足够的控件以超出面板的可视大小,则会出现滚动条(到目前为止一切顺利)。但是,如果我打开可视区域底部附近的组合框之一,组合列表将无法正确显示,并且可滚动区域似乎已扩展。这会导致所有控件都被“拉”到面板的新底部,并在顶部出现一些新的空白区域。如果我继续点击面板底部的下拉菜单,可滚动区域将继续无限期扩展。我将控件锚定到左侧、右侧和顶部,因此我认为不涉及锚定。有什么明显的原因可能导致这种情况吗?
更新:看起来问题在于将控件固定在右侧。如果我不锚定到右侧,那么我就不会出现奇怪的行为。但是,如果没有正确锚定,控件就会被滚动条切断。
这是我构建的一个简化的测试用例,它显示了该问题:
public Form1()
{
InitializeComponent();
Panel panel = new Panel();
panel.Size = new Size(80, 200);
panel.AutoScroll = true;
for (int i = 0; i < 10; ++i)
{
ComboBox cb = new ComboBox();
cb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
cb.Items.Add("Option 1");
cb.Items.Add("Option 2");
cb.Items.Add("Option 3");
cb.Items.Add("Option 4");
cb.Location = new Point(0, i * 24);
panel.Controls.Add(cb);
}
Controls.Add(panel);
}
如果您滚动面板底部并点击底部附近的组合框,您会注意到奇怪的行为。
I have a panel with AutoScroll
set to true
. In it, I am programmatically adding ComboBox
controls. If I add enough controls to exceed the viewable size of the panel a scroll bar appears (so far so good). However, if I open one of the combo boxes near the bottom of the viewable area the combo list isn't properly displayed and the scrollable area seems to be expanded. This results in all of the controls being "pulled" to the new bottom of the panel with some new blank space at the top. If I continue to tap on the drop down at the bottom of the panel the scrollable area will continue to expand indefinitely. I'm anchoring the controls to the left, right and top so I don't think anchoring is involved. Is there something obvious that could be causing this?
Update: It looks like the problem lies with anchoring the controls to the right. If I don't anchor to the right then I don't get the strange behavior. However, without right anchoring the control gets cut off by the scroll bar.
Here's a simplified test case I built that shows the issue:
public Form1()
{
InitializeComponent();
Panel panel = new Panel();
panel.Size = new Size(80, 200);
panel.AutoScroll = true;
for (int i = 0; i < 10; ++i)
{
ComboBox cb = new ComboBox();
cb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
cb.Items.Add("Option 1");
cb.Items.Add("Option 2");
cb.Items.Add("Option 3");
cb.Items.Add("Option 4");
cb.Location = new Point(0, i * 24);
panel.Controls.Add(cb);
}
Controls.Add(panel);
}
If you scroll the bottom of the panel and tap on the combo boxes near the bottom you'll notice the strange behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是您所使用的设备特有的问题。我根本无法重现这种行为。您是否尝试过设置Panel的Dock属性?影响滚动行为的其他属性(AutoScrollMargin、AutoScrollMinSize、AutoScrollPosition)怎么样?我还会调整面板的大小,并可能使用 System.Windows.Forms.Screen 类根据设备自动确定它。
This seems to be a problem specific to the devices you are using. I can't reproduce this behavior at all. Have you tried setting the Dock property of the Panel? How about the other properties that affect scrolling behavior (AutoScrollMargin, AutoScrollMinSize, AutoScrollPosition)? I'd also play with the size of the panel, and maybe use the System.Windows.Forms.Screen class to determine it automatically based on the device.
尝试将自动滚动设置为 false 并尝试正常使用滚动条。
或者,如果您无法找到另一个合理的解决方案,则可以处理滚动/调整大小事件并手动调整大小/移动控件(正是锚点的作用)虽然这会很烦人,但它将摆脱您可能遇到的任何问题。
Try setting autoscroll to false and try using scroll bars normally.
Alternatively, if you are unable to find another reasonable solution, it would be possible to handle the scroll/resize events and manually resize/move your controls (Exactly what anchor does) While this would be quite annoying to do, it would get rid of any problems you may be having.
我也能够在 Visual Studio 2005、.Net CF 2.0 下针对 Windows CE 设备进行测试,因为我们也有使用旧设备的东西。我没有任何问题。是否可能是设备上的 Windows CE 版本问题导致了该问题?正在使用什么设备(即:制造商?)。我什至创建了一个单一形式的全新项目,因此没有其他事情会改变/影响您的代码。
该行为在调试时、运行时或两者都有效吗?
I too was able to test against a device of Windows CE, under Visual Studio 2005, .Net CF 2.0 as we too have stuff using older equipment too. I have no problems. Might it be any issue of the Windows CE version on the device that is causing the issue? What device is being used (ie: Manufacturer?). I even created a brand new project of the single form, so no other things going on to alter / influence your code.
Does the behavior work while debugging, run-time, or both?