怎样能重写控件的show方法,使它按模式化展现。

zl194 2009-11-20 03:27:38
我想让控件出现后,强制获得焦点,必须处理控件上的事件而不能点其他的地方。
如,弹出combobox下拉框后,必须选择一项才可以,关闭下来框,否则不能退出。
...全文
84 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wartim 2009-11-21
  • 打赏
  • 举报
回复
纠正一下,同级子控件要禁止,但父控件要忽略
void SetEnabled(Control.ControlCollection CheckControls, Control ExceptControl, bool Enabled)
{
foreach (Control C in CheckControls)
if (C != ExceptControl)
{
if (Enabled || !Enabled && C.Controls.Find(ExceptControl.Name, true).Length == 0)
C.Enabled = Enabled;
SetEnabled(C.Controls, ExceptControl, Enabled);
}
}
wartim 2009-11-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication17
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

this.comboBox1.Enter += new EventHandler(ControlEnter);
this.comboBox2.Enter += new EventHandler(ControlEnter);

this.comboBox1.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
this.comboBox2.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
}

void ControlEnter(object sender, EventArgs e)
{
SetEnabled(this.Controls, (Control)sender, false);
}

void SelectedIndexChanged(object sender, EventArgs e)
{
SetEnabled(this.Controls, (Control)sender, true);
}

void SetEnabled(Control.ControlCollection CheckControls, Control ExceptControl, bool Enabled)
{
foreach (Control C in CheckControls)
if (C != ExceptControl)
{
C.Enabled = Enabled;
SetEnabled(C.Controls, ExceptControl, Enabled);
}
}
}
}
zhangwike 2009-11-21
  • 打赏
  • 举报
回复
看看!
cstester 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zl194 的回复:]
引用 2 楼 cstester 的回复:
在 失去焦点的事件里面写。 如果值为空, 从新获得一下焦点。

C# codepublic Form1()
        {
            InitializeComponent();this.textBox1.Leave+=new EventHandler(textBox1_Leave);

           
        }void textBox1_Leave(object sender, EventArgs e)
        {if (string.IsNullOrEmpty(textBox1.Text))
            {

                textBox1.Focus();
            }
        }

然后你可以循环遍历窗体上的控件动态的把所有需要的控件绑定textBox1_Leave。、
C# codethis.textBox1.Leave+=new EventHandler(textBox1_Leave);


Form也有焦点的,不一定都是textbox
[/Quote]

你可以写个全局的 Form 焦点, 别的焦点方法. 动态 绑定就是了. 期待更好的方法。
zl194 2009-11-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cstester 的回复:]
在 失去焦点的事件里面写。 如果值为空, 从新获得一下焦点。

C# codepublic Form1()
{
InitializeComponent();this.textBox1.Leave+=new EventHandler(textBox1_Leave);


}void textBox1_Leave(object sender, EventArgs e)
{if (string.IsNullOrEmpty(textBox1.Text))
{

textBox1.Focus();
}
}

然后你可以循环遍历窗体上的控件动态的把所有需要的控件绑定textBox1_Leave。、
C# codethis.textBox1.Leave+=new EventHandler(textBox1_Leave);
[/Quote]

Form也有焦点的,不一定都是textbox
cstester 2009-11-20
  • 打赏
  • 举报
回复
在 失去焦点的事件里面写。 如果值为空, 从新获得一下焦点。


public Form1()
{
InitializeComponent();

this.textBox1.Leave += new EventHandler(textBox1_Leave);


}
void textBox1_Leave(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{

textBox1.Focus();
}
}



然后你可以循环遍历窗体上的控件动态的把所有需要的控件绑定textBox1_Leave。、
 
this.textBox1.Leave += new EventHandler(textBox1_Leave);
呦呦 2009-11-20
  • 打赏
  • 举报
回复
up

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧