Dynamic textbox alignment - c#

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;

Related

C# context menu for flowlayout panel

I use flowlayout panel for dynamically create labels. So I want to use righ click menu for this dynamic created labels but contextmenustrip just recognize flowlayoutpanel. For example I right click and get label.text but couldnt. Is there any way for right click menu for dynamic objects?
Label addlabel(int i)
{
Label L = new Label();
L.Name = "LBL" + i.ToString();
L.Text = "LBL" + i.ToString();
L.ForeColor = Color.Black;
L.BackColor = Color.Gray;
L.Width = 94;
L.Height = 21;
L.TextAlign = ContentAlignment.MiddleCenter;
L.Margin = new Padding(5);
return L;
}
for (int i = 0; i < 10; i++)
{
Label L = addlabel(i);
flowLayoutPanel1.Controls.Add(L);
}
Sure...just set the ContextMenuStrip property of the Labels as you create them:
L.ContextMenuStrip = contextMenuStrip1;
If you want to get the Label that was the "source" of the menu, then use code like this:
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
if (contextMenuStrip1.SourceControl is Label)
{
Label lbl = (Label)contextMenuStrip1.SourceControl;
MessageBox.Show(lbl.Text);
}
}

How to get values of dynamically generated controls and call their events in asp.net c#

This is my code in which i created dynamic controls in 2 panels.
Now I want to get values from these dynamic controls. I also generated dynamic button and its event but when i click in button it will not hit break point and page is post back.I read on internet that i have to generate control or store their values into PreInit of Asp.net page life cycle. But i can't understand it. If anyone know about it Reply me.
Thanks.
public void showControls(string p)
{
Button btn = new Button();
List<string> lblUD = getUserDetails(p);
List<string> lblli = getWebPagesName();
for (int i = 0; i < o; i++)
{
HtmlAnchor ha = new HtmlAnchor();
CheckBox cb = new CheckBox();
Label lbl = new Label();
cb.Checked = false;
cb.ID = "cb" + i.ToString();
cb.Text = "Allow";
lbl.Text = lblli[i].ToString();
lbl.ID = "lbl" + i.ToString();
ha.Controls.Add(lbl);
ha.Controls.Add(new LiteralControl("<br/>"));
lbl.Style.Add("line-height", "28px");
//lbl.Style.Add("border", "1px solid");
//cb.Style.Add("border", "1px solid");
lblPanel.Controls.Add(ha);
//lblPanel.Controls.Add( new LiteralControl("<br/>"));
cb.Style.Add("line-height", "24px");
cbPanel.Controls.Add(cb);
cbPanel.Controls.Add(new LiteralControl("<br/>"));
Label2.Enabled = true;
Label2.Text = "<h2>Assign Roles</h2>";
}
for (int s = 0; s < lblUD.Count; s++)
{
Label lblud = new Label();
lblud.ID = "lblud" + s.ToString();
lblud.Text = lblUD[s].ToString();
lblud.Style.Add("line-height", "28px");
userdetailPanel.Controls.Add(lblud);
userdetailPanel.Controls.Add(new LiteralControl("<br/>"));
}
btn.ID = "btn_Submit";
btn.Text = "Save";
btn.Click += new System.EventHandler(this.btn_Submit_click);
cbPanel.Controls.Add(btn);
}

Clear Labels or Textbox on Panel or Winform

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.

How to set different events for dynamically created element

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!

display two textbox in customMessageBox?

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,

Categories