im creating a feature in my app that will re create the CTRL+Z thing. i got few textboxs and i made a table like this:
hashtable textChanges[obj.Name, obj.Text] = new HashTable(50);
im having problem extractthe value from a chossen key. im getting the key afther the keyDown is fired.
in the event im looking for the control with focus, and use his name to extract the last value the he enter the table.
this is the event code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyData == Keys.Z)
{
for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls[i].Focused)
{
if (this.Controls[i].GetType() == typeof(TextBox))
{
TextBox obj = (TextBox)this.Controls[i];
obj.Text = textChanges[obj.Name]; // <--- compile error
//Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
}
}
}
}
}
this is how i add keys & value to the HashTable
private void textBox_OnTextChange(object sender, EventArgs e)
{
if (sender.GetType() == typeof(TextBox))
{
TextBox workingTextBox = (TextBox)sender;
textChanges.Add(workingTextBox.Name, workingTextBox.Text);
}
if (sender.GetType() == typeof(RichTextBox))
{
RichTextBox workingRichTextBox = (RichTextBox)sender;
textChanges.Add(workingRichTextBox.Name, workingRichTextBox.Text);
}
}
why do i get missing a cast error?
(sorry for my english)
You need to convert it to string. It would be better if you use dictionary. Dictionary is a generic type and you do not need types casting which is required for hashtable.
Hashtable stores the object type and you are required to type cast the object back to your desired type.