Reputation: 13
I have searched for what I am asking of you but I may have been wording it wrong, so I hope what I have typed is easy to understand.
I am creating a Css formatter with two textboxes using WinForms
I would like it to take a css long format code in the first textBoxLongFormat
then appear in the second, textboxShortFormat
, as css short code.
I have a bit of code for both text boxes and a separate class of other code for the behind the scenes to change the format.
The class works but I’m having a problem with textBoxLongFormat
and I’m guessing it will happen the other way round, the code is looping on its self and not closing, so it’s not sending the format to textBoxShortFormat
so nothing happens.
There is something I am doing wrong, I know, but I cannot see it. What is it that I am doing wrong? It will be great to have your help.
Here is the code for the textboxes if it helps.
What is it that i need to add or to make it work?
private void textBoxLongFormat_TextChanged(object sender, EventArgs e)
{
CssFormatConverter cssLongFormatConverter = new CssFormatConverter();
string longFormatCss = textBoxLongFormat.Text;
string shortFormatCss = cssLongFormatConverter.ToShortFormat(longFormatCss);
textBoxShortFormat.Text = shortFormatCss;
}
private void textBoxShortFormat_TextChanged(object sender, EventArgs e)
{
CssFormatConverter cssShortFormatConverter = new CssFormatConverter();
string shortFormatCss = textBoxShortFormat.Text;
string longFormatCss = cssShortFormatConverter.ToLongFormat(shortFormatCss);
textBoxLongFormat.Text = longFormatCss;
}
Thank you in advance
Upvotes: 1
Views: 256
Reputation: 27633
Before updating a TextBox, unsubscribe it from the TextChanged
event. Then update. Then re-subscribe.
In the first one, that would be:
textBoxShortFormat.TextChanged -= textBoxShortFormat_TextChanged;
textBoxShortFormat.Text = shortFormatCss;
textBoxShortFormat.TextChanged += textBoxShortFormat_TextChanged;
Upvotes: 0
Reputation: 38079
Add a boolean check that indicates the other textbox is updating.
bool isUpdating = false;
private void textBoxLongFormat_TextChanged(object sender, EventArgs e)
{
if (!isUpdating)
{
isUpdating = true;
CssFormatConverter cssLongFormatConverter = new CssFormatConverter();
string longFormatCss = textBoxLongFormat.Text;
string shortFormatCss = cssLongFormatConverter.ToShortFormat(longFormatCss);
textBoxShortFormat.Text = shortFormatCss;
isUpdating = false;
}
}
private void textBoxShortFormat_TextChanged(object sender, EventArgs e)
{
if (!isUpdating)
{
isUpdating = true;
CssFormatConverter cssShortFormatConverter = new CssFormatConverter();
string shortFormatCss = textBoxShortFormat.Text;
string longFormatCss = cssShortFormatConverter.ToLongFormat(shortFormatCss);
textBoxLongFormat.Text = longFormatCss;
isUpdating = false;
}
}
Upvotes: 2