If I want to create a winform with dynamic UI controls appearing, what's the best way to do that?
I have a form with a textbox, a button1 to the right of it, a listbox underneath, and a button2 underneath the listbox. Pressing button1 should generate another textbox underneath the first textbox and the listbox/button2 should be shifted down. If anyone's used Adobe Bridge before, the batch rename window is an example of what I'm talking about.
I was thinking of simply adding textboxN.Height to this.Size, then textboxN.Height to each of the controls (except the first textbox) Y position so they all get shifted down by textboxN.Height pixels. But I think there's a better way of doing this. Rather, is Winforms suitable for something like this?
You -could- just add the height of the TextBox to the form's size, but tbh it would be better to use a constant that dictates the size of the TextBoxes, and then add that.
For moving the listBox/button2, anchor them to the bottom of the form, and they'll automatically stay at the same distance from the bottom of the form.
As for dynamic generation, use a List (or a Stack, depending on what exactly you're doing with it).
partial class Form1 : Form
{
List<TextBox> textBoxes = new List<TextBox>(); // or stack
const int textBoxWidth = 200; // control variables for TextBox placement
const int textBoxHeight = 50;
const int textBoxMargin = 5;
void button1_Click(object sender, EventArgs e)
{
this.Height += textBoxHeight + textBoxMargin;
TextBox tb = new TextBox();
if (textBoxes.Count == 0)
{
tb.Top = textBoxMargin;
}
else
{
tb.Top = ((textBoxHeight + textBoxMargin) * textBoxes.Count) + textBoxMargin;
}
tb.Left = textBoxMargin;
tb.Height = textBoxHeight;
tb.Width = textBoxWidth;
textBoxes.Add(tb);
this.Controls.Add(tb);
}
}
That should work. The thing with the method here is pretty much all of the placement customisation can be done with the constant values.
Is it best to do it in WinForms? Well, there's certainly no real reason to not do it in WinForms, this functionality is easy enough to implement. I'm a WPF guy myself but this is still legit.
Edited for logic errors
Related
Hey so I am attempting to create a form in which there are two buttons (acting as tabs) on the side in which when one is pressed specific controls are shown and when the other is pressed, another set are shown.
I have gotten this to work very well however I have run into an issue when about to create my second set of controls, how am I going to draw them (in designer) ontop of current controls (it's fine during runtime)? So my question is, how can I make this work. Current (possibly) important code:
private void CheatButton_Click(object sender, EventArgs e)
{
CheatControls(true);
ColorControls(false);
CheatButton.Normalcolor = Color.FromArgb(51, 51, 51);
ColorButton.Normalcolor = Color.FromArgb(61, 61, 61);
}
private void ColorButton_Click(object sender, EventArgs e)
{
CheatControls(false);
ColorControls(true);
CheatButton.Normalcolor = Color.FromArgb(61, 61, 61);
ColorButton.Normalcolor = Color.FromArgb(51, 51, 51);
}
private void CheatControls(bool b)
{
TriggerSwitch.Visible = b;
TriggerLabel.Visible = b;
BhopSwitch.Visible = b;
BhopLabel.Visible = b;
GlowSwitch.Visible = b;
GlowLabel.Visible = b;
RecoilSwitch.Visible = b;
RecoilLabel.Visible = b;
}
private void ColorControls(bool c)
{
}
My windows form application with an understandable graphic
Put all your cheat controls in a custom UserControl and all your color controls in a different custom UserControl. You can edit the different control sets easily in the designer, and in your code you can hide/show them more easily by setting the appropriate UserControl's visibility rather than setting the visibility of a bunch of controls individually. Here's an (admittedly old) example of creating a UserControl: msdn.microsoft.com/en-us/library/aa302342.aspx
When you create a UserControl in Visual Studio, you can add the controls to it using the designer. In your form with the tab buttons, you would add instances of the two UserControls that you created. Since UserControl derives from Control, it has a Visible property. So, in your CheatControls(bool b) method, your implementation would be simplified to something like _myCheatControls.Visible = b;. I haven't watched the whole thing, but this video might help, too: youtube.com/watch?v=l5L_q_jI494
You can use
XanderUI
Simply add a SuperButton(acts as a tab button) and when its clicked show a panel with your controls in it
EG -
private void ShowControlSet(int ControlSet)
{
panel1.visible = false;
panel2.visible = false;
if (ControlSet == 1) panel1.visible = true;
if (ControlSet == 2) panel2.visible = true;
}
// To show a panel use
ShowControlSet(1);
Your also able to use BringToFront() instead of making each panel invisible / visible but you need to anchor or dock the panels correctly
asides the visibility, what you can do is get the position of the buttons and st them when hiding and showing the other buttons
just set the top and left positions, ensuring the buttons are the same size.
I'm writing a C# programming which has couple of textboxes which give some information from the user. I have a "Submit" button to save the entered information.
I want this textbox work like this:
A. When there is no information entered, by pressing the Submit button, a thick red border shown around the textbox.
B. when the user is been noticed, and try to enter some information in the corresponding textbox, after a certain number of letters (for example after entering at least 4 letters), the border style of the textbox will be switched to the regular style!
I will be appreciated if you could help me!
I searched a lot for TextBox border color but I couldn't find anything helpful.
First of all, whenever you ask something in SO, you have to put some effort and show what you´ve tried so that people will be willing to help you. Take that as a note for future questions.
Having said that, you have to add a handler to the TextChanged event of your TextBox control. Then you have to change the properties as you desire:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//PUT THE BUSINESS LOGIC IN HERE
if(textBox1.Text = "")
{
textBox1.BorderStyle = BorderStyle.None;
Pen p = new Pen(Color.Red);
Graphics g = e.Graphics;
int variance = 3;
g.DrawRectangle(p, new Rectangle(textBox1.Location.X - variance, textBox1.Location.Y - variance, textBox1.Width + variance, textBox1.Height +variance ));
}
else
{
textBox1.BorderStyle = BorderStyle.FixedSingle;
}
}
I've found similar answers to my question before, but not quite to what I'm trying to do...
In Visual Basic (last I used it, in 06/07) there was an "Index" property you could assign to multiple controls with the same name. I used this primarily to loop through controls, i.e.:
For i = 1 to 500
picSeat(i).Print "Hello"
Next i
Is there a way to do this in C#? I know there is a .IndexOf(), but would that really help for what I'm doing? I want to have multiple controls with the same name, just different index.
This is a Windows Form Application, and I'm using Visual Studio 2012. I am talking about controls, not arrays/lists; this was possible in VB and I was wondering if it was possible at all in C#. So I want to have, say, 30 seats in a theatre. I want to have each seat represented by a picturebox named "picSeat". VB would let me name several objects the exact same, and would assign a value to a control property "Index". That way, I could use the above loop to print "Hello" in every picture box with only 3 lines of code.
No, this feature does not exist in C#, and was never implemented in the transition from classic VB to VB.Net.
What I normally do instead is put each of the controls in question in a common parent container. The Form itself can work, but if you need to distinguish these from others of the same type a GroupBox or Panel control will work, too. Then, you access the controls like this:
foreach (var picBox in parentControl.Controls.OfType<PictureBox>())
{
// do something with each picturebox
}
If you want to use a specific control, just write by name:
pictureBox6.SomeProperty = someValue;
If you need to change a specific control determined at run-time, normally this is in response to a user event:
void PictureBox_Click(object sender, EventArgs e)
{
var picBox = sender As PictureBox;
if (picBox == null) return;
//picBox is now whichever box was clicked
// (assuming you set all your pictureboxes to use this handler)
}
If you really really want the Control Arrays feature, you can do it by adding code to create the array to your form's Load event:
PictureBox[] pictureBoxes = Me.Controls.OfType<PictureBox>().ToArray();
Are we talking WinForms here? I'm not sure, but I don't think you can have multiple controls in winforms with same name. But I vaguely recall doing something similar and the solution was to name them Button_1, Button_2 etc. Then you can iterate through all controls and get your own index.
Beware though that if you want to instanciate a separate control for each seat in a theatre, you might run into some serious performance issues :) I've done something similar to that as well and ended up drawing the whole thing on a canvas and using mouse coordinates to handle the events correctly.
You may want to check out the Uid property of controls.
(http://msdn.microsoft.com/en-us/library/system.windows.uielement.uid(v=vs.110).aspx)
You can access Control through Uid property with the following
private static UIElement FindUid(this DependencyObject parent, string uid)
{
var count = VisualTreeHelper.GetChildrenCount(parent);
if (count == 0) return null;
for (int i = 0; i < count; i++)
{
var el = VisualTreeHelper.GetChild(parent, i) as UIElement;
if (el == null) continue;
if (el.Uid == uid) return el;
el = el.FindUid(uid);
if (el != null) return el;
}
return null;
}
And simply use
var control = FindUid("someUid");
I copied code from this post
If you create an indexed dictionary of your user control, it will behave pretty much the same as in VB6, though you'll not see it on the VS C# GUI. You'll have to get around the placement issues manually. Still - and most importantly -, you'll be able to refer to any instance by the index.
The following example is for 3 pieces for clarity, but of course you could automate every step of the process with appropriate loops.
public partial class Form1 : Form
{
...
Dictionary<int, UserControl1> NameOfUserControlInstance = new Dictionary<int, UserControl1>()
{
{ 1, new UserControl1 {}},
{ 2, new UserControl1 {}},
{ 3, new UserControl1 {}}
};
private void Form1_Load(object sender, EventArgs e)
{
NameOfUserControlInstance[1].Location = new System.Drawing.Point(0, 0);
NameOfUserControlInstance[2].Location = new System.Drawing.Point(200, 0);
NameOfUserControlInstance[3].Location = new System.Drawing.Point(400, 0);
Controls.Add(NameOfUserControlInstance[1]);
Controls.Add(NameOfUserControlInstance[2]);
Controls.Add(NameOfUserControlInstance[3]);
}
...
}
I like using Tags to apply any type of meta data about the controls
for (int i = 0; i< 10; ++i)
{
Button button = new Button();
button.Tag = i;
}
I've got the following scenario, where I'd like to position Button1 right below and horizontally aligned to Button 2, but still keep Button1 in groupBox1, whereas Button 2 is in groupBox2:
I've seen several posts about PointToClient() and PointToScreen(), but have still not been able to translate correctly between different containers - groupBox1 and groupBox2 in this case.
I've tried some variations of the code below (attempting repositioning the buttons whenever the form is resized, for instance), but I'm still confused about exactly how this works.
Specifically, it seems unclear to me which control I should call PointToScreen() on, and which parameters I should pass to that method in order to achieve what I've described above.
private void Form1_Paint(object sender, PaintEventArgs e)
{
var btn2Pos = button2.PointToScreen(button2.Location);
button1.Location = button1.PointToClient(btn2Pos);
}
What would be the simplest way to solve this?
Sidenote, reason for doing this:
I want to be able to disable groupBox2, and all the controls in it, but still keep certain
controls like button 1 enabled, even though they are positioned
relative to button 2.
I don't think you need to use PointToClient and PointToScreen here:
//This will place button1 over button2
button1.Left = groupBox2.Left + button2.Left;
button1.Top = groupBox2.Top + button2.Top;
If you want to use PointToClient and PointToScreen, you can do something like this:
//The code should be placed in Form load, if placing in form Constructor, the result may be not expected.
private void Form1_Load(object sender, EventArgs e){
//This will place button1 over button2
button1.Location = groupBox1.PointToClient(groupBox2.PointToScreen(button2.Location));
}
You can rely just on .Left/.Top properties by accounting for the different relative positions. Example:
button1.Location = positining(button1);
by calling function on these lines:
private Point positining(Button curButton)
{
Point outPoint = new Point();
if(curButton == button1)
{
outPoint.X = groupBox2.Left + button2.Right + 20;
outPoint.Y = groupBox2.Top + button2.Bottom - 20;
}
else if (curButton == button2)
{
outPoint.X = groupBox2.Left + button1.Left - 20 - button2.Width;
outPoint.Y = groupBox2.Top + button1.Top + 20 + button2.Height;
}
return outPoint;
}
I appreciate the answers here, but ended up using the following solution at the end, as proposed by a collegue:
I created another, third container, but this one without a border. That container overlaps groupBox2 exactly, and button1 is positioned within that container, and therefor relative to it.
Since the point (0 , 0) of the new container overlaps with the equivalent point in groupBox2, any controls with the same coordinates in either container, will be positioned at the same place, and all I need to do is make sure to call BringToFront() (and/or SendToBack() if needed) to make sure the control is displayed correctly.
Has C# indexed control arrays or not? I would like to put a "button array" for example with 5 buttons which use just one event handler which handles the index of all this 5 controls (like VB6 does). Else I have to write for each of these 5 buttons one extra event handler. And if I have 100 buttons, I need 100 event handlers? I mean something like that:
TextBox1[i].Text="Example";
It could make coding definitely easier for me to work with control arrays. Now I have seen, that C# at least has no visible array functionality on user controls and no "index" property on the user controls. So I guess C# has no control arrays, or I must each element call by known name.
Instead of giving 100 TextBoxes in a for loop 100 incrementing values, I have to write:
TextBox1.Text = Value1;
TextBox2.Text = Value2;
...
...
TextBox100.Text = Value100;
A lot of more work + all these 100 event handlers each for one additional TextBox extra.
I know I'm a little late to this party, but this solution will work:
Make a global array:
TextBox[] myTextBox;
Then in your object's constructor, after the call to
InitializeComponent();
initialize your array:
myTextBox = new TextBox[] {TextBox1, TextBox2, ... };
Now you can iterate your array of controls:
for(int i = 0; i < myTextBox.Length; i++)
myTextBox[i].Text = "OMG IT WORKS!!!";
I hope this helps!
Pete
As I mentioned in comment to a solution by HatSoft, C# Winforms does not allow you to create control arrays like old VB6 allowed us. The nearest I think we can get to is what HatSoft and Bert Evans in their posts have shown.
One thing that I hope would satisfy your requirement is the event handler, you get a common event handler and in the event handler when you typecast the "sender" you get the control directly just like you would in VB6
C#
TextBox textBox = sender as TextBox;
VB6
TextBox textBox = TextBox1[i];
So the only trouble you might have is wiring those 100 TextBoxes to a single event handler, if you are not creating the controls dynamically through code rather creating it manually at design time then all one can suggest is group them in a container like say Panel. Then on Form Load wire them all up to a single event handler like this:
foreach (Control control in myTextBoxPanel.Controls)
{
if(control is TextBox)
control.TextChanged += new EventHandler(control_TextChanged);
}
Just create one handler and point all the buttons to it.
var ButtonHandler = (sender, args) => {
var clicked = (Button)sender;
if (clicked.Text == "whatever")
//do stuff
else
//do other stuff
};
button1.Click += ButtonHandler;
button2.Click += ButtonHandler;
Alternatively, if you are creating controls in code, you could use one of the techniques specified in this answer.
Instead of giving 100 TextBoxes in a for loop 100 incrementing values, I have to write:
for(int i = 0; i <100; i++)
{
TextBox t = new TextBox(){ Id = "txt_" + i, Value = "txt_" + i};
t.TextChanged += new System.EventHandler(this.textBox_Textchanged);
Page.Controls.Add(t);
}
//and for event on TextChanged
private void textBox_Textchanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
////
}
}
Another thing to note: if you really need to edit 100 strings on one form, you should probably think about whether 100 text boxes is really the best way to do it. Perhaps a ListView, DataGridView, or PropertyGrid would be better suited.
This applies almost any time you think you need a huge array of controls.
If you are working with Web Forms and not MVC, you can acces a collection of controls on the page as shown in Using the Controls Collection in an ASP.NET Web Page. Essentially the controls collection is a tree with the page hosting the first level of child controls and some items having children of their own. See How to: Locate the Web Forms Controls on a Page by Walking the Controls Collection for an example of how to follow the tree.
Also, see How to: Add Controls to an ASP.NET Web Page Programmatically.
You can use the same event handler for multiple items as long as the signature required is the same.
For Windows Forms this is nearly identical since they're based on similar architectural models, but you'll want Control.Controls Property and How to: Add Controls to Windows Forms.
Keeping it simple:
TextBox[] keybox = new TextBox[16]; //create an array
for (int i=0; i<16; i++)
{
keybox[i] = new TextBox(); //initialize (create storage for elements)
keybox[i].Tag = i; //Tag prop = index (not available at design time)
keybox[i].KeyDown += keybox_down; //define event handler for array
}
private void keybox_down(object sender, KeyEventArgs e)
{
int index = (int)((TextBox)sender).Tag //get index of element that fired event
...
}