954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Printing the related key value by selecting the key in combo box

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.IO;
using System.Collections;

namespace FileReadFormsAuto
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.Equals("TV"))
{
comboBox2.Items.Clear();
comboBox2.Items.Add("TV.ON");
comboBox2.Items.Add("TV.OFF");
comboBox2.Items.Add("TV.MUTE");
}

else if(comboBox1.SelectedItem.Equals("FRIDGE"))
{
comboBox2.Items.Clear();
comboBox2.Items.Add("FRIDGE.ON");
comboBox2.Items.Add("FRIDGE.OFF");
}

else if(comboBox1.SelectedItem.Equals("AC CONTROL"))
{
comboBox2.Items.Clear();
comboBox2.Items.Add("AC.ON");
comboBox2.Items.Add("AC.MEDIUM");
comboBox2.Items.Add("AC.OFF");
}

else if(comboBox1.SelectedItem.Equals("VOLUME CONTROL"))
{
comboBox2.Items.Clear();
comboBox2.Items.Add("VOLUP");
comboBox2.Items.Add("VOLDOWN");
}

else if (comboBox1.SelectedItem.Equals("CHANNEL CONTROL"))
{
comboBox2.Items.Clear();
comboBox2.Items.Add("CHANNEL+");
comboBox2.Items.Add("CHANNEL-");
}
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string key = string.Empty;
string[] lines = File.ReadAllLines(@"C:\SUBS\FileReadFormsAuto\FileReadFormsAuto\TVOP.txt");
Hashtable tab = new Hashtable();

foreach (string line in lines)
{
string[] items = line.Split('=');
tab.Add(items[0], int.Parse(items[1]));
}

if (tab.ContainsKey(comboBox2.SelectedValue))
{
textBox1.Text = tab[comboBox2.SelectedValue].ToString();
}

else
{
MessageBox.Show("Invalid Key or Command Selected....\nPlease Select A Valid Device & Operation");
}
}

catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
}

Here is my doubt

here i included values in combobox2,If i select the key in it the respecting value should be printed in the textbox1 by selecting the key in the combobox2

adityavarma999
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

It looks like you're processing this stuff twice -- once in the "if" statements and once for the HashTable.

What does the file contain?
From your parse method, it looks like a string, and equal sign and a number.
Is that correct?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You