CheckedListBox.SelectedIndexChanged : CheckedListBox « System.Windows.Forms « C# / C Sharp by API

Home
C# / C Sharp by API
1.Microsoft.Win32
2.System
3.System.Collections
4.System.Collections.Generic
5.System.Collections.Specialized
6.System.ComponentModel
7.System.Configuration
8.System.Data
9.System.Data.Common
10.System.Data.Linq
11.System.Data.Odbc
12.System.Data.OleDb
13.System.Data.Sql
14.System.Data.SqlClient
15.System.Diagnostics
16.System.DirectoryServices
17.System.Drawing
18.System.Drawing.Drawing2D
19.System.Drawing.Imaging
20.System.Drawing.Printing
21.System.Drawing.Text
22.System.EnterpriseServices
23.System.Globalization
24.System.IO
25.System.IO.Compression
26.System.IO.IsolatedStorage
27.System.IO.Ports
28.System.Linq
29.System.Management
30.System.Media
31.System.Messaging
32.System.Net
33.System.Net.Mail
34.System.Net.NetworkInformation
35.System.Net.Sockets
36.System.Reflection
37.System.Resources
38.System.Runtime
39.System.Runtime.CompilerServices
40.System.Runtime.InteropServices
41.System.Runtime.Remoting
42.System.Runtime.Remoting.Channels
43.System.Runtime.Remoting.Channels.Http
44.System.Runtime.Remoting.Messaging
45.System.Runtime.Serialization
46.System.Runtime.Serialization.Formatters.Binary
47.System.Runtime.Serialization.Formatters.Soap
48.System.Security
49.System.Security.AccessControl
50.System.Security.Cryptography
51.System.Security.Cryptography.X509Certificates
52.System.Security.Permissions
53.System.Security.Policy
54.System.Security.Principal
55.System.ServiceProcess
56.System.Text
57.System.Text.RegularExpressions
58.System.Threading
59.System.Timers
60.System.Web.Security
61.System.Web.Services
62.System.Windows.Controls
63.System.Windows.Forms
64.System.Xml
65.System.Xml.Linq
66.System.Xml.Schema
67.System.Xml.Serialization
68.System.Xml.XPath
69.System.Xml.Xsl
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp by API » System.Windows.Forms » CheckedListBox 
CheckedListBox.SelectedIndexChanged
  
using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
 
public class CheckedListBoxDemo:Form{
  CheckedListBox FavLangs;
  GroupBox grpControls;
  Button AddValue;
  Button EditValue;
  Button DeleteValue;
  Button ShowValues;
  TextBox OldValue;
  TextBox NewValue;
  Label OldCaption;
  Label NewCaption;
  CheckBox chkAll;
 
  public CheckedListBoxDemo(){
    grpControls=new GroupBox();
    grpControls.Text="CheckedListBox Demo";
 
    AddValue=new Button();
    AddValue.Text="&Add";
    AddValue.Click+=new EventHandler(Add_Click);
    
    EditValue=new Button();
    EditValue.Text="&Edit";
    EditValue.Click+=new EventHandler(Edit_Click);
    
    DeleteValue=new Button();
    DeleteValue.Text="&Delete";
    DeleteValue.Click+=new EventHandler(Delete_Click);
    
    ShowValues=new Button();
    ShowValues.Text="&Show";
    //ShowValues.Click+=new EventHandler(ShowValues_Click);
    ShowValues.Click+=new EventHandler(Checked_Changed);
    
    OldValue=new TextBox();
    OldValue.ReadOnly=true;
    NewValue=new TextBox();
 
    OldCaption=new Label();
    OldCaption.Text="Old Value:";
    NewCaption=new Label();
    NewCaption.Text="New Value:";

    chkAll=new CheckBox();
    chkAll.Text="Check/UnCheck All";
    chkAll.CheckedChanged+= new EventHandler(Checked_Changed);
    chkAll.Width=175;
 
    OldCaption.Location=new Point(15,15);
    PositionControl(OldCaption,OldValue,true);
    PositionControl(OldCaption,NewCaption,false);
    PositionControl(OldValue,NewValue,false);
    PositionControl(NewCaption,AddValue,false);
    PositionControl(AddValue,EditValue,true);
    PositionControl(EditValue,DeleteValue,true);
    PositionControl(DeleteValue,ShowValues,true);
    PositionControl(AddValue,chkAll,false);
 
    grpControls.Controls.AddRange(new Control[]{OldCaption,OldValue,NewCaption,NewValue,AddValue,EditValue,DeleteValue,ShowValues,chkAll});
    grpControls.Size=new Size(450,200);
 
    FavLangs=new CheckedListBox();
    FavLangs.Location=new Point(10,10);
    FavLangs.SelectedIndexChanged+=new EventHandler(SelectedIndex_Changed);
 
    grpControls.Location=new Point(FavLangs.Left+FavLangs.Width+20,FavLangs.Top);
    this.Controls.AddRange(new Control[]{FavLangs,grpControls});
  }
 
  private void PositionControl(Control source,Control destination,bool CanPlaceHorizontal)
  {
    if(CanPlaceHorizontal){
      destination.Location=new Point(source.Left+source.Width+20,source.Top);
    }else{
      destination.Location=new Point(source.Left,source.Top+source.Height+20);      
    }
  }
 
  private void Add_Click(object sender,EventArgs e){
    ((Button)sender).Text = "aaa";
    
    if(NewValue.Text.Trim()!=""){
      FavLangs.Items.Add(NewValue.Text);
    }else{
      MessageBox.Show("Enter a Value to Add");
    }
  }
 
  private void SelectedIndex_Changed(object sender,EventArgs e){
    OldValue.Text=FavLangs.Items[FavLangs.SelectedIndex].ToString();
  }
 
  private void Edit_Click(object sender,EventArgs e){
    if(FavLangs.SelectedIndex==-1){
      MessageBox.Show("Select a Item to Edit");
    else{
     if(NewValue.Text.Trim()!=""){
        FavLangs.Items[FavLangs.SelectedIndex]=NewValue.Text;
     }
      else
      {
        MessageBox.Show("Enter a Value to Edit");
      }            
    }
  }
 
  private void Delete_Click(object sender,EventArgs e)
  {
    if(FavLangs.SelectedIndex!=-1)
    {
      FavLangs.Items.RemoveAt(FavLangs.SelectedIndex);
    }
    else
    {
      MessageBox.Show("Select a Item to Delete");
    }
  }
 
  private void ShowValues_Click(object sender,EventArgs e){
    string SelectedValues="The following value(s) are Selected:\n" new String('-',48"\n";
    for(int i=0;i<FavLangs.CheckedItems.Count;i++){
      SelectedValues+=FavLangs.CheckedItems[i].ToString() "\n";
    }
    MessageBox.Show(SelectedValues);
  }
 
  private void Checked_Changed(object sender,EventArgs e){
      for(int i=0;i<FavLangs.Items.Count;i++){
          FavLangs.SetItemChecked(i,chkAll.Checked);
      }    
  }
 
  public static void Main(){
      Application.Run(new CheckedListBoxDemo());
  }
}

   
    
  
Related examples in the same category
1.CheckedListBox.CheckOnClick
2.CheckedListBox.CheckState.Checked
3.CheckedListBox.CheckState.Indeterminate
4.CheckedListBox.GetItemCheckState
5.CheckedListBox.GetItemText
6.CheckedListBox.ItemCheck
7.CheckedListBox.Items
8.CheckedListBox.Items.Add
9.CheckedListBox.Items.AddRang
10.CheckedListBox.MultiColumn
11.CheckedListBox.ScrollAlwaysVisible
12.CheckedListBox.SetItemCheckState
13.CheckedListBox.ThreeDCheckBoxes
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.