I am attempting to resolve some flickering issues in the Paint event by setting DoubleBuffered = true
in a form that gets opened when the user clicks a button in the main form. However, just as the new form loads I get the exception in Program.cs
"System.ArgumentException occurred in System.Drawing.dll"
"System.ArgumentException: 'Parameter is not valid.'"
in the Program.cs file
I can confirm that the Paint method does not get called when the error pops up, and the error seems to occur just as the form's constructor is completed.
I'm setting DoubleBuffered = True
in the Designer properties.
First form calling second form (Form1.cs)
if(toggleButton())
{
int numThreads = Int32.Parse(numeric_threads.Value.ToString());
List<Section> sections = new List<Section>();
sections.Add(new Section(0, 0, prevScreen.Bounds.Width, 100, checkbox_UseAudio.Checked, false, Int32.Parse(numeric_horizontalLEDs.Value.ToString()))); //Top
sections.Add(new Section(0, prevScreen.Bounds.Height - 100, prevScreen.Bounds.Width, 100, false, false, Int32.Parse(numeric_horizontalLEDs.Value.ToString()))); //Bottom
sections.Add(new Section(0, 0, 100, prevScreen.Bounds.Height, false, true, Int32.Parse(numeric_verticalLEDs.Value.ToString()))); //Left
sections.Add(new Section(prevScreen.Bounds.Width - 100, 0, 100, prevScreen.Bounds.Height, false, true, Int32.Parse(numeric_verticalLEDs.Value.ToString())));// Right
dial = new LEDSimulate(pathScript, pathPython, formInst, sections, combo_monitor.SelectedIndex, numThreads);
dial.Show();
}
else
{
button_flag = true;
//Kill all the threads by closing
dial.Close();
}
Second form that gets opened form first form and results in the error (LEDSimulate.cs)
public LEDSimulate(string pathScript, string pathPython, Form1 formInst, List<Section> sections, int mon, int numThreads)
{
InitializeComponent();
instance = this;
this.TransparencyKey = (BackColor);
this.sections = sections;
this.procs = new List<Process>();
this.formInst = formInst;
//Initialize timer to tick every 30 seconds to update UI
timer_updatePaint.Interval = 33;
//Load with dummy data to update later
Pen dummy_pen = new Pen(Color.FromArgb(0, 0, 0, 0));
foreach (Section s in sections)
{
for(int i = 0; i<s.subSections; i++)
{
paintList.Add(new PaintForm(dummy_pen, 0, 0, 0, 0));
}
}
Console.WriteLine("Finished adding and starting");
timer_updatePaint.Start();
//-----Once this is done, the error occurs------
}
Program.cs
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //Error pops up on this line
}
}
private void timer_updatePaint_Tick(object sender, EventArgs e)
{
this.Refresh();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}