Monday, September 27, 2010

Setting Objects in ListBox and retrieving Objects from ListBox in C#

As my previous blog for ComboBox, this is to demonstrate setting and retrieving objects from Listbox, here the difference is to set more than one values in Listbox, and retrieving more than one values from ListBox.

I have a simple class ListValues having two fields with getter and setter function, ListValues class also implemented ToString and Equal functions.

My form having a listBox with name “lstBoxValues” and its property SelectionMode is set to multiple.

There is a lable in form for displaying selected values of listBox and two buttons, one for selecting value in ListBox and another for displaying value of selected items in ListBox.

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 ListTestApplication
{
 public partial class Form1 : Form
 {
    public Form1()
    {      
       InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {      
      lstBoxValues.Items.Add(new ListValue(1, "Value1"));
      lstBoxValues.Items.Add(new ListValue(2, "Value2"));      
      lstBoxValues.Items.Add(new ListValue(3, "Value3"));
      
      lstBoxValues.Items.Add(new ListValue(4, "Value4"));
    }
    private void button1_Click(object sender, EventArgs e)
    {      
      lstBoxValues.ClearSelected();
      ListValue v = new ListValue(3, null);      
      lstBoxValues.SelectedItems.Add(v);
    }
    private void button2_Click(object sender, EventArgs e)
    {
      String values = "";
      for (int i = 0; i< lstBoxValues.SelectedItems.Count; i++)
      {          
         values += lstBoxValues.SelectedItems[i] as ListValue;
          
         values += ", ";
      }      
      lblValue.Text = values;
    }
  }
}


Setting Objects in ComboBox and retrieving Object from ComboBox in C#


Let me demonstrate how to set your object in combo box, in my case for demonstration I have created a simple Class with the name ListValue. Then created a simple C# form having a ComboBox with the name “cmbListValues”, buttons with the name “button1” and text “Set Value”, “button2” with text “Get Value” and I have a label having “lblValue”.

My ListValue class looks like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListTestApplication
{
    class ListValue
    {
        private int id;
        private String value;
        public ListValue(int id_, String value_)
        {
            id = id_;
            value = value_;
        }
        public int ID { set { id = ID; } get { return id; } }
        public String Value
                { set { value = Value; } get { return value; } }
        public override string ToString()
        {
            return value;
        }
        public override bool Equals(object obj)
        {
            if (obj is ListValue)
            {
                ListValue l = obj as ListValue;
                if(l.id.Equals(id)) return true;
            }
            return false;
        }
    }
}


I have two variables in my ListValue class, a constructor with 2 arguments to set values for “id” and “value”, I am having getter and setter for these variables.

I have also overridden ToString method, for displaying my meaningful value in comboBox.

I have also overridden Equals method, it is important to override equals method for preselecting any value in combo.

In form load function I have
        private void Form1_Load(object sender, EventArgs e)
        {
            cmbListValues.Items.Add(new ListValue(1, "Value1"));
            cmbListValues.Items.Add(new ListValue(2, "Value2"));
            cmbListValues.Items.Add(new ListValue(3, "Value3"));
            cmbListValues.Items.Add(new ListValue(4, "Value4"));
        }

Button1 click, I have click event as follows

        private void button1_Click(object sender, EventArgs e)
        {
            ListValue v = new ListValue(3,null);
            cmbListValues.SelectedItem = v;
        }

When click on button “Set Value” the method will set value “Value3” in combo, it is very useful to set value in case of updating any record and you want to set previous selected combo, for example you are going to update an employee and in update screen employee’s country should be preselected.

Button2 click, I have button click event as follows

       private void button2_Click(object sender, EventArgs e)
        {
            ListValue v = cmbListValues.SelectedItem as ListValue;
            if( v != null )
            lblValue.Text = v.Value;
        }


The above method demonstrates how to retrieve the value from selected combobox. You can retrieve all the
information of selected object.

All to gather forms code looks like

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 ListTestApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cmbListValues.Items.Add(new ListValue(1, "Value1"));
            cmbListValues.Items.Add(new ListValue(2, "Value2"));
            cmbListValues.Items.Add(new ListValue(3, "Value3"));
            cmbListValues.Items.Add(new ListValue(4, "Value4"));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ListValue v = new ListValue(3,null);
            cmbListValues.SelectedItem = v;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            ListValue v = cmbListValues.SelectedItem as ListValue;
            if( v != null )
            lblValue.Text = v.Value;
        }
    }
}

Saturday, September 25, 2010

Speech Recognition in Vista & XP using C#

Following is the code to recognize numbers and strings, and having different behavior depending on operating system.

For running project you need to Add Reference of System.Speech.Recognition by Click on Project then click on Add References and find System.Speech.Recognition


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;
using System.Speech.Recognition;
namespace NumbeRecognitionTest
{
public partial class Form1 : Form
{
SpeechRecognizer rec = new SpeechRecognizer();

public Form1()
{
InitializeComponent();
rec.SpeechRecognized += rec_SpeechRecognized;

}
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
lblReuslt.Text = e.Result.Text;
}


private void Form1_Load(object sender, EventArgs e)
{
//recoTest1();
recoTest2();
}

//Recognition of following choices only work with Vista but doesn't work with XP with Speech SDK5.2
public void recoTest1()
{
var c = new Choices();
for (var i = 0; i <= 100; i++)
c.Add(i.ToString());
c.Add("go");
c.Add("delete record");

var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true;
}

//Recognition of following choices Work with vista & XP with Speech SDK5.2
public void recoTest2()
{
var c = new Choices();

c.Add("one");
c.Add("two");
c.Add("three");
c.Add("four");
c.Add("five");
c.Add("twenty five");
c.Add("fifty six");
c.Add("go");
c.Add("delete record");

var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true;
}
}
}





Please do let me know any updates.
I have found the number recognition doesn't work for SDK5.2 and function recoTest1 doesn't work on it.

Speech Recognition in vista and XP using C#

I am trying to run a sample program of speech recognition in vista and then XP giving me different behaviour, I think it is because Vista has its own speech sdk,
I am using speech sdk5.2 in XP operating system & downloaded it but for vista there is builtin speech sdk.
I am having goal to recognize numbers but in vista I am able to recognize number but not yet success with XP operating system with Speech SDK5.2 even though I am able to recognize words..

Well still trying to figure out why....