I have the problem that the text inside my panel gets cut of strangely. The panel is located inside a textbox. But even if I replace the textbox by a flowlayoutpanel, I have the same issue.
Code:
List<string> list = datenbank.FerienAuswahl(monat, jahr);
int i = 0;
//Create Panel
try
{
//Fill Panel
do
{
Label panel = new Label();
panel.Name = "panel" + i;
panel.Height = 30;
panel.Width = 400;
panel.AutoSize = false;
panel.TextAlign = ContentAlignment.MiddleCenter;
panel.ForeColor = Color.Black;
panel.Text = list[i];
Label ferien = new Label();
panel.Controls.Add(ferien);
tbFerien.Controls.Add(panel);
i++;
} while (i < list.Count);
}
catch { }
Result:
I have already tried to change the width of the panel. But as result I only get a messed up alignment of the text.
The only settings of the textbox I have changed are these:
Multiline: True
TextAlign: Center
Size: 359; 125
Does Someone know what else I could try ?
These lines worry me:
Label panel = new Label();
Label ferien = new Label();
panel.Controls.Add(ferien);
tbFerien.Controls.Add(panel);
It seems to me you are adding one label to another. That's not good. Use a Panel or TableLayoutPanel instead of the actual panel and make sure you have your positioning good.
Related
I use this code to add some labels to a windows form in c#:
Label[] lbl = new Label[temp+1];
for (int i = 0; i <= temp; i++) {
lbl[i] = new Label();
lbl[i].Text = "" + i;
lbl[i].Location = new Point(30 + (i * unit), 380);
lbl[i].Visible = true;
this.Controls.Add(lbl[i]);
}
There is not a serious problem but my code works for temps less than 5 and for temps greater than 5 it shows only the first one.
What you think? where is the problem?
Make the labels automatically adjust their size to their content by setting their AutoSize property to true:
lbl[i] = new Label();
lbl[i].Text = "" + i;
lbl[i].Location = new Point(30 + (i * unit), 380);
lbl[i].Visible = true;
lbl[i].AutoSize = true;
this.Controls.Add(lbl[i]);
Without this, the labels have a fixed size. When this fixed size is greater than unit, the labels overlap and hide each other's text. With more labels to add, unit becomes smaller and then smaller than the default width of the labels when temp is ≥ 5.
Alternatively, you could set the labels' width to unit to make sure that they do not overlap.
I am writing my Windows Forms app. And I have some problem with TableLayoutContainer element. Or rows are superpose (without needed scroll bar) or there is a big interval between first and second row.
I need a container with dynamic changing sizes according to Form size, with auto vertical scroll (if the container's size to big). Please help me to correct my code or container's properties.
Label LabelG = new Label[len];
NumericUpDown NumberControlBars = new NumericUpDown[len];
for (int i = 0; i < len; i++)
{
TablePanelContainer.RowCount++;
TablePanelContainer.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));
LabelG[i] = new System.Windows.Forms.Label();
LabelG[i].Name = "Label" + i.ToString();
LabelG[i].Size = new System.Drawing.Size(40, 23);
LabelG[i].Text = Groups[i].ToString();
LabelG[i].Dock = DockStyle.Right;
LabelG[i].Anchor = (AnchorStyles.Right | AnchorStyles.Top);
LabelG[i].TextAlign = ContentAlignment.MiddleRight;
TablePanelContainer.Controls.Add(LabelG[i], 0, i);
NumberControlBars[i] = new System.Windows.Forms.NumericUpDown();
NumberControlBars[i].Name = "Label" + i.ToString();
NumberControlBars[i].MaximumSize = new System.Drawing.Size(40,23);
NumberControlBars[i].Text = "0";
NumberControlBars[i].Dock = DockStyle.Left;
NumberControlBars[i].Anchor = (AnchorStyles.Left | AnchorStyles.Top);
TablePanelContainer.Controls.Add(NumberControlBars[i], 1, i);
}
Properties
Bug
I have recreated your functionality in a small app. These are the settings for your TablePanelLayout control
Anchor: Top, Bottom, Left, Right
AutoScroll: True
AutoSize: False
Specially the AutoSize setting to false is important. If you don't do that the control will resize itself to the height needed to accommodate all rows. Because the container has enough space in that case to show everything it will not show the scrollbars. It doesn't care that its size doesn't fit on the form.
This is what the designer should look like:
To overcome the quirks with the first row I adapted the Style of that first row. It looks like the designer plays some tricks here. Your code will look like this.
tableLayoutPanel1.SuspendLayout();
// adapt styling of first row
if (tableLayoutPanel1.RowStyles.Count > 0)
{
tableLayoutPanel1.RowStyles[0].SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles[0].Height = 25F;
}
for(int i=0; i<100; i++)
{
var lbl = new Label();
lbl.Text = i.ToString();
tableLayoutPanel1.Controls.Add(lbl, 0, i);
var num = new NumericUpDown();
tableLayoutPanel1.Controls.Add(num,1 ,i);
tableLayoutPanel1.RowCount++;
}
tableLayoutPanel1.ResumeLayout();
When run this is the result:
I have lots of buttons on flowlayoutpanel, and then there's text labels to break the flow. Last button before label and label itself has SetFlowBreak. All works kind of fine, but what I don't understand, is why there is so much space under the text label? If form is resized so narrow that there's only one column of buttons, then the unwanted space disappears. Can someone explain how that space can be removed?
Code:
public Form1()
{
InitializeComponent();
for (int i = 1; i <= 100; i++)
{
Button button = new Button();
button.Text = i.ToString();
button.Width = 150;
button.Height = 50;
button.Margin = new Padding(5);
flowLayoutPanel1.Controls.Add(button);
if (i % 10 == 0)
{
flowLayoutPanel1.SetFlowBreak(button, true);
Label label = new Label();
label.Text = "Some random text";
label.AutoSize = true;
label.Margin = new Padding(5, 5, 0, 0);
label.BackColor = ColorTranslator.FromHtml("#ccc");
flowLayoutPanel1.Controls.Add(label);
flowLayoutPanel1.SetFlowBreak(label, true);
}
}
}
And couple of images to show what I mean:
Image1: Strange space under the Label
Image2: No space under the Label when the form is resized (this is how I'd like this to work)
Thank you Hans! I thinks this is a real answer, as it solved my problem: (quote from comments)
It is a bug, same one as this one. The extra space is the height of the next label. The workaround is exactly the same, just add a dummy control with a Width of 0 after the label. – Hans Passant
So first I removed flowbreak after the real label:
flowLayoutPanel1.SetFlowBreak(label, true);
And then replaced it with the following code, and the mysterious space disappeared!
Label dummyLabel = new Label();
dummyLabel.Width = 0;
dummyLabel.Height = 0;
dummyLabel.Margin = new Padding(0, 0, 0, 0);
flowLayoutPanel1.Controls.Add(dummyLabel);
flowLayoutPanel1.SetFlowBreak(dummyLabel, true);
i dynamic populate a dock panel with answers from database and another dock panel with questions from the database as well. the answers will be populated as Labels and i trying to do a drag and drop with labels to textblock . Yes i can drag and drop , but the thing is i want to drag the label too . For example if the Label content is Hello , i want the hello to be dragged over with the word " hello " as well , for now , when i drag it , it doesn't drag the word as well , but when i drop it into a textbox , the word " hello " is dropped . I want to drag the animation or word as well together with the cursor .
this is my code :
private void PopulateQuestion(int activityID, int taskID)
{
IList<Model.question> lstQuestion = qn.GetRecords(taskID, activityID);
StackPanel sp = new StackPanel();
StackPanel stp = new StackPanel();
foreach (Model.question qhm in lstQuestion)
{
StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal }; // Question
TextBlock tb = new TextBlock();
tb.Text = qhm.QuestionContent;
tb.FontWeight = FontWeights.Bold;
tb.FontSize = 24;
sp1.Children.Add(tb);
StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal }; // Answer
Label tb1 = new Label();
tb1.Content = qhm.Answer;
tb1.FontWeight = FontWeights.Bold;
tb1.FontSize = 24;
tb1.MouseLeftButtonDown += tb1_Click;
sp2.Children.Add(tb1);
TextBox tbox = new TextBox();
tbox.Width = 100;
tbox.FontSize = 24;
tbox.AllowDrop = true;
tbox.FontWeight = FontWeights.Bold;
if (qhm.Answer.Trim().Length > 0 )
{
sp1.Children.Add(tbox);
}
sp.Children.Add(sp1);
stp.Children.Add(sp2);
}
dockQuestion.Children.Add(sp);
dockAnswer.Children.Add(stp);
}
private void tb1_Click(object sender, RoutedEventArgs e)
{
Label lbl = (Label)sender;
DataObject dataObj = new DataObject(lbl.Content);
DragDrop.DoDragDrop(lbl, dataObj, DragDropEffects.All);
lbl.IsEnabled = false;
lbl.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFFB3B46"); // Red
}
You can follow the strategy outlined in the link below, which essentially creates a new window and causes the window position to be updated with the mouse cursor.
http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx
So the main points from the page are that you decorate the cursor using the Adorner.
You can use the this.DragSource.GiveFeedback and other events on the DragSource event handlers to set up the Adorner.
Once you have the event handler, that gives you the opportunity to do something.
//Here we create our adorner..
_adorner = new DragAdorner(DragScope, (UIElement)this.dragElement, true, 0.5);
_layer = AdornerLayer.GetAdornerLayer(DragScope as Visual);
_layer.Add(_adorner);
So you can create your own Adorner by subclassing it. You can find more info on creating a custom adorner here:
http://msdn.microsoft.com/en-us/library/ms743737.aspx
take a look at this
http://blogs.msdn.com/b/adamroot/archive/2008/02/19/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx
the default wpf drag & drop's animation is ugly, if you want be show some text or image while dragging,you need
do something more.
When I set a label's ContentAlignment property to MiddleRight, I expect it to align to the right within the panel on which I place it. Instead, it stubbornly stays put aligned left. Why is this, and how can I fix it? Here's my code:
private void CreateNewLabel(int YPos, string DisplayStr, ContentAlignment contentAlign,
int FontSize)
{
Label lbl = new Label();
//lbl.Left = DEFAULT_XPOS;
lbl.Font = new Font(lbl.Font.Name, FontSize, lbl.Font.Style);
lbl.Top = YPos;
lbl.Text = DisplayStr;
lbl.TextAlign = contentAlign;
lbl.AutoSize = true;
panelFauxLabel.Controls.Add(lbl);
}
Everything is working EXCEPT horizontal placement. I don't want to set the Left property, because I want certain alignments to take up all the "right side" space they can; calculating the XPos is possible, I'm sure, but also quite complicated, I'm even more sure.
UPDATE
Olivier's answer worked just fine. The code is now:
private void CreateNewLabel(int YPos, string DisplayStr, ContentAlignment contentAlign, int FontSize)
{
Label lbl = new Label();
lbl.Left = DEFAULT_XPOS;
lbl.Font = new Font(lbl.Font.Name, FontSize, lbl.Font.Style);
lbl.Top = YPos;
lbl.Text = DisplayStr;
lbl.TextAlign = contentAlign;
if (contentAlign.Equals(ContentAlignment.MiddleRight))
{
lbl.Anchor = AnchorStyles.Right;
}
else // there is no AnchorStyles.Center or AnchorStyles.Middle
{
lbl.Anchor = AnchorStyles.Left;
}
lbl.AutoSize = false;
lbl.Width = panelFauxLabel.Width;
panelFauxLabel.Controls.Add(lbl);
}
UPDATE 2
I had to add a tweak to the height of the label to prevent large font sizes from being chopped off at the knees, so to speak:
// This factor was just a guess, but it seems to work pretty well
double down = Math.Round(FontSize*1.5);
lbl.Height = Convert.ToInt32(down);
TextAlign aligns the text within the label, not the label within the panel. Consider using the Anchor property in order to align the label to the right edge of the panel.
UPDATE
Here's how this can be done: Make the label the same width than the panel, anchor the label to the top, left and right
lbl.TextAlign = contentAlign;
lbl.AutoSize = false;
lbl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
lbl.Top = YPos;
lbl.Left = 0;
lbl.Width = panelFauxLabel.ClientSize.Width; // ClientSize takes account of scroll
// bars, borders and padding.
panelFauxLabel.Controls.Add(lbl);
Now the label resizes together with the panel and the text aligns correctly within the label. Set lbl.AutoSize to false in order to make the label size independent of the text length.