110,566
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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);
}
}
}
}
public Form1()
{
InitializeComponent();
this.textBox1.Leave += new EventHandler(textBox1_Leave);
}
void textBox1_Leave(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Focus();
}
}
this.textBox1.Leave += new EventHandler(textBox1_Leave);