How to add Buttons in WinForm in Runtime? - c#

I have the following Code :
public GUIWevbDav()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//My XML Loading and other Code Here
//Trying to add Buttons here
if (DisplayNameNodes.Count > 0)
{
for (int i = 0; i < DisplayNameNodes.Count; i++)
{
Button folderButton = new Button();
folderButton.Width = 150;
folderButton.Height = 70;
folderButton.ForeColor = Color.Black;
folderButton.Text = DisplayNameNodes[i].InnerText;
Now trying to do GUIWevbDav.Controls.Add
(unable to get GUIWevbDav.Controls method )
}
}
I dont want to create a form at run time but add the dynamically created buttons to my Current Winform i.e: GUIWevDav
Thanks

Just use this.Controls.Add(folderButton). this is your form.

Problem in your code is that you're trying to call Controls.Add() method on GUIWevbDav which is the type of your form and you can't get Control.Add on a type, it's not a static method. It only works on instances.
for (int i = 0; i < DisplayNameNodes.Count; i++)
{
Button folderButton = new Button();
folderButton.Width = 150;
folderButton.Height = 70;
folderButton.ForeColor = Color.Black;
folderButton.Text = DisplayNameNodes[i].InnerText;
//This will work and add button to your Form.
this.Controls.Add(folderButton );
//you can't get Control.Add on a type, it's not a static method. It only works on instances.
//GUIWevbDav.Controls.Add
}

You need to work with Control.Controls property.
In Form Class Members you can see Controls property.
Use it like this :
this.Controls.Add(folderButton); // "this" is your form class object.

Related

How to assign tab order from left to right - winforms C#

I have a windows form in C# project that keeps some information. I created a bunch of textbox and combobox dynamically, depend upon user input.
So here there is two rows since user has given the input as 2. All the components in the image are dynamically created. For each component i have created a class to set the property and its behaviour.
Now the issue is I need to traverse the component using tab.
When i tried to set tabindex = 1 for the first textbox and tabindex = 2 for the second textbox. I'm traversing the components vertically like mentioned below
Actual Output : enter image description here
The code in which i have added are following.
public class addDynamicCptboxComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_txtBox" + getNoOfTxtBox;
//assigned the tabindex as 2 for the second textbox
txtBox.TabIndex = 2;
}
}
public class addDynamicDateofServiceComponents : add_components
{
public override void add_dynamic_components(int getNoOfTxtBox, int pointX, int pointY, Form1 f)
{
TextBox txtBox = new TextBox();
f.panel1.Controls.Add(txtBox);
txtBox.Location = new Point(pointX, pointY);
txtBox.Size = new System.Drawing.Size(75, 23);
f.panel1.Controls.Add(txtBox);
txtBox.Name = "Add_dos_txtBox" + getNoOfTxtBox;
//assigned the tabindex as 1 for first textbox
txtBox.TabIndex = 1;
}
}
But what i need is , I need to traverse the components horizontally as mentioned below.
Expected Ouput: enter image description here
The Requried tab order is specified in the above image.
Guessing from the name of your class you are adding rows dynamically to your form. But since you are hard coding the tab index the result per row looks like in your expected output. This means by tabbing you go from index 1 to index 1 to index 2 to index 2 and so on and so forth.
I'd advise you to have an incrementing tab index stored somewhere in your application which is incremented after it is assigned to a new dynamically created control.
As a really simple example I created a fresh forms project which just has two buttons. The first one adds a new textbox and the second button switches into a new row. And in this example everything has the tab index you require. The code behind looks like this:
public partial class Form1 : Form
{
private int currentX = 0;
private int currentY = 0;
private const int tbWidth = 75;
private const int tbHeight = 23;
private int currentTabIndex = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var textBoxOne = new TextBox();
this.Controls.Add(textBoxOne);
textBoxOne.Location = new Point(this.currentX, this.currentY);
textBoxOne.Size = new Size(tbWidth, tbHeight);
textBoxOne.TabIndex = currentTabIndex;
textBoxOne.Text = $"{currentTabIndex}";
currentTabIndex++;
this.currentX += tbWidth + 5;
}
private void button2_Click(object sender, EventArgs e)
{
currentY += tbHeight + 5;
currentX = 0;
}
}
Please bare in mind this is just a simple example. I could help you better in the context of your application if I knew more about it.

C# How to define event for dynamically created control?

I have written a code that create buttons dynamically. The code works well, and create controls when I click on a button. Now the next task is, I want to define click event for these dynamically created controls. How can I do this ? Below is the code, Please modify this code and paste in a reply, so that I can understand easily.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// create controls dynamically on form
int n = 4;
private void btnDisplay_Click_1(object sender, EventArgs e)
{
Button[] button = new Button[n];
int previousButtonPositionY;
int previousButtonHeight;
for (int i = 0; i < n; i++)
{
button[i] = new Button();
button[i].Name = "btnButton" + i;
button[i].Text = "btnButton" + i;
if (i > 0)
{
previousButtonPositionY = button[i - 1].Location.Y;
previousButtonHeight = button[i - 1].Height;
}
else
{
previousButtonPositionY = 50;
previousButtonHeight = 0;
}
button[i].Location = new Point(0, previousButtonPositionY + previousButtonHeight);
}
for (int i = 0; i < n; i++)
{
panel1.Controls.Add(button[i]);
}
}
}
After you have initialized your button you can add an onclick event to it with
button[i].Click += new EventHandler(button_Click);
More here: http://msdn.microsoft.com/en-us/library/ms743596%28v=vs.110%29.aspx

change local function

I have problem with my C# WinForms project. I have a function that should change the place of buttons if they touch each other. For example, if I have btn1 at oldloction = (4,2) and btn2 at oldlocaction (2,6), then if I will move the buttons and they will touch bt1 new location = (2,6) and bt2 new location = (4,2)
now i did that with 2 buttons and it works.
locationx - means the x location on the button and its orgenize firat place of the location feat to the first buttons[0], the second feat to locationx[1] = buttons[1].location.x;
location - works the same ass locationx but uts the y locaion.
private void myText_MouseUp(object sender, MouseEventArgs e)
{
Point oldlocation = new Point(locationx[0], locationy[0]);
Point oldlocation2 = new Point(locationx[1], locationy[1]);
if (buttons[0].Location.Y == buttons[1].Location.Y)
{
buttons[1].Location = oldlocation;
buttons[0].Location = oldlocation2;
}
}
When I tried to make that as a global function it doesn't work and I don't know why.
This is the code of the global function that doesn't work:
private void myText_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < counter; i++)
{
Point oldlocation = new Point(locationx[i], locationy[i]);
for (int j = 0; j < counter; j++)
{
if (i != j)
{
Point oldlocation2 = new Point(locationx[j], locationy[j]);
if (buttons[i].Location.Y != buttons[j].Location.Y)
{
buttons[j].Location = oldlocation2;
buttons[i].Location = oldlocation;
}
else if (buttons[i].Location.Y == buttons[j].Location.Y)
{
buttons[j].Location = oldlocation;
buttons[i].Location = oldlocation2;
}
}
}
}
}
Try using the button event for when it is pressed to call the function, rather than creating your own.
If the second function is not part of the control or form containing the buttons, it won't have a way to access the buttons array or locationx and locationy. You may need to pass these values as arguments to your function or ensure that they are provided as members of the class containing the second function. Note that generally a utility function would not take in "sender" and "MouseEventArgs" - pass only the specific data that the utility function needs to do its job.

Create button during runtime in C#.net?

I know how to create button during runtime.
Button button1 = new Button();
button1.Location = new Point(20,10);
button1.Text = "Click Me";
// adding to groupBox1
groupBox1.Controls.Add(button1);
But the problem is i want to add multiple buttons like this..
for(int i = 1; i < 30; i++) {
Button button[i] = new Button();
// Button customization here...
...
groupBox1.Controls.Add(button[i]);
}
The code above is false code. How can I make this happen true in C#.net? i want to create multiple buttons with button name, button1, button2, button3, button4, .... button30;
You can't declare extra variables at execution time in C# - but you really don't want to anyway, as you wouldn't be able to access them dynamically afterwards. Just create an array:
// buttons would be declared as Button[] as a member variable
buttons = new Button[30];
for(int i = 0; i < buttons.Length; i++) {
buttons[i] = new Button();
// Button customization here...
...
groupBox1.Controls.Add(buttons[i]);
}
Alternatively, use a List<Button>, which will certainly be more convenient if you don't know how many buttons you need beforehand. (See the obligatory "arrays considered somewhat harmful" blog post.)
Of course, if you don't actually need to get at the buttons later, don't bother assigning them to anything visible outside the loop:
for(int i = 0; i < 30; i++) {
Button button = new Button();
// Button customization here...
...
groupBox1.Controls.Add(button);
}
You need to think about what information you need access to when... and how you want to access it. If you logically have a collection of buttons, you should use a collection type variable (like a list or an array).
Frankly I think it's one of the curses of the VS designers that you end up with horrible names such as "groupBox1" which carry no information beyond what's already in the type declaration, and encourage developers to think of collections of controls via individually-named variables. That's just me being grumpy though :)
Try this
for(int i = 1; i < 30; i++) {
Button button = new Button();
// Button customization here...
button.Name = "Button" + i.ToString();
groupBox1.Controls.Add(button);
}
You seem like you're almost on the right track:
// in form class
Button[] m_newButtons = new Button[30];
// in your trigger function
for(int i = 0; i < 30; ++i)
{
m_newButtons[i] = new Button();
// ...
groupBox1.Controls.Add(m_newButtons[i]);
}
If you try and do this more than once you may have to remove the old buttons from the control before adding the new ones.
buttons = new Button[30];
for(int i = 0; i < buttons.Length; i++) {
buttons[i] = new Button();
groupBox1.Controls.Add(buttons[i]);
}
this code will work but button will be added one over other so set location also
buttons = new Button[30];
for(int i = 0; i < buttons.Length; i++)
{
buttons[i] = new Button();
Point p=new Point(xvalue,yvalue);
buttons[i].Location = p;
groupBox1.Controls.Add(buttons[i]);
}
one thing you want to remember increment the x or y position by which you want to display it
Try this one out, I have just learned it myself:
public partial class Form1 : Form
{
Button[] btn = new Button[12];// <--------<<<Button Array
public Form1()
{
InitializeComponent();
}
private void Form1_Load (object sender, EventArgs e)
{
for (int i = 0; i < 12; i++)
{
btn[i] = new Button ( );
this.flowLayoutPanel1.Controls.Add(btn[i]);
}
}
// double click on the flow layoutPannel initiates this code
private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
}

Passing a Variable to an Event in .NET

For example I have a list of picture boxes that raise an event once the cursor hover over them.
Yet I need somehow not only to rise this event, but also to pass the "i" variable, so that I would know which picturebox has the cursor over it.
for (int i = 0; i < 25; i++)
{
....
pbList.Add(new PictureBox());
pbList[i].MouseHover += new System.EventHandler(this.beeHideInfo);
//// need to pass "i" here
}
and
private void beeShowInfo(object sender, EventArgs e)
{
lb_beeInfo.Text = "You are hovering over: "+beeList[i].name;
/// need to get this "i"
}
Any ideas?
You cannot pass variables to an event.
Besides, the necessary variable has already been passed to you: sender.
sender is a reference to the object which raised the event. In your case, it's a reference to the PictureBox that raised the event.
The object sender parameter is the PictureBox sending the event. If you need to associate something with that object, you can use its Tag member:
for (int i = 0; i < 25; i++)
{
....
pbList.Add(new PictureBox() { Tag = beeList[i] });
pbList[i].MouseHover += new System.EventHandler(this.beeHideInfo);
}
and
private void beeShowInfo(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
Bee b = (Bee)pb.Tag;
lb_beeInfo.Text = "You are hovering over: "+b.name;
}
Assuming pbList and beelist contain related items in the same order, you can do something like beeList[ pbList.IndexOf(sender) ].name
You could you do something like:
for (int i = 0; i < 25; i++)
{
...
pbList.Add(new PictureBox());
var index = i;
pbList[i].MouseHover +=
delegate
{
lb_beeInfo.Text = "You are hovering over: "+beeList[index].name;
};
}
I.e. use an anonymous method.
As John Saunders says, there is an easier solution.
The easiest way to do this is use an anonymous function to explicitly pass the PictureBox or index instance into the handler.
for (int i = 0; i < 25; i++)
{
....
var box = new PictureBox();
pbList.Add(box);
box.MouseHover += delegate { this.beeShowInfo(box); }
}
private void beeShowInfo(PictureBox box)
{
lb_beeInfo.Text = "You are hovering over: "+box.Name;
}
another way might be if you create a Custom Picture Box
class CustomPictureBox : PictureBox
{
public int id;
public CustomPictureBox(int ID)
{
id = ID;
}
}
firt place a GLOBAL id to parent and each time if a CustomPicureBox is clicked get the ID
than everywhere you want to make changes to Clicked CustomPicutreBox test it
foreach(CustomPicutreBox i in Control.controls)
{
if(i.ID == sender.ID)
doWhatEveryYouWant();
}
}

Categories