简单的问题。如果我在winform项目中设置一个单选按钮为只读,它的外观(字体颜色)将变为浅灰色。当我将它的enabled属性设置为false时也是如此。我如何创建一个只读单选按钮看起来像一个正常的?因为这样你几乎看不到它。谢谢
ndh0cuux1#
作为一个选项,您可以添加ReadOnly属性并覆盖OnClick,然后仅在!ReadOnly:
ReadOnly
OnClick
!ReadOnly
using System; using System.Windows.Forms; public class MyRadioButton : RadioButton { public bool ReadOnly { get; set; } protected override void OnClick(EventArgs e) { if (!ReadOnly) base.OnClick(e); } }
cgvd09ve2#
将AutoCheck属性设置为false。
bd1hkmkf3#
通过删除单选按钮的文本并在其旁边添加标签来管理它。不是最好的解决方案,但工作...
9vw9lbht4#
老问题用老密码,我知道。我最近遇到了同样的问题。原因:我想显示一个状态(以一个很好的可视格式),但是现在允许用户改变那个状态(有意或无意)。“禁用”格式不是很可读或吸引人。我搜索了可能性,这个帖子排在了最前面。随着时间的推移,关于这个toppic有很多讨论,不知何故,它现在仍然提出了问题。我为此创建了RadioButtonReadonly,其中readonly状态使用与enabled case相同的格式,但不允许用户更改任何设置。这个想法是屏蔽base.Enabled并替换它。然后拒绝来自用户的任何输入,如果在Readonly模式下。如果有人认为Enabled和!Enabled有不同的格式是有原因的,那么我完全同意。但是没有Readonly选项在某些情况下是一个令人恼火的问题。我发现了一些建议,建议使用一个带有覆盖层的面板,覆盖层是一个显示内容的图像。这个图像显示了Enabled模式下的所有面板成员,但图像捕捉了所有的点击事件。尝试了一下,并得到了它,但产生的代码是麻烦和笨拙的。下一次尝试RadioButtonReadonly,它有一个额外的属性Readonly并覆盖Enabled。当Readonly=false时,所有行为类似于Radiobutton当Readonly=true时,显示器上的布局与Enabled=false时相同,但所有用户交互都将被忽略。下面是我的RadioButtonReadonly的代码,它有自己的测试形式。(Note:我借了一些代码和想法,以解决我在搜索时发现的问题。不幸的是,我不记得我在哪里看到了所有的代码。使用覆盖的最初想法来自Hans Passant,作为一个静态解决方案,不能在运行时改变状态。我修改了它,以便能够跟踪变化。)
RadioButtonReadonly
base.Enabled
Readonly
Enabled
!Enabled
Radiobutton
using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace RadioButtonControl { class Form1 : Form { RadioButtonReadonly RadioButton1 = new RadioButtonReadonly(); RadioButtonReadonly RadioButton2 = new RadioButtonReadonly(); RadioButton RadioButton3 = new RadioButton(); RadioButton RadioButton4 = new RadioButton(); Button button1 = new Button(); Button button2 = new Button(); Button button3 = new Button(); public Form1() : base() { RadioButton1.Location = new Point(50, 50); RadioButton1.Size = new Size(100, 23); RadioButton1.Text = "Click 1ro"; RadioButton1.Font = SystemFonts.IconTitleFont; Controls.Add(RadioButton1); RadioButton2.Location = new Point(50, 75); RadioButton2.Size = new Size(100, 23); RadioButton2.Text = "Click 2ro"; RadioButton2.Font = SystemFonts.IconTitleFont; Controls.Add(RadioButton2); RadioButton3.Location = new Point(50, 100); RadioButton3.Size = new Size(100, 23); RadioButton3.Text = "Click 3"; RadioButton3.Font = SystemFonts.IconTitleFont; Controls.Add(RadioButton3); RadioButton4.Location = new Point(50, 125); RadioButton4.Size = new Size(100, 23); RadioButton4.Text = "Click 4"; RadioButton4.Font = SystemFonts.IconTitleFont; Controls.Add(RadioButton4); button1.Location = new Point(175, 231); button1.Size = new Size(105, 23); button1.Text = "Toggle Style"; button1.Click += new EventHandler(this.button1_Click); Controls.Add(button1); button2.Location = new Point(175, 200); button2.Size = new Size(105, 23); button2.Text = "Toggle Enabled"; button2.Click += new EventHandler(this.button2_Click); Controls.Add(button2); button3.Location = new Point(175, 169); button3.Size = new Size(105, 23); button3.Text = "Toggle Readonly"; button3.Click += new EventHandler(this.button3_Click); Controls.Add(button3); Text = (Application.RenderWithVisualStyles) ? "Visual Styles Enabled" : "Visual Styles Disabled"; } [STAThread] static void Main() { // If you do not call EnableVisualStyles below, then RadioButtonRenderer.DrawRadioButton automatically // detects this and draws the radio button without visual styles. Application.EnableVisualStyles(); Application.Run(new Form1()); } private void button1_Click(object sender, EventArgs e) { Application.VisualStyleState = Application.VisualStyleState ^ VisualStyleState.ClientAndNonClientAreasEnabled; GroupBoxRenderer.RenderMatchingApplicationState = true; Text = (Application.RenderWithVisualStyles) ? "Visual Styles Enabled" : "Visual Styles Disabled"; } private void button2_Click(object sender, EventArgs e) { RadioButton1.Enabled = !RadioButton1.Enabled; RadioButton2.Enabled = RadioButton1.Enabled; RadioButton3.Enabled = RadioButton1.Enabled; RadioButton4.Enabled = RadioButton1.Enabled; } private void button3_Click(object sender, EventArgs e) { RadioButton1.Readonly = !RadioButton1.Readonly; RadioButton2.Readonly = RadioButton1.Readonly; } } /// <summary> /// Enables the user to show radio buttons in readonly mode. /// </summary> /// <remarks> /// When visualstyle is enabled there is minimal feedback to the user about 'Readonly=true' (e.g.: not changing when hoovering over). /// /// When visualstyle is not enabled there is no feedback to the user about 'Readonly=true'. /// </remarks> public class RadioButtonReadonly : RadioButton { /// <summary> /// Radiobutton with Readonly attribute. /// </summary> public RadioButtonReadonly() { BaseEnabledCheck(null, null); base.EnabledChanged += BaseEnabledCheck; } #region Readonly/Enabled /// <summary> /// Gets or sets a value indicating whether the control will respond to user interaction. /// </summary> public bool Readonly { get { return _readonly; } set { if (_readonly != value) { _readonly = value; BaseEnabledCheck(null, null); } } } bool _readonly = false; /// <summary> /// Gets or sets a value indicating whether the control can respond to user interaction. /// </summary> /// <remarks> /// Overruled to take away visualstyle behaviour. /// </remarks> public new bool Enabled { get { return _enabled; } set { if (_enabled != value) { _enabled = value; BaseEnabledCheck(null, null); } } } bool _enabled = true; /// <summary> /// Raises the System.Windows.Forms.Control.EnabledChanged event, force base.Enabled to always be true. /// </summary> /// <param name="sender"></param> /// <param name="e">An System.EventArgs that contains the event data.</param> private void BaseEnabledCheck(object sender, EventArgs e) { if (Readonly) { if (!base.Enabled) { base.Enabled = true; Invalidate(); } } else { if (Enabled != base.Enabled) { base.Enabled = Enabled; Invalidate(); } } } #endregion Readonly/Enabled #region User input handling /// <summary> /// Raises the System.Windows.Forms.Control.Click event. /// </summary> /// <param name="e">An System.EventArgs that contains the event data.</param> protected override void OnClick(EventArgs e) { if (!Readonly) base.OnClick(e); } /// <summary> /// Raises the System.Windows.Forms.ButtonBase.OnKeyUp(System.Windows.Forms.KeyEventArgs) event. /// </summary> /// <param name="kevent">A System.Windows.Forms.KeyEventArgs that contains the event data.</param> protected override void OnKeyDown(KeyEventArgs kevent) { if (!Readonly) base.OnKeyDown(kevent); } /// <summary> /// Occurs when a character. space or backspace key is pressed while the control has focus. /// </summary> /// <param name="e">A System.Windows.Forms.KeyPressEventArgs that contains the event data.</param> protected override void OnKeyPress(KeyPressEventArgs e) { if (!Readonly) base.OnKeyPress(e); } /// <summary> /// Raises the System.Windows.Forms.ButtonBase.OnKeyUp(System.Windows.Forms.KeyEventArgs) event. /// </summary> /// <param name="kevent">A System.Windows.Forms.KeyEventArgs that contains the event data.</param> protected override void OnKeyUp(KeyEventArgs kevent) { if (!Readonly) base.OnKeyUp(kevent); } /// <summary> /// Raises the System.Windows.Forms.Control.MouseUp event, draw the radio button in the checked or unchecked state. /// </summary> /// <param name="e">A System.Windows.Forms.MouseEventArgs that contains the event data.</param> protected override void OnMouseDown(MouseEventArgs e) { if (!Readonly) base.OnMouseDown(e); } /// <summary> /// Raises the System.Windows.Forms.Control.MouseUp event, draw the radio button in the hot state. /// </summary> /// <param name="e"></param> protected override void OnMouseUp(MouseEventArgs e) { if (!Readonly) base.OnMouseUp(e); } /// <summary> /// Raises the System.Windows.Forms.Control.MouseHover event, draw the radio button in the hot state. /// </summary> /// <param name="e">An System.EventArgs that contains the event data.</param> protected override void OnMouseHover(EventArgs e) { if (!Readonly) base.OnMouseHover(e); } /// <summary> /// Raises the System.Windows.Forms.Control.OnMouseEnter(System.EventArgs) event, draw the radio button in the hot state. /// </summary> /// <param name="eventargs"></param> protected override void OnMouseEnter(EventArgs eventargs) { if (!Readonly) base.OnMouseEnter(eventargs); } /// <summary> /// Raises the System.Windows.Forms.Control.OnMouseLeave(System.EventArgs) event, draw the radio button in the unpressed state. /// </summary> /// <param name="e">An System.EventArgs that contains the event data.</param> protected override void OnMouseLeave(EventArgs e) { if (!Readonly) base.OnMouseLeave(e); } /// <summary> /// Raises the System.Windows.Forms.Control.MouseWheel event. /// </summary> /// <param name="e">A System.Windows.Forms.MouseEventArgs that contains the event data.</param> protected override void OnMouseWheel(MouseEventArgs e) { if (!Readonly) base.OnMouseWheel(e); } /// <summary> /// Raises the System.Windows.Forms.Control.DragDrop event. /// </summary> /// <param name="drgevent">A System.Windows.Forms.DragEventArgs that contains the event data.</param> protected override void OnDragDrop(DragEventArgs drgevent) { if (!Readonly) base.OnDragDrop(drgevent); } /// <summary> /// Raises the System.Windows.Forms.Control.DragEnter event. /// </summary> /// <param name="drgevent">A System.Windows.Forms.DragEventArgs that contains the event data.</param> protected override void OnDragEnter(DragEventArgs drgevent) { if (!Readonly) base.OnDragEnter(drgevent); } /// <summary> /// Raises the System.Windows.Forms.Control.DragLeave event. /// </summary> /// <param name="e">An System.EventArgs that contains the event data.</param> protected override void OnDragLeave(EventArgs e) { if (!Readonly) base.OnDragLeave(e); } /// <summary> /// Raises the System.Windows.Forms.Control.DragOver event. /// </summary> /// <param name="drgevent">A System.Windows.Forms.DragEventArgs that contains the event data.</param> protected override void OnDragOver(DragEventArgs drgevent) { if (!Readonly) base.OnDragOver(drgevent); } #endregion User input handling } }
4条答案
按热度按时间ndh0cuux1#
作为一个选项,您可以添加
ReadOnly
属性并覆盖OnClick
,然后仅在!ReadOnly
:cgvd09ve2#
将AutoCheck属性设置为false。
bd1hkmkf3#
通过删除单选按钮的文本并在其旁边添加标签来管理它。
不是最好的解决方案,但工作...
9vw9lbht4#
老问题用老密码,我知道。
我最近遇到了同样的问题。原因:我想显示一个状态(以一个很好的可视格式),但是现在允许用户改变那个状态(有意或无意)。“禁用”格式不是很可读或吸引人。
我搜索了可能性,这个帖子排在了最前面。随着时间的推移,关于这个toppic有很多讨论,不知何故,它现在仍然提出了问题。
我为此创建了
RadioButtonReadonly
,其中readonly状态使用与enabled case相同的格式,但不允许用户更改任何设置。这个想法是屏蔽
base.Enabled
并替换它。然后拒绝来自用户的任何输入,如果在Readonly
模式下。如果有人认为
Enabled
和!Enabled
有不同的格式是有原因的,那么我完全同意。但是没有Readonly
选项在某些情况下是一个令人恼火的问题。我发现了一些建议,建议使用一个带有覆盖层的面板,覆盖层是一个显示内容的图像。这个图像显示了
Enabled
模式下的所有面板成员,但图像捕捉了所有的点击事件。尝试了一下,并得到了它,但产生的代码是麻烦和笨拙的。下一次尝试
RadioButtonReadonly
,它有一个额外的属性Readonly并覆盖Enabled。当Readonly=false时,所有行为类似于
Radiobutton
当Readonly=true时,显示器上的布局与Enabled=false时相同,但所有用户交互都将被忽略。
下面是我的
RadioButtonReadonly
的代码,它有自己的测试形式。(Note:我借了一些代码和想法,以解决我在搜索时发现的问题。不幸的是,我不记得我在哪里看到了所有的代码。使用覆盖的最初想法来自Hans Passant,作为一个静态解决方案,不能在运行时改变状态。我修改了它,以便能够跟踪变化。)