I am trying to add a row in middle of table layout panel. However I am struggling to find a way to do this. I tried following from one of the article on internet but this doesn't seems to be working. Can you help.
i have 3 columns and 5 rows in table layout panel. each row containing lable, textbox and blank lable.
i am trying to add a row after 2nd row in already created table layout panel.
var AddOnControl = ConfirmationTable.Controls.Find("Discount", true).First();
int childIndex = 1+ ConfirmationTable.Controls.GetChildIndex(AddOnControl);
Label lbl = new Label();
lbl.Name = key;
lbl.Text = key;
lbl.Font = new Font("Calibri", 10F, System.Drawing.FontStyle.Regular);
lbl.Size = new Size(lbl.Size.Width + 70, lbl.Size.Height);
ConfirmationTable.Controls.Add(lbl);
TextBox txt = new TextBox();
txt.Multiline = true;
txt.TextChanged += txt_TextChanged;
txt.Name = "txtPrice" + key;
txt.Text = value;
txt.BorderStyle = BorderStyle.None;
ConfirmationTable.Controls.Add(txt);
lbl = new Label();
lbl.Name = "blank";
ConfirmationTable.Controls.Add(lbl);
ConfirmationTable.Controls.SetChildIndex(lbl, childIndex);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 1);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 2);
but above code always adds a row at the bottom of the table. Any suggestion?
There is an overload of the Controls.Add method that accepts two integers as indexes for column/row position.
Like this:
tableLayoutPanel1.Controls.Add(lb3, 0, 1);
I believe this is enough to solve your problem in a simple way.
Related
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 am trying to make a text editor and I have 2 rich text boxes. I am uses the 1st rich text box as a number like and setting its Enabled property to false. Then the second text box is going next to it
I have currently set the dock of the first text box to left and the second one to fill. But the 2nd one keeps taking up the whole tabpage? And going slightly more left towards the number line and its hidden under there a little bit. Here is my create new document void I have... T2 is the number line and T is the default text box they will type into.
TabPage t = new TabPage("new " + getNumber());
tabControl1.TabPages.Add(t);
tabControl1.SelectedTab = t;
RichTextBox T2 = new RichTextBox();
t.Controls.Add(T2);
T2.Dock = DockStyle.Left;
T2.Enabled = false;
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
Random R = new Random();
int RandomNumberHere = R.Next(1000, 100000);
T.Text = "Welcome, type your text...";
T.Select();
The problem is that you define your fill-docked text box at the end of the control list... winforms likes the fill-docked elements first in the list.
Easily solved:
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
// Add this line
T.BringToFront();
Or you could also do T2.SendToBack(); after T is added to the controls collection.
Or you could simply create (and add to t.Controls), the fill-docked textbox first, and the left-docked textbox second.
Either way works
By the way, try to name your variables correctly. t, T, T2 are just not good names
Here you go:
t.Controls.Add(T2);
t.Controls.Add(T);
t.Controls.SetChildIndex(T, 1);
t.Controls.SetChildIndex(T2, 0);
t.PerformLayout(); // needed after SetChildIndex!
T2.Dock = DockStyle.Left;
T.Dock = DockStyle.Left;
If you want your boxes to grow with the TabPage here is one way to do it:
private void t_Resize(object sender, EventArgs e)
{
// assuming you want the two RTBs to fill the TabPage
// if you want something else, best add an anchored container panel
// and use its resize event instead
T2.Width = t.Width / 4;
T.Width = t.Width / 4;
}
And yes, t, T and T2 are really bad names!
There's a few things going on:
Fill does just that; it Fills the entirety of the PARENT control. Whether anything else is there or not.
Your other text box will be hidden until it is set to Enabled.
What you Might? be looking for is a way to have the text box size itself based on the size of the tab page being created:
{
TabPage t = new TabPage("new " + 1);
tabControl1.TabPages.Add(t);
tabControl1.SelectedTab = t;
RichTextBox T2 = new RichTextBox();
t.Controls.Add(T2);
T2.Dock = DockStyle.Left;
T2.Enabled = true ;
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T2.Dock = DockStyle.Right;
var AdjustedSize = T2.Size;
AdjustedSize.Width = t.Size.Width * 2 / 3;
T2.Size = AdjustedSize;
T.Font = new Font("Microsoft San Serif", 11);
Random R = new Random();
int RandomNumberHere = R.Next(1000, 100000);
T.Text = "Welcome, type your text...";
T.Select();
}
I have an application where i will need to add dynamic controls to a panel
based on the value of a number entered in a textbox.
E.g 5 means i generate 5 rows of the controls on the button click event.
The issue is that when a large number (for example 50) is entered,although
50 rows of dynamic controls are added,i am unable to scroll down to each
of the 50 rows.
private void button1_Click(object sender, EventArgs e)
{
int inputNumber = Int32.Parse(textBox1.Text);
int wid=0;
int hgt = 0;
for (int i = 1; i <= inputNumber; i++)
{
//Create a new label and text box
Label labelInput = new Label();
TextBox textBoxNewInput = new TextBox();
ComboBox cb = new ComboBox();
//Initialize label's property
labelInput.Text = "Input " + i;
labelInput.Location = new Point(30, textBox1.Bottom + (i * 30));
labelInput.AutoSize = true;
//Initialize textBoxes Property
textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);
cb.Location = new Point(textBoxNewInput.Width + labelInput.Width + 10, textBoxNewInput.Top);
hgt += textBoxNewInput.Top;
//Add the labels and text box to the form
panel1.Controls.Add(labelInput);
panel1.Controls.Add(textBoxNewInput);
panel1.Controls.Add(cb);
}
ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (mender, f) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);
Controls.Add(panel1);
}
How can i be able to scroll from row 1 to row 100 or row 500 as the case may be?
Thanks
Not sure why you're not using a grid? Manipulate the grid instead.
I did further research and i got the answer:
I removed these lines of code
ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (mender, f) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);
And i just set the autoscroll property
panel1.AutoScroll=true;
Works fine
I'm trying to print a WPF FlowDocument. The layout needs to be in the form of 4 documents per page, laid out as follows:
Doc1 | Doc2
-------------
Doc3 | Doc4
(Sorry, I couldn't come up with a better way of illustrating the layout).
The page needs to fill, so if Doc1 & 2 are blank or just one or two characters, it still needs to print them the same size as Doc3 & 4.
The code I'm using is as follows (sorry it's long, I've tried to abridge where feasible):
PrintDialog printDialog = new PrintDialog();
if ((bool)printDialog.ShowDialog().GetValueOrDefault())
{
FlowDocument flowDocument = new FlowDocument();
flowDocument.PageHeight = printDialog.PrintableAreaHeight;
flowDocument.PageWidth = printDialog.PrintableAreaWidth;
flowDocument.PagePadding = new Thickness(25);
flowDocument.ColumnGap = 0;
flowDocument.ColumnWidth = (flowDocument.PageWidth -
flowDocument.ColumnGap -
flowDocument.PagePadding.Left -
flowDocument.PagePadding.Right);
Table myTable = new Table();
myTable.BorderThickness = new Thickness(3);
AddCols(myTable); // Add 2 cols
TableRowGroup rg = new TableRowGroup();
TableRow row = new TableRow();
AddRows(myTable); // Adds 2 rows
TableCell cell = new TableCell(new Paragraph(new Run("Doc1")));
cell.BorderThickness = new Thickness(1);
cell.BorderBrush = Brushes.Black;
// Repeat 4 times
row.Cells.Add(cell);
myTable.RowGroups.Add(rg);
doc.Blocks.Add(myTable);
....
The problem that I have is that although this does print, it doesn't try to fit it to the page as described above. Is what I am attempting possible and if so, how?
EDIT:
From looking here I believe what I actually need is a way to calculate the height of the paragraph, so that I can set the Padding property. Unfortunately the solution proposed in this link doesn't work!
Try placing the entire block in a grid so as to give it the uniform layout and then place the grid in the block and block inside your single table cell. See if this works for you -
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions.Add(new RowDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
Label text1 = new Label();
text1.Content = "Doc1";
grid.Children.Add(text1);
Grid.SetColumn(text1, 0);
Grid.SetRow(text1, 0);
Label text2 = new Label();
text1.Content = "Doc2";
grid.Children.Add(text2);
Grid.SetColumn(text2, 1);
Grid.SetRow(text2, 0);
Label text3 = new Label();
text1.Content = "Doc3";
grid.Children.Add(text3);
Grid.SetColumn(text3, 0);
Grid.SetRow(text3, 1);
Label text4 = new Label();
text1.Content = "Doc4";
grid.Children.Add(text4);
Grid.SetColumn(text4, 1);
Grid.SetRow(text4, 1);
BlockUIContainer block = new BlockUIContainer(grid);
Table table = new Table();
TableRowGroup rg = new TableRowGroup();
TableCell cell = new TableCell();
cell.Blocks.Add(block);
TableRow row = new TableRow();
row.Cells.Add(cell);
rg.Rows.Add(row);
table.RowGroups.Add(rg);
doc.Blocks.Add(table);
You could create your own custom DocumentPaginator. See here:
http://www.codeproject.com/Articles/164033/WPF-Visual-Print-Component
http://www.codeproject.com/Articles/31834/FlowDocument-pagination-with-repeating-page-header
http://www.switchonthecode.com/tutorials/wpf-printing-part-2-pagination
http://www.codeproject.com/Articles/138233/Custom-Data-Grid-Document-Paginator
Is this what you're looking for?
Convert xaml flow document to xps
question, you added the cell to the row.cell and the rowgroup to the table, but did you add the row to the rowgroup?
I wish to add a button for every line in a file to a panel.
My code so far is:
StreamReader menu = new StreamReader("menu.prefs");
int repetition = 0;
while(!menu.EndOfStream)
{
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(menuItem_Click);
dynamicbutton.Text = menu.ReadLine();
dynamicbutton.Visible = true;
dynamicbutton.Location = new Point(4+repetition*307, 4);
dynamicbutton.Height = 44;
dynamicbutton.Width = 203;
dynamicbutton.BackColor = Color.FromArgb(40,40,40);
dynamicbutton.ForeColor = Color.White;
dynamicbutton.Font = new Font("Lucida Console", 16);
dynamicbutton.Show();
menuPanel.Controls.Add(dynamicbutton);
repetition++;
MessageBox.Show(dynamicbutton.Location.ToString());
}
menu.Close();
The problem is that only the first control gets created.
The code looks fine but there could be a following situations.
1.You might have only one entry in the file, so you are experiencing only One Button added to the panel.
2.Your panel width is smaller than the sum of all the dynamic buttons width.
I suspect no 2 is the main reason that is causing problem.
So, I recommend that you use FlowLayoutPanel. To add a dynamic content as it automatically layout all the child controls.
Each time it is generating the same name for dynamic controls. That's the reason why it is showing only the last one. It simply overwrites the previous control each time.
int x = 4;
int y = 4;
foreach(PhysicianData pd in listPhysicians)
{
x = 4;
y = panPhysicians.Controls.Count * 30;
RadioButton rb = new RadioButton();
rb.CheckedChanged += new System.EventHandler(rbPhysician_CheckedChanged);
rb.Text = pd.name;
rb.Visible = true;
rb.Location = new Point(x, y);
rb.Height = 40;
rb.Width = 200;
rb.BackColor = SystemColors.Control;
rb.ForeColor = Color.Black;
rb.Font = new Font("Microsoft Sans Serif", 10);
rb.Show();
rb.Name = "rb" + panPhysicians.Controls.Count;
panPhysicians.Controls.Add(rb);
}
Try this code
StreamReader menu = new StreamReader("menu.prefs");
var str = menu.ReadToEnd();
var items = str.Split(new string[] {"\r\n" } , StringSplitOptions.RemoveEmptyEntries);
foreach (var item in items)
{
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(menuItem_Click);
dynamicbutton.Text = item;
dynamicbutton.Visible = true;
dynamicbutton.Location = new Point(4+repetition*307, 4);
dynamicbutton.Height = 44;
dynamicbutton.Width = 203;
dynamicbutton.BackColor = Color.FromArgb(40,40,40);
dynamicbutton.ForeColor = Color.White;
dynamicbutton.Font = new Font("Lucida Console", 16);
dynamicbutton.Show();
menuPanel.Controls.Add(dynamicbutton);
repetition++;
}
The problem with Panel and similar controls other than the FlowLayoutPanel is when you create a control and a second one, the second is created at the same position if you are not changing it's location dynamically or setting it according to the other already added controls. Your control is there, it's in the back of the first control.
A flowLayoutPanel is better as it will add the controls next to each other as you add them while compromising more finer control at their positioning.
I also have similar problems with panels. For what you are doing it could be useful to just add strings to a listbox rather than using labels and a panel. That should be simpler.