Well, I want to display two text boxes next to next in customMessageBox. So i have coded for two Text Boxes. like below. I named soora and ayath for it. But in customMessageBox, i cannot call two text boxes in the same time. It shows error. How to display two text boxes next to next in customMessageBox. I only the error and it is form Content = soora + ayath
My C# CODE;
TextBox soora = new TextBox();
soora.Height = 72;
soora.Width = 150;
soora.MaxLength = 3;
TextBox ayath = new TextBox();
ayath.Height = 72;
ayath.Width = 150;
ayath.MaxLength = 3;
CustomMessageBox messageBox = new CustomMessageBox()
{
Title = "GO TO",
Content = soora + ayath,
RightButtonContent = "Go !",
};
use a container control to hold both textboxes
TextBox soora = new TextBox();
soora.Height = 72;
soora.Width = 150;
soora.MaxLength = 3;
TextBox ayath = new TextBox();
ayath.Height = 72;
ayath.Width = 150;
ayath.MaxLength = 3;
StackPanel container = new StackPanel{
Orientation = System.Windows.Controls.Orientation.Horizontal
};
container.Children.Add(soora);
container.Children.Add(ayath);
CustomMessageBox messageBox = new CustomMessageBox()
{
Title = "GO TO",
Content = container,
RightButtonContent = "Go !",
};
if you want to display text then
Content = soora.Text + ayath.Text,
Related
I have a button which will create additional textboxes when clicked. The problem is that i dont get them aligned with the default box of company code.
public System.Windows.Forms.TextBox Addcompcode()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = A * 123;
txt.Left = 90;
txt.Text = "Company Code";
A = A + 1;
return txt;
}
Try following vertical alignment
this.txt.VerticalAlignment = VerticalTextAlignment.Top;
I'm using WinForms in VS15 with C#.
I'm dynamically adding TextBoxs and Labels to my Form based upon a user selected value in a ComboBox (essentially this looks up a value in a data collection which tells my UI what controls it needs).
When I attempt to generate the controls, the Labels appear and layout just fine, however, the TextBoxs are remarkable in there absence.
I've tried fidelling with the MaximumSize and MinimumSize properties to see if they could be messing with something but it doesn't seem to be making any difference.
The code I use for doing this is below (I know the use of the List<Pair<Label,TextBox>> is pretty unecessary but I find it helps readability):
private void GenerateControls(string formType)
{
string[] formParameters = engine.GetFormParameters(formType);
if (formParameters == null) return;
SplitterPanel panel = splitContainer.Panel1;
panel.Controls.Clear();
List<Pair<Label, TextBox>> controlPairs = new List<Pair<Label, TextBox>>();
int tabIndex = 0;
Point labelPoint = panel.Location + new Size(20, 20);
Size initialOffset = new Size(0, 30);
Size horizontalOffset = new Size(40, 0);
Size tBoxSize = new Size(40,20);
foreach (string parameter in formParameters)
{
Label label = new Label
{
Text = parameter,
Tag = "Parameter Label",
Name = $"lbl{parameter}",
Location = (labelPoint += initialOffset)
};
TextBox textBox = new TextBox
{
AcceptsTab = true,
TabIndex = tabIndex++,
Text = "",
Tag = parameter,
Name = $"txt{parameter}",
MaximumSize = tBoxSize,
MinimumSize = tBoxSize,
Size = tBoxSize,
Location = labelPoint + horizontalOffset
};
controlPairs.Add(new Pair<Label, TextBox>(label, textBox));
}
foreach (Pair<Label, TextBox> pair in controlPairs)
{
panel.Controls.Add(pair.First);
panel.Controls.Add(pair.Second);
}
}
I don't believe that my use of Point + Size is the issue as the Point class overrides the + operator like so:
Unfortunately for me the issue appears to be simply that the dX was not a big enough value to prevent the text boxes from being hidden under the labels, I forgot that labels don't have transparent backgrounds.
While I was at it: I've removed the redundant List<Pair<<>>; added support for dynamically adjusting TextBox location based on Label size; and split it out into two separate loops, so my code now looks as below and works just fine:
private void GenerateControls(string formType)
{
string[] formParameters = engine.GetFormParameters(formType);
if (formParameters == null) return;
SplitterPanel panel = splitContainer.Panel1;
panel.Controls.Clear();
int tabIndex = 0;
Point labelPoint = panel.Location + new Size(20, 20);
Size verticalOffset = new Size(0, 30);
Size tBoxSize = new Size(200,20);
int maxLabelLength = 0;
foreach (string parameter in formParameters)
{
Label label = new Label
{
Text = parameter,
Tag = "Parameter Label",
Name = $"lbl{parameter}",
Location = (labelPoint += verticalOffset),
AutoSize = true
};
panel.Controls.Add(label);
if (label.Size.Width > maxLabelLength)
{
maxLabelLength = label.Size.Width;
}
}
Size horizontalOffset = new Size(maxLabelLength + 30, 0);
labelPoint = panel.Location + new Size(20, 20) + horizontalOffset;
foreach (string parameter in formParameters)
{
TextBox textBox = new TextBox
{
AcceptsTab = true,
TabIndex = tabIndex++,
Text = "",
Tag = parameter,
Name = $"txt{parameter}",
MaximumSize = tBoxSize,
MinimumSize = tBoxSize,
Size = tBoxSize,
Location = labelPoint += verticalOffset
};
panel.Controls.Add(textBox);
}
}
Thanks everyone who helped!
I want to add 2 label at the same group box, this is my code :
int x = 0;
foreach (var item in comboboxinterface.Items)
{
drv = item as DataRowView;
Button btn = new Button();
Label lblerrortoday = new Label();
Label lblcounterror = new Label();
btn.Text = drv.Row.ItemArray[0].ToString();
btn.Location = new System.Drawing.Point(10, 20 + (x * 30));
lblcounterror.Location = new System.Drawing.Point(100, 25 + (x * 30));
lblcounterror.Text = "No";
lblerrortoday.Location = new System.Drawing.Point(120, 25 + (x * 30));
lblerrortoday.Text = "Error Today";
grouptodayerror.Controls.Add(btn);
grouptodayerror.Controls.Add(lblcounterror);
grouptodayerror.Controls.Add(lblerrortoday);
x++;
}
But when i start the program, the lblerrortoday is not showing up but the lblcountererror is fine, when i tried to comment the the lblcounterror, the lblerrortoday is showing fine, did i miss something?
For me your approach seems to ok, only point to note that width of labels that may cause for not displaying other label.
You could use Control.AutoSize property , This may resolve your problem
lblcounterror.AutoSize = true;
lblerrortoday.AutoSize = true;
The TextBox is there, but overlapped by lblcounterror. Reduce Width of lblcounterror and you will see lblerrortoday.
I have dynamic Labels and TextBox's on one panel.
I can delete the Panel. No Problem but then I also don't know how to delete the Textboxes etc
and i hoped that i can refresh or clear the panel so that all labels and textboxes will deleted..
Label makeLabelC = new Label();
makeLabelC.Width = 100;
makeLabelC.Font = new Font(makeLabelC.Font.Name, 8, FontStyle.Bold | FontStyle.Underline);
makeLabelC.Location = new Point(400, 100);
makeLabelC.Name = e.Node.Text;
makeLabelC.Text = e.Node.Text;
this.Controls.Add(makeLabelC);
this.Controls.Add(panel1);
TextBox textboxC = new TextBox();
textboxC.Width = 100;
textboxC.Location = new Point(500, 100 );
textboxC.Name = e.Node.Text + "lbl";
textboxC.Text = "enter here";
this.Controls.Add(textboxC);
this.Controls.Add(panel1);
for (int z = 0; z < n; z++)
{
Label makeLabel = new Label();
makeLabel.Width = 100;
makeLabel.Location = new Point(400, 150 + 2 * z * makeLabel.Height);
makeLabel.Name = e.Node.Text;
makeLabel.Text = e.Node.Nodes[z].Text;
this.Controls.Add(makeLabel);
this.Controls.Add(panel1);
TextBox textbox = new TextBox();
textbox.Width = 100;
textbox.Location = new Point(500, 150 + 2 * z * textbox.Height);
textbox.Name = e.Node.Text + "lbl";
textbox.Text = "enter here";
this.Controls.Add(textbox);
this.Controls.Add(panel1);
}
}
is there a way with panel how to do this or an other solution?
I thought that the Panel can help me there...
thanks Janik
You are adding the controls to the form instead of the panel - which you also add multiple times
this.Controls.Add(panel1); // do this once
panel1.Controls.Add(textbox); // add the controls to the panel
Once you have done this, when you remove the panel, you will also remove its child controls.
Im trying to create event with different index for dynamically created GroupBox. With my actual code event for every groupbox is that same. How can i make event with different index for every groupbox? My Code:
public void LoadGry()
{
// GroupBox groupbox = new GroupBox();
Label nazwagry = new Label();
for(int i = 0; i < myCollection.Count; i++)
{
GroupBox groupbox = new GroupBox();
groupbox.Text = myCollection[i];
groupbox.Size = new Size(290, 131);
groupbox.Location = new Point(6, 150 * (myCollection.Count - i - 1));
groupbox.ForeColor = Color.White;
Label label1 = new Label();
label1.Text = groupbox.Text;
label1.AutoSize = true;
label1.Location = new Point(groupbox.Location.X + 80, groupbox.Location.Y + 20);
groupbox.Controls.Add(label1);
Gry.Controls.Add(label1);
PictureBox picturebox = new PictureBox();
picturebox.Location = new Point(groupbox.Location.X + 5, groupbox.Location.Y + 20);
picturebox.Size = new Size(75, 75);
picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
picturebox.LoadAsync(myCollection3[i]);
groupbox.Click += new EventHandler(delegate {groupboxclick(groupbox, picturebox, i);});
Label label2 = new Label();
label2.Text = "Status: " + "Aktualny";
label2.ForeColor = Color.Green;
label2.AutoSize = true;
label2.Location = new Point(label1.Location.X, label1.Location.Y + 20);
Gry.Controls.Add(label2);
Label zapiszopis = new Label();
zapiszopis.Text = myCollection4[i];
zapiszopis.Visible = false;
Gry.Controls.Add((Control)groupbox);
//MessageBox.Show("pokaz mi wysokosc");
}
}
private void groupboxclick(GroupBox groupbox, PictureBox picturebox, int itest)
{
groupbox.ForeColor = Color.Aqua;
this.pictureBox1.BackgroundImage = picturebox.BackgroundImage;
opishacka.Text = myCollection4[itest];
}
The problem is that the event setup is using the variable K value. For use the number instead you probably needs to create an expression manually to use the current value in each case.
BUT
You can easily do what you want using the following properties to attach values to controls.
1-) Tag in WinForms & WPF:
// Setup
pictureBox.Tag = i;
// Event
int i = (int) pictureBox.Tag;
2-) ViewState in WebForms
// Setup
ViewState[pictureBox.UniqueID] = i;
// Event
int i = (int) ViewState[pictureBox.UniqueID];
You can use many other techniques. I only post one for each popular framework. I guest that you are in a WinFors project.
Hope this help!