How to make a user control which extend from TextBox ,and add a Label beside the textbox , not inside the textbox , the label must be beside it or on top of it.
BTW the user control must extend from TextBox .
For long time I have been searching for this problem and I cant find any anwser.
If I'm understanding you correctly, you want to place a Label next to a TextBox. If you want to do this with code, it's as simple as:
int x, y;
x = y = 200;
TextBox tb = new TextBox();
tb.Width = 100;
tb.Left = x;
tb.Top = y;
Label lbl = new Label();
lbl.Width = 50;
//If you want it on the right of the TextBox
lbl.Left = tb.Right + 10;
//If you want it on the left of the TextBox
lbl.Right = tb.Left - 10;
You can obviously modify all of those values to your hearts desire. Also, this seems kind of moot, because if you're going to be doing this in a WinForms Application, you can just drag and drop everything you want in to place. You're obviously new to this... Check out some YouTube on how to create WinForms Applications and get on MSDN...
https://www.youtube.com/watch?v=DdXrw6HUzCA
https://msdn.microsoft.com/en-us/dn308572.aspx
Related
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.
I am creating a custom control, a custom looking TextBox designed to only allow numeric input. It consists of a plain control, with a TextBox as one of it's properties:
class NumericControl : Control
{
private TextBox text;
//rest of the code
}
This TextBox is drawn inside of the control. It all works very nicely, except that when you press enter it makes this horrible DING noise. To fix this, I thought I'd make the TextBox multiline. However, when I do this, because the font size of the TextBox is changed to resize it, the blinking cursor inside the TextBox appears to disappear, which is a problem because then you can't tell by looking at it whether or not it has focus. If I don't change the font size of the TextBox, the cursor appears and acts normally, however I need the font size to change based on the height of the control, otherwise it doesn't look any good.
The code setting the TextBox properties is as follows, and resides within the constructor of my control:
text = new TextBox();
text.AutoSize = false;
text.Left = 10;
text.Top = 2;
text.Text = "0";
text.Multiline = true;
text.BorderStyle = BorderStyle.None;
text.TextChanged += text_TextChanged;
text.LostFocus += text_LostFocus;
this.Controls.Add(text);
The font size is changed inside the OnPaint event, and looks like this:
text.BackColor = Enabled ? Background : SystemColors.Control; //Fixes an issue I had with disabled TextBox BackColor being able to be changed
text.Font = new Font(TextBox.DefaultFont.FontFamily, (float)(rc.Height * 0.7 - 2), FontStyle.Regular);
text.Width = this.Width - 21;
text.Height = this.Height - 4;
How do I both resize the textbox (and font size), and make it multiline while retaining the blinking cursor when it has focus?
I know it isn't much, but this is all I got:
TextBox textBox = new TextBox();
textBox.Text = "sample text";
textBox.Width = 200;
textBox.Height = 50;
I have been programming c# for a while now, but Winforms are new to me. Google hasn't been of any help either.
You need to add the Textbox to the control's collection of your panel instance.
panel.Controls.Add(textbox);
YourPanel.Controls.Add(controlName);
And then: YourPanel.Location = new Point(Coordinates for X, Coordinates for Y); <- so you can move your control's position :P
I have a loop that is supposed to go through DataTable and for each row create a new GroupBox, set it's text to value from one column, in that GroupBox I want to put a Label with Text similar to another column in the table.
This is just part of the code!
for (int i = 0; i < tab.Rows.Count; i++)
{
lblbox[i] = new GroupBox();
lblbox[i].Text = tab.Rows[i]["text"].ToString();
lblbox[i].Name = "box no " + i.ToString();
lblbox[i].Visible = true;
this.Controls.Add(lblbox[i]);
lblbox[i].Location = new Point(5, 55 * i);
lblbox[i].Height = 50;
lblbox[i].SendToBack();
importancelbl[i] = new Label();
importancelbl[i].Text = "Importance: " + tab.Rows[i]["importance"].ToString();
importancelbl[i].Name = "implbl" + i.ToString();
importancelbl[i].Visible = true;
lblbox[i].Controls.Add(importancelbl[i]);
importancelbl[i].BringToFront();
Point locP = new Point();
locP.X = lblbox[i].Location.X + 5;
locP.Y = lblbox[i].Location.Y + 15;
importancelbl[i].Location = locP;
}
When i run the code it creates three (I have three rows in my table) GroupBoxes correctly and creates all the labels, but only first label is visible in its Groupbox. When I add those labels to the Form and not to the GroupBox, all of them are visible, but I want them to be in boxes...
I've tried pretty much everything and I'm still very confused (espacially by the behavior of the first label). I know the mistake is probably obvious and stupid, but I just can't find it!
Control.Location is relative to its parent, so set Location for the label to (5, 15).
locP.X = 5;
locP.Y = 15;
My guess is that they are somehow overlapping and making each other disappear somehow.
Could you try posting pictures of the form when it works and when it doesn't? Also add all your code?
Try to preform adding
lblbox[i].Controls.Add(importancelbl[i]);
this.Controls.Add(lblbox[i]);
after setting all your properties
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.