[英]How do I ensure visibility of a control in a UserControl that has to be scrolled?
I have an UserControl(I use WinForms) with many components in it and you have to scroll up/down or left/right if you want to see a certain one. 我有一个其中包含许多组件的UserControl(我使用WinForms),如果要查看某个组件,则必须向上/向下或向左/向右滚动。 I now want to implement a searchbox which will allow you to search a component and when you select it in the results (a datagridview), the UserControl will scroll to make sure you can see the selected component.
现在,我想实现一个搜索框,该搜索框将允许您搜索组件,并在结果中选择它(一个datagridview)时,UserControl将滚动以确保您可以看到所选的组件。
My first idea was to check if the component location is bigger than the ClientSize, something like: 我的第一个想法是检查组件位置是否大于ClientSize,例如:
if (ivrMenu.X > _designerControl.ClientSize.Width && ivrMenu.Y > _designerControl.ClientSize.Height)
MessageBox.Show("Down-Right");
else if (ivrMenu.X > _designerControl.ClientSize.Width)
MessageBox.Show("Right");
else if (ivrMenu.Y > _designerControl.ClientSize.Height)
MessageBox.Show("Down");
Obviously, this would work only if the scrolling position would be 0 for both H and V. So, I have to take into account the position of the current Scrolling Position or I could scroll to position 0 and then make my scrolling to the component but that wouldn't look so good. 显然,这仅在H和V的滚动位置都为0的情况下才有效。因此,我必须考虑当前Scrolling Position的位置,否则我可以滚动到位置0,然后滚动到组件,但是看起来不太好。
My question is, how do I take into account the scrolling? 我的问题是,如何考虑滚动? How do I calculate the location of the visible area with the scrolling Position?
如何通过滚动“位置”计算可见区域的位置?
There are a couple of things you can try with a datagridview
: 您可以尝试使用
datagridview
:
1) theGridView.FirstDisplayedScrollingRowIndex = theGridView.SelectedRows[0].Index;
1)
theGridView.FirstDisplayedScrollingRowIndex = theGridView.SelectedRows[0].Index;
2) theGridView.CurrentCell = theGridView.Rows[index].Cells[0];
2)
theGridView.CurrentCell = theGridView.Rows[index].Cells[0];
The second will scroll to the item without moving it to the top of the view. 第二个将滚动到该项目,而无需将其移动到视图顶部。
I did some math with the ClientSize, AutoScrollPosition and the Size of my components and this code seems to work perfectly: 我对ClientSize,AutoScrollPosition和组件的大小进行了一些数学运算,此代码似乎可以正常工作:
if (ivrMenu.X + ivrMenu.Width < _designerControl.AutoScrollPosition.X * (-1))
MessageBox.Show("Left");
if (ivrMenu.X + ivrMenu.Width > _designerControl.ClientSize.Width - _designerControl.AutoScrollPosition.X)
MessageBox.Show("Right");
if (ivrMenu.Y + ivrMenu.Height < _designerControl.AutoScrollPosition.Y * (-1))
MessageBox.Show("Up");
if (ivrMenu.Y + ivrMenu.Height > _designerControl.ClientSize.Height - _designerControl.AutoScrollPosition.Y)
MessageBox.Show("Down");
this where is the position of the component (in my case ivrMenu) outside the visibe ClientArea. 这是组件(在我的情况下为ivrMenu)在visibe ClientArea之外的位置。 From this I just have to do the scrolling so the component fits the screen.
由此,我只需要进行滚动即可使组件适合屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.