I'm trying to create a "Debug window" or "Output window" using windows forms, so if there is some error in the users input I can show that error in this "output window", code and the problem below:
public partial class DebugWindow : Form
{
public DebugWindow()
{
InitializeComponent();
}
public void WriteDebugWindow(string text)
{
DateTime dt = DateTime.Now;
text = dt.ToString("[yyyy-MM-dd HH:mm:ss] ") + text;
listBox1.Items.Add(text);
}
}
And the class
public class Debug
{
DebugWindow debugWindow;
public Debug() { debugWindow = new DebugWindow(); }
public void WriteDebugWindow(string text)
{
this.debugWindow.WriteDebugWindow(text);
OpenWindow();
}
public void OpenWindow()
{
if (debugWindow.Visible)
debugWindow.BringToFront();
else
debugWindow.Show(); //Problem here
}
}
So, if I try to run a code like:
int i = 10;
Debug.WriteDebugWindow(i.ToString());
i = 20;
Debug.WriteDebugWindow(i.ToString());
It will write the text to the listBox, but if I use the ".Show()" the debug window open, but it's freezes, I cant close/move/etc, if I use ".ShowDialog()" it does NOT freeze, but it will set the i to 20 and print it again only if I CLOSE the "debug window" form.
There is a solution for that? Because I tried everything, run it in a thread, create a new stance etc etc, but the probem persists, since I want to keep the listBox items in future "Debug.Write..." calls, I dont want a blank listBox everytime I call it(in case of using new DebugWindow()).
Thank you.