i have a button click to pop up a new form, and it is taking a while to load up. the following is my
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CashPOS
{
public partial class SubItems : Form
{
CashSales cashSalesForm;
List<string> itemList= new List<String>();
public SubItems()
{
InitializeComponent();
addItemToList();
getMainFormInfo();
}
private void getMainFormInfo()
{
cashSalesForm = new CashSales();
createItemBtn(itemList, subItemPanel, itemBtnClicked);
}
private void addItemToList()
{
itemList.Add("abc12");
itemList.Add("abc13");
itemList.Add("abc14");
itemList.Add("abc15");
itemList.Add("abc16");
itemList.Add("abc17");
itemList.Add("abc18");
itemList.Add("abc19");
itemList.Add("abc20");
itemList.Add("abc21");
itemList.Add("abc22");
itemList.Add("abc23");
itemList.Add("abc24");
itemList.Add("abc25");
itemList.Add("abc26");
itemList.Add("abc27");
itemList.Add("abc28");
itemList.Add("abc29");
itemList.Add("abc30");
itemList.Add("abc31");
itemList.Add("abc32");
itemList.Add("abc33");
itemList.Add("abc34");
itemList.Add("abc35");
itemList.Add("abc36");
itemList.Add("abc37");
itemList.Add("abc38");
itemList.Add("abc39");
}
protected void itemBtnClicked(object sender, EventArgs e)
{
float unitPrice = 0.0f;
Button btn = sender as Button;
string itemSelected = btn.Text;
}
public void createItemBtn(List<String> itemList, Control panel, EventHandler handler)
{
for (int i = 0; i < itemList.Count; i++)
{
Button newButton = new Button();
newButton.Width = 203;
newButton.Height = 132;
newButton.AutoSize = false;
newButton.Name = "newBtn" + i;
newButton.Text = itemList[i].ToString();
// btnList.Add(newButton);
panel.Controls.Add(newButton);
}
}
}
}
this is the form that open from the following button click event
protected void itemBtnClicked(object sender, EventArgs e)
{
Button btn = sender as Button;
string itemSelected = btn.Text;
SubItems sub = new SubItems();
sub.Show();
}
i am not sure if the createItemBtn is delaying it by alot, because when I remove this function, it would open the form instantly, but with this function, it takes around 1.5 second to open the form. Is there a better way to reduce the time for this?
Related
When I launch my application it gives me an error at " Form1 Test = new Form1();" in my class. Here is my code. I want to use labels from my form so therefore I used "form1 test".
I made a class so I can call my methods from it in my Mainform as I need to code my application with classes. When I launched the application for the first time it worked, but then after trying again it didn't work anymore.
Main form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tester
{
public partial class Form1 : Form
{
Zombie zombie = new Zombie();
int levens = 3;
public Form1()
{
InitializeComponent();
test1.Text = "Levens: " + "" + levens;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Zombie()
{
foreach (Control control in Controls)
{
PictureBox pic = control as PictureBox;
if (pic != null)
{
pic.Top += 1;
if (pic.Top > 600 && pic.Visible == true)
{
pic.Top = 0;
test1.Text = $"Levens: {--levens}";
}
else if (pic.Top > 600 && pic.Visible == false)
{
pic.Visible = true;
pic.Top = 0;
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
zombie.MakeZombie(5, this);
}
}
}
Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tester
{
class Zombie
{
Random random = new Random();
Form1 Test = new Form1();
private int score = 0;
public void MakeZombie(int aantal, Form formInstance)
{
for (int i = 0; i < aantal; i++)
{
PictureBox picture = new PictureBox();
picture.Image = Properties.Resources.ZombieDik;
picture.Size = new Size(200, 200);
picture.Location = new Point(random.Next(1500), 0);
picture.SizeMode = PictureBoxSizeMode.Zoom;
picture.Click += zombie_Click;
picture.BackColor = Color.Transparent;
formInstance.Controls.Add(picture);
}
}
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
pic.Visible = false;
score++;
Test.label2.Text = $"Score: {score}";
Test.Controls.Remove(pic);
pic.Dispose();
}
}
}
In zombie_Click() you can get a reference to the Form from the sender itself:
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
Form1 f1 = pic.FindForm() as Form1;
score++;
f1.label2.Text = $"Score: {score}";
pic.Dispose();
}
I’am switching between buttons by adding/removing them dynamically. After 20 switches the whole application starts to be more and more slow.
It seem’s It is related to function Dispose(). When I use it the problem become worst. You can see how I used it in pasted code.
I’am using Visual Studio 2010 Express.
Any idea what I’am doing wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Tracker
{
public partial class Tracker : Form
{
Button New_Trace = new Button();
Button Continue_Trace = new Button();
Button Save_Trace = new Button();
Button New_Point = new Button();
Button Exit_Trace = new Button();
public Tracker()
{
InitializeComponent();
Init_Menu1();
}
public void Init_Menu1()
{
New_Trace.Left = 20;
New_Trace.Top = 40;
New_Trace.Text = "New Trace";
this.New_Trace.Click += new System.EventHandler(this.New_Trace_Click);
this.Controls.Add(New_Trace);
Continue_Trace.Left = 100;
Continue_Trace.Top = 40;
Continue_Trace.Text = "Cont. Trace";
this.Continue_Trace.Click += new System.EventHandler(this.Continue_Trace_Click);
this.Controls.Add(Continue_Trace);
Save_Trace.Left = 180;
Save_Trace.Top = 40;
Save_Trace.Text = "Save Trace";
this.Save_Trace.Click += new System.EventHandler(this.Save_Trace_Click);
this.Controls.Add(Save_Trace);
}
public void Init_Menu2()
{
New_Point.Left = 20;
New_Point.Top = 40;
New_Point.Text = "New Point";
this.New_Point.Click += new System.EventHandler(this.New_Point_Click);
this.Controls.Add(New_Point);
Exit_Trace.Left = 180;
Exit_Trace.Top = 40;
Exit_Trace.Text = "Exit";
this.Exit_Trace.Click += new System.EventHandler(this.Exit_Trace_Click);
this.Controls.Add(Exit_Trace);
}
public void remove_Menu1()
{
if (this.Controls.Contains(New_Trace))
{
// this.New_Trace.Click -= new System.EventHandler(this.New_Trace_Click);
this.Controls.Remove(New_Trace);
//New_Trace.Dispose();
}
if (this.Controls.Contains(Continue_Trace))
{
// this.Continue_Trace.Click -= new System.EventHandler(this.Continue_Trace_Click);
this.Controls.Remove(Continue_Trace);
//Continue_Trace.Dispose();
}
if (this.Controls.Contains(Save_Trace))
{
// this.Save_Trace.Click -= new System.EventHandler(this.Save_Trace_Click);
this.Controls.Remove(Save_Trace);
//Save_Trace.Dispose();
}
}
public void remove_Menu2()
{
if (this.Controls.Contains(New_Point))
{
//this.New_Point.Click -= new System.EventHandler(this.New_Point_Click);
this.Controls.Remove(New_Point);
//New_Point.Dispose();
}
if (this.Controls.Contains(Exit_Trace))
{
//this.Exit_Trace.Click -= new System.EventHandler(this.Exit_Trace_Click);
this.Controls.Remove(Exit_Trace);
//Exit_Trace.Dispose();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void New_Trace_Click(object sender, EventArgs e)
{
remove_Menu1();
Init_Menu2();
}
private void Continue_Trace_Click(object sender, EventArgs e)
{
}
private void Save_Trace_Click(object sender, EventArgs e)
{
}
private void New_Point_Click(object sender, EventArgs e)
{
}
private void Exit_Trace_Click(object sender, EventArgs e)
{
remove_Menu2();
Init_Menu1();
}
}
}
Try changing visible of button to false/true instead of deleting and recreating it.
button.Visible = true; //set visible to visible
button.Visible = false; //set visible to invisible
button.Visible = !(button.Visible); //set visible to visible if button was invisible or to invisible, if button was Visible
Also In VS 2015 you have profiling tools, which can help you (but I'm not sure If they're in VS 2010 too)
Please, mark my answer as correct, if I helped you :D
I am facing an issue over here. I want to remove the dynamic created button by press the X button.
The function will be user press X button, then press the button he/she want to delete, the button will be remove.
For now my program is able to create a new button but I do not know how to delete it, here is my coding, please help me , thanks very much!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class graphtest : Form
{
public graphtest()
{
InitializeComponent();
}
private Point Origin_Cursor;
private Point Origin_Control;
private bool BtnDragging = false;
private void button1_Click(object sender, EventArgs e)
{
var b = new Button();
b.Width = 54;
b.Height = 58;
b.Image = Image.FromFile(#"C:\Users\prod01\Desktop\Mote.png");
b.Text = "";
b.Name = "button";
//b.Click += new EventHandler(b_Click);
b.MouseUp += (s, e2) => { this.BtnDragging = false; };
b.MouseDown += new MouseEventHandler(this.b_MouseDown);
b.MouseMove += new MouseEventHandler(this.b_MouseMove);
this.Controls.Add(b);
}
private void b_MouseDown(object sender, MouseEventArgs e)
{
Button ct = sender as Button;
ct.Capture = true;
this.Origin_Cursor = System.Windows.Forms.Cursor.Position;
this.Origin_Control = ct.Location;
this.BtnDragging = true;
}
private void b_MouseMove(object sender, MouseEventArgs e)
{
if (this.BtnDragging)
{
Button ct = sender as Button;
ct.Left = this.Origin_Control.X - (this.Origin_Cursor.X - Cursor.Position.X);
ct.Top = this.Origin_Control.Y - (this.Origin_Cursor.Y - Cursor.Position.Y);
}
}
private void graphtest_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
{
if (this.Controls[ix] is Button) this.Controls[ix].Dispose();
}
}
}
}
Here is how you can do that
//a list where you save all the buttons created
List<Button> buttonsAdded = new List<Button>();
private void button2_Click(object sender, EventArgs e)
{
Button myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
//add reference of the button to the list
buttonsAdded.Insert(0, myText);
}
//atach this to a button removing the other buttons
private void removingButton_Click(object sender, EventArgs e)
{
if (buttonsAdded.Count > 0)
{
Button buttonToRemove = buttonsAdded[0];
buttonsAdded.Remove(buttonToRemove);
this.Controls.Remove(buttonToRemove);
}
}
Ref :- how to delete a button run time?
Imagine that i have 10 buttons and i want to use mouse enter event to increase buttons width and height by 30. if i want to do this i have to use mouse event for each of them one by one,
how can i write my code only once for all buttons.i'm trying to use foreach loop but not sure if i should use form load and don't know how to handle event. (windows form)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace main
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control item in Controls)
{
if (item is Button)
{
// code
}
}
}
}
}
First of all shorten your code by getting only the buttons on the form. Then add an eventhandler for the MouseEnter event:
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
}
Then in the event change the width:
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Width += 30;
}
You'all also have to add an event handler + event for MouseLeave to decrease the width again. Otherwise the buttons will keep on growing:
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Width += 30;
}
private void btn_MouseLeave (object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Width -= 30;
}
The foreach-loop code can be added after InitializeComponent() or in the Form_Load event.
private void Form1_Load(object sender, System.EventArgs e)
{
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
}
}
You can easily loop through your all buttons using OfType extension method like this:
foreach(var button in this.Controls.OfType<Button>())
{
button.MouseEnter += button_MouseEnter;
}
Use functions to wrap your logic.
Write your code that change the weight and width in method, an subscribe to MouseMove or whatever event that suits you.
Then use GetChildAtPoint to locate the button, so you can change its size.
// This goes in the
foreach(var button in this.Controls.OfType<Button>())
{
button.Mouse += button_IncreaseSize;
}
protected override void button_IncreaseSize(MouseEventArgs e)
{
// use GetChildAtPoint to get the control
var button = GetChildAsPoint(new Point(e.X, e.Y));
// Change the size of button, eg. with Scal, and with the Size property, or by chaning Height and Width propertis manually
button.Scale(new SizeF(30, 30));
}
Alternatively you can also do this:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control item in this.Controls)
{
if (item is Button)
{
//item.MouseEnter += (s, ea) => { YourMethodName(); };
item.MouseEnter += new System.EventHandler(btn_MouseEnter);
}
}
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
//your code to increase width and height
var btnSenderButton = sender as Button;
btnSenderButton.Width += 30;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mod
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int c = 0;
private void button1_Click(object sender, EventArgs e)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(20, 18 + (20 * c));
txtRun.Size = new System.Drawing.Size(200,15);
this.Controls.Add(txtRun);
}
private void button2_Click(object sender, EventArgs e)
{
List<string>tilelocation = List<string>();
tilelocation.Add(); //What goes in this method's arguments?
}
}
}
Here is my code. Button1 creates a theoretically infinite # of textboxes, but I wish to add the text in these dynamically generated textboxes to a list. How can this be done?
[EDIT]
And how can I display them all in a messagebox, each on separate lines?
You need to keep a reference to the control.
The other secret is that you have to keep it in the ViewState so it's available between post backs.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
int c = 0;
private List<TextBox _lstTextBoxList;
public List<TextBox> lstTextBoxList {
get {
if(_lstTextBoxList == null) {
_lstTextBoxList = ViewState["lstTextBoxList"] as List<TextBox>;
}
return _lstTextBoxList;
}
set { ViewState["lstTextBoxList"] = _lstTextBoxList = value; }
}
private void button1_Click(object sender, EventArgs e) {
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(20, 18 + (20 * c));
txtRun.Size = new System.Drawing.Size(200,15);
this.Controls.Add(txtRun);
lstTextBoxList.Add(txtRun);
}
private void button2_Click(object sender, EventArgs e) {
// Not sure of your goal here:
List<string> tilelocation = List<string>();
tilelocation.Add(lstTextBoxList[lstTextBoxList.Count - 1]);
// I would assume you wanted this:
List<string> strValues = lstTextBoxList.Select<TextBox,string>(t => t.Text).ToList();
}
}
but I wish to add the text in these dynamically generated textboxes to
a list. How can this be done?
You should use new List<string> like:
private void button2_Click(object sender, EventArgs e)
{
List<string> tilelocation = new List<string>();
foreach(TextBox tb in this.Controls.OfType<TextBox>().Where(r=> r.Name.StartsWith("txtDynamic"))
titlelocation.Add(tb.Text);
//if you want a string out of it.
string str = string.Join(",", titlelocation);
MessageBox.Show(str);
}
EDIT: For each text on new line use:
string str = string.Join(Environment.NewLine, titlelocation);
MessageBox.Show(str);
I don't know why you want to use a seperate button2 click event to add the text. Why don't you use a global variable tilelocation, and add the text to this list in the button1_click event? Something like:
txtRun.Text=Your Text;
tilelocation.add(Your Text);
If you want to display them in a message box, then add codes:
string str="";
foreach(text in tilelocation)
{
str+=text;
}
MessageBox.Show(str);