I am making an application where I have a tabControl and some controls I want to create. So I have a function that basically makes controls and a textbox. The code looks like this:
TextBox message_box = new TextBox();
message_box.Size = new Size(790, 38);
message_box.Location = new Point(9, 7);
message_box.Font = new Font("Microsoft Sans Serif", 20);
message_box.KeyDown += Message_box_KeyDown;
message_box.Enter += Message_box_Enter;
bottom_panel.Controls.Add(message_box);
I also added some other controls, but this specific control is the one I am having problems with. Because when I add these controls to the tabpage, and then I add the tabpage to the tabcontrol something strange happenes.
Every single control in the added tabPage is working fine, and without lag. But when I type in the textBox I get massive lag and it freezes. This happens also if I add textbox to the designer created page in the tabControl aswell.
This is the event that is being triggered:
private async void Message_box_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
string name = "";
this.Invoke(new MethodInvoker(delegate { name = tabControl1.SelectedTab.Name; }));
TextBox box = new TextBox();
this.Invoke(new MethodInvoker(delegate { box = (TextBox)sender; }));
if (box.Text.Length > 0)
{
uiControls.ListItem melding = await Task.Factory.StartNew(() => sendMelding(name, box.Text));
box.Clear();
}
}
}
I don't know what I am doing wrong, and how to make the textBox to stop lagging. I have tryed to check if it needs invokation, I have tryed invokation. I have also used this code, owerwriting the CreateParams in the form:
protected override CreateParams CreateParams
{
get
{
// add the drop shadow flag for automatically drawing
// a drop shadow around the form
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
To be clear, the KeyDown function has no hit on the performance, the code within the KeyDown function is NOT fired everytime i write, its only fired when i hit enter. however usually if i write about 2-3 chars in the textbox the lagg starts to appear and the program freezes, if i remove the events it also occours.