CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Posts
    11

    Checkboxes for checked and unchecked

    Hi,

    In windows application i taken datagridview displaying rows with checkboxes
    user has to checked only one at a time.
    i can take radiobutton but client is asking only with checkboxes......

    so i need code for this can anybody help for this situation

    Thanks And Regards,
    Vijay..........

  2. #2
    Join Date
    Dec 2006
    Posts
    86

    Re: Checkboxes for checked and unchecked

    I remember doing something like this. I had two checkboxes, but only one was allowed to be checked at a time. I just wrote some basic event handling that was executed each time one of the boxes was checked.

    I don't remember the exact syntax, but I think it went something like this

    Code:
    CheckBox1_checked()
    {
      CheckBox2.enabled = false;
    }
    
    CheckBox2_checked()
    {
      CheckBox1.enabled = false;
    }
    That way, each time one of the boxes is checked, the other will be unchecked. You can also set, as a default, for one of them to be checked.

  3. #3
    Join Date
    Feb 2007
    Location
    Cat Square (near Charlotte)
    Posts
    16

    Re: Checkboxes for checked and unchecked

    Quote Originally Posted by asantavicca
    I remember doing something like this. I had two checkboxes, but only one was allowed to be checked at a time. I just wrote some basic event handling that was executed each time one of the boxes was checked.

    I don't remember the exact syntax, but I think it went something like this

    Code:
    CheckBox1_checked()
    {
      CheckBox2.enabled = false;
    }
    
    CheckBox2_checked()
    {
      CheckBox1.enabled = false;
    }
    That way, each time one of the boxes is checked, the other will be unchecked. You can also set, as a default, for one of them to be checked.
    Good answer... but doesn't this code need to read:
    Code:
    CheckBox1_checked()
    {
        CheckBox2.checked= false;
    }
    
    CheckBox2_checked()
    {
        CheckBox1.checked = false;
    }
    By setting the "enabled" property to false, you are making it such that the field cannot be checked/unchecked at all!
    C# - Visual Studio 2005
    Frameworks 2.0

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Checkboxes for checked and unchecked

    Quote Originally Posted by masaboina
    Hi,

    In windows application i taken datagridview displaying rows with checkboxes
    user has to checked only one at a time.
    i can take radiobutton but client is asking only with checkboxes......

    so i need code for this can anybody help for this situation

    Thanks And Regards,
    Vijay..........
    You may try to explain to your client that using a radio button implies exclusivity whereas checkboxes do not. Using the proper control for the situation makes for better usability for the end users - it would be kind of like using a listbox as a button.

  5. #5
    Join Date
    Dec 2006
    Posts
    86

    Re: Checkboxes for checked and unchecked

    Quote Originally Posted by ronmoles
    Good answer... but doesn't this code need to read:
    Code:
    CheckBox1_checked()
    {
        CheckBox2.checked= false;
    }
    
    CheckBox2_checked()
    {
        CheckBox1.checked = false;
    }
    By setting the "enabled" property to false, you are making it such that the field cannot be checked/unchecked at all!
    Yes. I was just trying to get the idea across.

    And, as Arjay said, it would be wise to advise your client on the benefits of sticking to standards.

  6. #6
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Checkboxes for checked and unchecked

    This talk of standards and RadioButtons is all well and good but this is within a DataGridView, not just on a form. There is no radio button column by default so unless you want to create your own you're stuck using the check box column. Creating your own wouldn't be too hard but the check box column is fine for this.
    Code:
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
    	if (e.ColumnIndex == checkBoxColumnIndex && (bool)this.dataGridView1[e.ColumnIndex, e.RowIndex].Value == true)
    	{
    		for (int rowIndex = 0; rowIndex < this.dataGridView1.Rows.Count - 1; rowIndex++)
    		{
    			if (rowIndex != e.RowIndex)
    			{
    				this.dataGridView1[e.ColumnIndex, rowIndex].Value = false;
    			}
    		}
    	}
    }
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured