Ok so here is my designer.cs code
for (int i = 0; i < textBoxes.Length; i++)
{
textBoxes[i] = new System.Windows.Forms.TextBox();
this.textBoxes[i].Location = new System.Drawing.Point(90, 50 + i * 20);
this.textBoxes[i].Name = "textBox" + i;
this.textBoxes[i].Size = new System.Drawing.Size(100, 20);
this.textBoxes[i].TabIndex = i + 1;
this.Controls.Add(textBoxes[i]);
}
This was edited code below the Windows Generated code
private System.Windows.Forms.TextBox[] textBoxes = new System.Windows.Forms.TextBox[5];
I deleted any code that is related to any of my textboxes.
Instead it's giving me this error when I go to the design view:
Games MoreGames.Designer.cs Line:32 Column:1
screenshot of error
The program can run but why wouldn't it let me access the designer so I can move things around?
As Joel says, you should place that code in the constructor following the InitializeComponent() method in your MoreGames.cs file (NOT MoreGames.Designer.cs which cannot be edited) but you might also want to add the following:
textboxes[i].Parent = this;
That will tell each of your textboxes that the form is it's parent.
The basic namespace of your Form will look something like this:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private System.Windows.Forms.TextBox[] textBoxes = new System.Windows.Forms.TextBox[5];
public Form1()
{
InitializeComponent();
for (int i = 0; i < textBoxes.Length; i++)
{
textBoxes[i] = new System.Windows.Forms.TextBox();
this.textBoxes[i].Location = new System.Drawing.Point(90, 50 + i * 20);
this.textBoxes[i].Name = "textBox" + i;
this.textBoxes[i].Size = new System.Drawing.Size(100, 20);
this.textBoxes[i].TabIndex = i + 1;
this.textBoxes[i].Parent = this;
this.Controls.Add(textBoxes[i]);
}
}
}
}
You cannot edit the designer.cs file. Or rather, you can edit the file, but any changes you make will be overwritten every time the designer needs to regenerate the file, and certain things in the file will prevent the visual designer from rendering your form properly. This is by design.
What you should do instead is put your code in your form's constructor, immediately following the call to the InitializeComponent() method. The new controls won't show up on the screen for you to drag around, but they will be there when you actually run the program.
Moreover, it sounds like what you really need here is something that is more data driven, like a FlowLayoutPanel, that you can add and remove controls from at run time.
Related
i have buttons this buttons for categories i want when click in this buttons i want to get rows from database products table and get two data product name and price and set it those values into dynamic buttons in windows application notes i have set location properities for button such like flowLayoutPanel
i have this code
private void btnhotdrink_Click_1(object sender, EventArgs e)
{
//int StartPositition = 100;
//int EndPosition = 10;
DataTable dt = new DataTable();
dt =clsprdcat.GET_HOTDRINK_CATEGORY();
for(int i=0; i < dt.Rows.Count; i++)
{
for (int s=0; s < dt.Columns[4]; s++)
{
Button l = addbuttons(i,s);
flowLayoutPanel1.Controls.Add(l);
//EndPosition +=70;
}
}
}
Button addbuttons(int i)
{
Button l = new Button();
l.Name = "Name" + l.ToString();
l.Text = "label" + l.ToString();
l.ForeColor = Color.White;
l.BackColor = Color.Green;
l.Font = new Font("Serif", 24, FontStyle.Bold);
l.Width = 170;
l.Height = 80;
//l.Location = new Point(start, end);
l.TextAlign = ContentAlignment.MiddleCenter;
l.Margin = new Padding(5);
return l;
}
How Can i Do this
All Buttons are inherently dynamically created at runtime. The Windows Forms Designer works via partial classes: You work on one part. The designer on the other (designer.cs). And at compile time the Designers code is executed by calling "InitializeComponents()" in the Constructor. It can only do exactly the same things you can do.
If you need help with creating buttons yourself, you can always look into the Designer part. Just do take care not to change anything in that part. This System only works because the designer is exclusively writing in the file and compilation/loading of a Project is prone to seizing up if you did manual changes.
As you wrote it right now, this code will create dt.Rows.Count buttons at the same locaiton. Usually the Button creation code it needs access to the loop variable, to adapt the positions of each consequtive element.. Personally I prefer to leave positioning of Elements as much to automatics as possible. These designs simply adapt better to changes in resolution and window size.
I have created a form that has generated several UserControls. There has to be an unlimited amount that can be added - there wont necessarily, but there needs to not be a cap on the amount.
I have used the following code to add them:
for (int i = 0; i < ucCount; i++)
{
UserControl1 uc = new UserControl1();
uc.name = "uc" + i.ToString();
flowLayout.Controls.Add(uc);
uc.Click -= new EventHandler(uc_Click);
uc.Click += new EventHandler(uc_Click);
}
ucCount is simply a variable to change the amount that are added.
The problem I'm having now, is I have a method in the UserControls that needs to be run when a button is pressed. This is the code for the following method:
public void ResizeTrucks()
{
...
}
Normally, if I had added the controls with a special name, I would have done the following:
uc.ResizeTrucks();
Since I added them in the code, and changed their names, I can no longer do that.
I now have the following code to change the size of all the usercontrols.
foreach (Control c in flowTrucks.Controls)
{
c.Width = 103;
c.Height = 528;
}
Basically, the problem that I'm facing is running a function in a usercontrol that I have generated in code, so I can't just do uc1.ResizeTrucks();.
I think I remember reading somewhere that you can get the function by name and then run it, but I haven't been able to find it anywhere. Any help will be appreciated.
You want to cast to the appropriate user control type, and then you can run the function. Something like:
foreach (Control c in flowTrucks.Controls)
{
c.Width = 103;
c.Height = 528;
var x = c as UserControl1;
if (x == null) continue; // not a UserControl1
x.ResizeTrucks();
}
Basically I'm making a program that allows you to add to a stackpanel another stackpanel with several horizontally aligned textboxes with the press of a button. So far, everything is working as intented. Here's my code so far ,Stacker is the name of the parent stackpanel and it starts off empty:
private void Add_Click(object sender, RoutedEventArgs e)
{
Stacker.Children.Add(NewXD(Stacker.Children.Count + 1));
}
public System.Windows.Controls.StackPanel NewXD(int XD)
{
System.Windows.Controls.StackPanel NewP = new StackPanel();
NewP.Orientation = Orientation.Horizontal;
System.Windows.Controls.TextBox HAHA = new TextBox();
HAHA.Name = "question" + XD.ToString();
//HAHA.Text = HAHA.Height.ToString()+" "+HAHA.Width.ToString();
HAHA.Height = Double.NaN;
HAHA.Width = 120;
HAHA.FontSize=20;
NewP.Children.Add(HAHA);
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.TextBox newBox = new TextBox();
newBox.Name = "answer"+XD.ToString()+"_"+i.ToString();
newBox.Height = Double.NaN;
newBox.Width = 120;
NewP.Children.Add(newBox);
}
System.Windows.Controls.ComboBox correct = new ComboBox();
correct.Name = "correct" + XD.ToString();
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.ComboBoxItem newItem = new ComboBoxItem();
newItem.Name = "ans" + XD.ToString() + "_" + i.ToString();
newItem.Content = i.ToString();
correct.Items.Add(newItem);
}
NewP.Children.Add(correct);
return NewP;
}
I apologize for the lack of seriousness in some of my code.
Now, what I need to do is for the child stackpanels to also contain independent file pickers that work like the one sampled in this thread: Open file dialog and select a file using WPF controls and C#
What I don't know how to perform is that each of these generated buttons have the same basic funcion but are linked with each of their corresponding textbox.
Thanks in advance :)
Edit: As I was writing this it occured to me that perhaps I could use the help of the child stackpanel's array-like properties to choose the corresponding textbox, because the file selector's textbox and button will always be the last two items in the stackpanel, but I'm not very sure how to perform this.
For functionality you can create an EventHandler that will be assigned to each button. Your event handler will then open File Dialog...
Buttons have Tag property which you could use to identify your TextBoxes, or you could derive from Button class and add AssociatedTextBox property for example.
Initially I was rather confused. I am starting to understand my code is skipping the second for because my NUD array is static. My setup is this.
Controls.cs is a partial class for AranockCompanion.cs my main winform. I have all of my controls (labels, numiercUpDownS, buttons, textBoxs) which are not created in the designer (programmatically rather) in this file. Then I have 1 file for Enums.cs and one for GameData.cs (basically initializing arrays with information) and one for basic FileIO.cs.
Because of my fileWriting method to dump information about items into a file, I had to make it static. I think it had something to do with the StreamWriter IIRC. So then I had to make my array static/public in the Controls class to access it. Now because it is static.. this happens.
I feel like there is some rather basic underlying issue with static / new / objects and the fundamentals of OOP and the general design of how I have my project setup, but I cannot put my finger on it for the life of me.
Why would the code skip executing a loop prematurely without any type of error and successfully launch the program???
//Equipment Tab Page
Label[] equipLabels = new Label[skillTotal];
public static NumericUpDown[] equipCore = new NumericUpDown[skillTotal];
NumericUpDown[] equipReq = new NumericUpDown[skillTotal];
#region Equipment
MessageBox.Show("0_" + skillTotal);
x = labelEquipSkill.Location.X - panelEquip.Location.X;
for (int skillCount = 0; skillCount < skillTotal; skillCount++)
{
equipLabels[skillCount] = new Label();
panelEquip.Controls.Add(equipLabels[skillCount]);
equipLabels[skillCount].Text = GameData.skillsAll[skillCount];
equipLabels[skillCount].Location = new System.Drawing.Point(x, skillCount * spacer);
equipLabels[skillCount].Size = new System.Drawing.Size(50, 15);
}
MessageBox.Show("1_" + skillTotal);
x = labelEquipBase.Location.X - panelEquip.Location.X ;
for (int skillCount = 0; skillCount < skillTotal; skillCount++)
{
equipCore[skillCount] = new NumericUpDown();
panelEquip.Controls.Add(equipCore[skillCount]);
equipCore[skillCount].Minimum = 0;
equipCore[skillCount].Maximum = 255;
equipCore[skillCount].Value = 0;
//equipCore[skillCount].Text = GameData.skillsAll[skillCount];
equipCore[skillCount].Location = new System.Drawing.Point(x, skillCount * spacer);
equipCore[skillCount].Size = new System.Drawing.Size(50, 15);
}
MessageBox.Show("2_" + skillTotal);
x = labelEquipReq.Location.X - panelEquip.Location.X;
for (int skillCount = 0; skillCount < skillTotal; skillCount++)
{
equipReq[skillCount] = new NumericUpDown();
panelEquip.Controls.Add(equipReq[skillCount]);
equipReq[skillCount].Minimum = 0;
equipReq[skillCount].Maximum = 255;
equipReq[skillCount].Value = 0;
//equipReq[skillCount].Text = GameData.skillsAll[skillCount];
equipReq[skillCount].Location = new System.Drawing.Point(x, skillCount * spacer);
equipReq[skillCount].Size = new System.Drawing.Size(50, 15);
}
#endregion
private void buttonVisible_Click(object sender, EventArgs e)
{
for (int rc = 0; 0 < skillTotal; rc++)
{
MessageBox.Show("" + rc);
//equipCore[rc].Visible = true;
equipReq[rc].Visible = true;
}
}
equipCore[rc].Visible results in IndexOutOfRangeException.
equipReq[rc].Visible results in Object reference not set to an instance of an object.
Both at index 0.
Which would logically follow that the 3rd for statement is never executed because of an error during the 2nd for. I confirmed the 3rd is fine by commenting out the 2nd.
Any explanation or help would be appreciated.
With such a vague, not-very-useful code example, it's impossible to say for sure what is wrong.
That said, IndexOutOfRangeException, along with that apparently you are initializing the equipCore array once, using a field initializer, suggests that at the time it's initialized, the skillTotal variable is still set to its default value of 0, instead of the 59 you expected to be.
As far as the NullReferenceException, that's to be expected if your code throws an exception before populating any of the members of equipReq. I presume that if you fix the initialization of equipCore, both exceptions will go away.
I hesitate to even post the above as an answer, except that it seems to be about as specific an answer as could be provided given the code example. If this is not enough information to help you find the error in your code, please post a (much) better code example. See How to create a Minimal, Complete, and Verifiable example and How do I ask a good question? for good advice on how to post a good, useful question.
I'm sure there's a way to do it, I just haven't been able to work it out for myself and searching the site hasn't shown me what I need to know. Maybe I'm just using the wrong keywords.
I am trying to add controls to a form during execution. I would like to create new controls for the number displayed in a numericUpDown. E.g. if the user inputs 3, 3 controls should be created.
Is it something in Form.ActiveForm.* ?
Thanks.
Instead of "elements", I think you are referring to "controls".
The general way is:
TextBox textBox = new TextBox();
textBox.Location = Some Point on your form or container.
this.Controls.Add(textBox);
For your extra numbers, just do that in a loop:
int topValue = 0;
for (int i = 0; i < numericUpDown1.Value; i++) {
TextBox textbox = new TextBox();
textBox.Location = new Point(0, topValue);
this.Controls.Add(textBox);
topValue += textBox.Height + 2;
}
Do you mean something as simple as this?
numericUpDown1.Maximum = int.Parse(textBox1.Text);
If not, please elaborate.