Hi...!
i'm working on dictionary project and i want to know how can i get the value of a dictionary<string,string> by it's key?! i want to show it on a text field! thanks...!
this is the code that you add word and it's translate but i don't know how to show it's value by key...!

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, string> dic = new Dictionary<string, string>();

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Dictionary";
            txtWordforTranslate.Enabled = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            if ("".Equals(txtWord.Text)  || "".Equals(txtTranslateWord.Text))
            {
                DialogResult d = MessageBox.Show("Please Fill The Blank Fields!", "Blank Field Error!", MessageBoxButtons.OK);
                txtWordforTranslate.Enabled = false;   
            }
            if (!"".Equals(txtWord.Text) && !"".Equals(txtTranslateWord.Text))
            {
                txtWordforTranslate.Enabled = true;
                dic.Add(txtTranslateWord.Text,txtWord.Text);
            }
        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {

        }
    }

Recommended Answers

All 4 Replies

It sounds like you haven't read the documentation on the Dictionary class, because retrieving stored values by key is one of the most basic and fundamental operations. Specifically, what you want is the Item property or the TryGetValue() method.

How Can i do that would you explain more?!
this is my code but it won't work what is it's problem?!

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, string> dic = new Dictionary<string, string>(); 
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Dictionary";
            txtWordforTranslate.Enabled = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            if ("".Equals(txtWord.Text)  || "".Equals(txtTranslateWord.Text))
            {
                DialogResult d = MessageBox.Show("Please Fill The Blank Fields!", "Blank Field Error!", MessageBoxButtons.OK);
                txtWordforTranslate.Enabled = false;   
            }
            if (!"".Equals(txtWord.Text) && !"".Equals(txtTranslateWord.Text))
            {
                txtWordforTranslate.Enabled = true;
                dic.Add(txtTranslateWord.Text,txtWordforTranslate.Text);

                string value=txtWordforTranslate.Text;
                dic.TryGetValue(txtWord.Text, out value);
                rtxtTranslate.Text = value;
            }
        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {

        }
    }

What are lines 27-29 doing in your button add method? My guess is that they somehow belong in your translate button event handler

You could also simplify your code by enabling the Add button in the TextChanged event handler for txtWord and txtTranslateWord, then disabling it after the new value has been added to the disctionary. This way you don't have to keep popping up the messagebox.

Also TryGetValue returns a boolean. The out variable won't change if the return is false. A conditional statement to trap this and adjust things will probably help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.