System.Windows.Forms.Trackbar - 如何防止用户拖动滑块?
有没有某种方法可以(暂时)阻止用户通过拖动滑块来更改 TrackBar 的值?我发现的唯一方法是将 Enabled
属性设置为 false,但它也会使轨迹栏变灰。
编辑: 由于我得到的更多答案是关于为什么我不应该这样做,而不是如何做到这一点,所以我决定解释为什么我想这样做。
我使用它不是为了允许用户调整某些应用程序属性的值,而是为了显示和控制某些操作的进度。想象一下,例如您最喜欢的媒体播放器 - 它可能包含一些控件(我称之为轨迹栏,但英语不是我的母语,所以它可能是错误的) 它显示当前正在播放的电影部分,而且还允许您控制它 - 及时向后或向前移动并观看不同的部分。
我正是以这种方式使用轨迹栏 - 我不知道任何其他组件会更好(进度栏不允许我更改“位置”)。运行良好, 我唯一想做的就是不允许用户“使用轨迹栏,除非电影暂停”。
出于这个确切的原因,我在 Delphi 6 中多次使用了 trackbar 组件,但是当它被禁用时,它并没有变灰,在我看来它工作得很好。这就是为什么我问 这里是否可以在 C# 中实现相同的效果。
Is there some way to (temporarily) prevent user from changing the value of TrackBar by dragging the slider? Only way I found is to set Enabled
property to false but it also greys out the trackbar.
Edit:
Since I´m getting more answers about why shouldn't I do it rather than how it's possible to do it, I decided to explain why I would like to do it.
I'm not using it to allow user adjust value of some application property, but to display and control progress of some action. Imagine for example your favourite media player - it probably contains some control (I'd call it trackbar but English is not my native language so it's maybe wrong)
that displays what part of movie it's currently playing, but also allows you to control it - move back or forward in time and watch the different part.
I use the trackbar in exactly this way - I don't know any other component that would be better (Progressbar won't allow me changing the "position"). It's working fine,
the only thing I'd like to do is not to allow user to "use the trackbar unless the movie is paused".
For this exact reason I've used trackbar component many times in Delphi 6, but when it was disabled it didn't grey out and in my opinion it worked fine. That's why I asked
here if it's possible to achieve the same effect in C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的方法是将
Enabled
属性设置为 false,正如您正确指出的那样,它也会使轨迹栏变灰。使控件变灰是 Windows 标准,表示“您目前无法使用此控件”,试想一下,您看到一些在禁用时未变灰的控件,尝试将它们全部都尝试起来将是一件非常偶然的事情。查看哪些已启用,哪些未启用。
如果您确实想在不使用
Enabled
属性的情况下阻止更改,一种可能的替代方法是使用TrackBar的ValueChanged
事件The best way would be to set the
Enabled
property to false, as you rightly pointed out it greys out the track bar too.Greying out the controls is a windows standard for saying "You cannot use this control at the moment", just imagine you were presented with some controls that was not grayed out where disabled, it would be a very hit and miss affair trying them all to see which is enabled and which isn't.
If you really want to prevent changes without using the
Enabled
property one possible alternative is to use theValueChanged
event of the TrackBar如果您想要雄心勃勃(或过于热心),您可以创建一个透明面板控件来覆盖 TrackBar,从而防止用户与其交互,即将
此控件添加到您的窗体中,然后将其放置在 TrackBar 上,即
然后您可以当您想要允许用户交互时隐藏它。
If you wanted to be ambitious (or overzealous), you could create a Transparent Panel control to overlay the TrackBar thereby preventing the user from interacting with it, i.e.
Add this control to your form and then position it over the TrackBar, i.e.
Then you can hide it when you want to allow user interaction.
正如 Xander 指出的那样,拥有一个看起来完全正常但实际上无法使用的轨迹栏是让用户发疯的好方法!
如果您担心跟踪栏看起来“不活动”,您可以尝试另一种方式来显示它所代表的数据,例如标签。这样,当您禁用跟踪栏时,您仅表示无法编辑该值。
As Xander pointed out, having a trackbar that looks perfectly normal but can't actually be used is a good way to drive your users crazy!
If you're worried about the trackbar looking "inactive", you might try another way of displaying the data it represents, like a label. That way when you disable the trackbar, you're only indicating that the editing of the value is unavailable.