Click here to Skip to main content
15,895,283 members
Home / Discussions / C#
   

C#

 
QuestionFile name validation Pin
baronics5-Oct-05 10:29
baronics5-Oct-05 10:29 
AnswerRe: File name validation Pin
David Stone5-Oct-05 11:06
sitebuilderDavid Stone5-Oct-05 11:06 
QuestionHow to get datarow from OdbcDataReader? Pin
Ming Luo5-Oct-05 9:56
Ming Luo5-Oct-05 9:56 
AnswerRe: How to get datarow from OdbcDataReader? Pin
Wjousts5-Oct-05 10:10
Wjousts5-Oct-05 10:10 
AnswerRe: How to get datarow from OdbcDataReader? Pin
Guffa5-Oct-05 10:20
Guffa5-Oct-05 10:20 
Questionforms Pin
xilefxilef5-Oct-05 8:40
xilefxilef5-Oct-05 8:40 
AnswerRe: forms Pin
Heath Stewart5-Oct-05 9:19
protectorHeath Stewart5-Oct-05 9:19 
QuestionWindow Handles and Mouse Clicks Pin
budidharma5-Oct-05 8:28
budidharma5-Oct-05 8:28 
AnswerRe: Window Handles and Mouse Clicks Pin
Heath Stewart5-Oct-05 8:35
protectorHeath Stewart5-Oct-05 8:35 
GeneralRe: Window Handles and Mouse Clicks Pin
budidharma5-Oct-05 8:42
budidharma5-Oct-05 8:42 
GeneralRe: Window Handles and Mouse Clicks Pin
budidharma6-Oct-05 6:57
budidharma6-Oct-05 6:57 
Questionhow handle objects and code of setup forms? Pin
Anonymous5-Oct-05 8:26
Anonymous5-Oct-05 8:26 
QuestionAccessing dBase files with C# Pin
TheMajorRager5-Oct-05 8:02
TheMajorRager5-Oct-05 8:02 
AnswerRe: Accessing dBase files with C# Pin
Heath Stewart5-Oct-05 9:15
protectorHeath Stewart5-Oct-05 9:15 
GeneralRe: Accessing dBase files with C# Pin
TheMajorRager5-Oct-05 11:37
TheMajorRager5-Oct-05 11:37 
AnswerRe: Accessing dBase files with C# Pin
Heath Stewart5-Oct-05 11:40
protectorHeath Stewart5-Oct-05 11:40 
GeneralRe: Accessing dBase files with C# Pin
TheMajorRager5-Oct-05 12:14
TheMajorRager5-Oct-05 12:14 
GeneralRe: Accessing dBase files with C# Pin
TheMajorRager5-Oct-05 13:54
TheMajorRager5-Oct-05 13:54 
Questioncheckbox ques Pin
ter815-Oct-05 7:13
susster815-Oct-05 7:13 
GeneralRe: checkbox ques Pin
Guffa5-Oct-05 8:57
Guffa5-Oct-05 8:57 
AnswerRe: checkbox ques Pin
Heath Stewart5-Oct-05 9:10
protectorHeath Stewart5-Oct-05 9:10 
You should use RadioButtons, then, within the same container control. Not only do they already provide the functionality you want, but it gives the user a consistent experience which is very important for applications. Great care goes into creating Windows and Windows applications to make sure users have a consistent experience. ISVs should follow these guidelines as well.

If you have a legitemate reason for using CheckBoxes, you'll have to clear them yourself. I recommend writing a separate class that uses a list with you can add CheckBoxes to and then have methods to clear all except the one that was checked. You could also simply enumerate all controls in your container using the Control.Controls property and - if the current enumerated control is a CheckBox and it's not the one that was clicked, clear the check mark. It also allows you to logically group differnet check boxes even if they're in the same container or different containers.

The former - using a separate class - is more flexible and scalable. A simple example follows:
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
 
class CheckBoxManager
{
  ArrayList cbs;
  bool changing = false;
 
  public CheckBoxManager()
  {
    cbs = new ArrayList();
  }
 
  public void Add(CheckBox cb)
  {
    if (cb == null) throw new ArgumentNullException("cb");
    cbs.Add(cb);
    cb.CheckedChanged += new EventHandler(OnCheckChanged);
  }
 
  public void AddRange(IEnumerable cbs)
  {
    if (cbs == null) throw new ArgumentNullException("cbs");
    foreach (object obj in cbs)
    {
      CheckBox cb = obj as CheckBox;
      if (cb != null) Add(cb);
    }
  }
 
  void OnCheckChanged(object sender, EventArgs e)
  {
    // Don't run if we're current changing the Checked property
    // for each CheckBox.
    if (changing) return;
    else changing = true;
 
    foreach (CheckBox cb in cbs)
    {
      if (sender != cb) cb.Checked = false;
    }
 
    changing = false;
  }
}
 
class Test : Form
{
  static void Main()
  {
    Application.Run(new Test());
  }
 
  Test()
  {
    CheckBoxManager group0 = new CheckBoxManager();
    CheckBoxManager group1 = new CheckBoxManager();
    int top = 8;
 
    for (int i = 0; i < 10; i++)
    {
      bool isgroup0 = i % 2 == 0;
 
      CheckBox cb = new CheckBox();
      if (isgroup0) group0.Add(cb);
      else group1.Add(cb);
      Controls.Add(cb);
 
      cb.Text = string.Format("CheckBox{0} (Group {1})", i, i % 2);
      cb.Top = top;
      cb.Left = 8;
      cb.Width = 200;
      top = cb.Bottom + 8;
    }
 
    Height = SystemInformation.CaptionHeight + top;
    Text = "CheckBoxManager Example";
  }
}


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft

[My Articles] [My Blog]
QuestionChanging the colour &amp; font of (null) value datagrid entries Pin
Red_Wizard_Shot_The_Food5-Oct-05 6:58
Red_Wizard_Shot_The_Food5-Oct-05 6:58 
AnswerRe: Changing the colour &amp; font of (null) value datagrid entries Pin
Heath Stewart5-Oct-05 8:43
protectorHeath Stewart5-Oct-05 8:43 
QuestionAdding a chart to my form Pin
james3775-Oct-05 6:25
james3775-Oct-05 6:25 
AnswerRe: Adding a chart to my form Pin
enjoycrack5-Oct-05 14:24
enjoycrack5-Oct-05 14:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.