I am creating a dictionary with = Tag value, and Text of multiple TextBox in a WPF application when the "submit button" is clicked. I only want to add to the dictionary the Tag and text of the Textboxes where the text has changed.
How do I add the tag value and the text value of multiple TextBox, only when the text has changed?
The code below is what I have so far, but I am stuck:
private void Submit_Button_Click(object sender, RoutedEventArgs e)
{
Dictionary<string, string> tagDict = new Dictionary<string, string>();
foreach(Control ctrl in MainGrid.Children)
{
if (ctrl.GetType() == typeof(textBox))
{
TextBox tb = ctrl as TextBox;
if (//I am trying to get a value that represents that the Text has changed here
)
{
string tagString = tb.Tag.ToString();
string textString = tb.Text;
tagDict.Add(tagString, textString);
}
}
}
}
I think a good solution is, first, to put your dictionary somewhere else (a private in the form, maybe)., and populate it when showing the form. This will allow you to update the dictionary when the text box is changed.
Assuming this, I would create a single method to be called every time the user "leaves" the edit box, and check if the text changed, on which case I would update the dictionary.
This way, the "submit" just have to use the dictionary that will be updated already.
ex:
private Dictionary<string, string> fDic; //This must be instantiated in the initialization
//Add this method to every Leave event in all text box controls.
private void textBox_Leave(object sender, EventArgs e)
{
TextBox text = sender as TextBox;
//Check if the text changed
if (text != null)
{
if (fDic.ContainsKey((string)text.Tag))
{
if (fDic[(string)text.Tag] != text.Text)
fDic[(string)text.Tag] = text.Text;
}
else if(text.Text != null)
{
fDic[(string)text.Tag] = text.Text;
}
}
}
The former code will update the dictionary if the TAG is already there AND the text associated with the tag is DIFFERENT from the text in the text box, and if THERE IS a text in the text box and the tag isn't in the dictionary already.
I'm assuming that the "Tag" is stored in the Tag property of each text box. Also, it will store strings formed of only spaces. If you do not want this, the code can be changed to:
private Dictionary<string, string> fDic; //This must be instantiated in the initialization
//Add this method to every Leave event in all text box controls.
private void textBox_Leave(object sender, EventArgs e)
{
TextBox text = sender as TextBox;
//Check if the text changed
if (text != null)
{
if (fDic.ContainsKey((string)text.Tag))
{
if (fDic[(string)text.Tag] != text.Text && !string.IsNullOrWhiteSpace(text.Text))
fDic[(string)text.Tag] = text.Text;
}
else if(!string.IsNullOrWhiteSpace(text.Text) != null)
{
fDic[(string)text.Tag] = text.Text;
}
else if (string.IsNullOrWhiteSpace(text.Text)
fDic.Remove((string)text.Tag);
}
}
In the code above, it will also remove from the dictionary if the text is empty.
EDIT
After some chat, we came with a better option to accomplish what you want to do.
First, make the dictionary a Form variable, like in the option above.
private Dictionary<string, string> fDic;
Then, when you populate the TextBox's, put one entry (with the TAG) for each text box in the fDic. To do this, you can use this kind of code:
foreach(Control ctrl in MainGrid.Children)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
fDic[(string)text.Tag] = text.Text;
}
When the buttom click event is called, just compare the text in the text boxes with the one's previously stored in the dic, using the dictionary to do this.
private void Submit_Button_Click(object sender, RoutedEventArgs e)
{
Dictionary<string, string> tagDict = new Dictionary<string, string>();
foreach(Control ctrl in MainGrid.Children)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
{
if (fDic[(string)text.Tag] != text.Text)
{
string tagString = tb.Tag.ToString();
string textString = tb.Text;
tagDict.Add(tagString, textString);
}
}
}
}