I created some labels in a for loop on the behind code.
At the beginning it looks like that:
private void SlotLabelCreation(string name)
{
Label label = new Label();
label.Name = name;
label.HorizontalAlignment = HorizontalAlignment.Left;
label.VerticalAlignment = VerticalAlignment.Top;
label.Content = "[Free Slot]";
label.Foreground = Brushes.Gray;
label.BorderBrush = Brushes.Black;
label.BorderThickness = new Thickness(1);
label.Visibility = Visibility.Hidden;
MainGrid.Children.Add(label);
//the margin has been inserted later in other code.
}
everything was fine but when I inserted to the label other content which not contains the same amount of letters it looks like that:
sorry about the links.. it's because I can't upload images
http://s27.postimg.org/6halp6p37/pic2.png
I wanted to make all slots the same size so I added a MinWidth property to the labels.
The new result was:
http://s27.postimg.org/6z5r51eo3/pic3.png
now it looks better but I am wondering how could I center the content which inside the label.
Unfortunately I didn't find any solution to solve this problem.
p.s, HorizontalAlignment and VerticalAlignment don't solve the problem.
Thanks a lot, Alon P.
You should use HorizontalContentAlignment and VerticalContentAlignment properties
Related
is it possible to add elements like panel to a listbox, listview or other list with .items?
I want to create panels with other elements like labels, checkbox, buttons,.. on a panel.
The panel should then be in a list so that I can check it for example: checkbox (which is on the panel) and is active in the list is showing, the other elements should be hidden in the list.
If a panel is between 2 panels, it should move upwards so that there is no space in between. If I then only display the panels of the non-activated elements, the hidden ones should be displayed again and the displayed ones should be hidden.
I would like to control this display using button event,
show everything,
only activated and
only deactivated.
With another button I would like to be able to bring a panel to the top position if it was declared as a favorite. if the favorite is removed again it should go back to where it was before.
In addition, I would then like to create a search mask that only displays the elements that match the search string when entered.
The only way I found is with listbox1.Controls.Add(panel1); for it to appear.
Unfortunately it doesn't work with listbox1.Items. :(
So I don't have a selectedItem either....
Here is my code that I have so far:
private void Reload_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
panel1.Size = new Size (250, 35);
panel1.BackColor = Color.Red;
panel1.ForeColor = Color.Green;
panelxy.Dock = DockStyle.Top;
Panel panel2 = new Panel();
panel2.Size = new Size(250, 35);
panel2.BackColor = Color.Blue;
panel2.ForeColor = Color.Green;
panel2.Dock = DockStyle.Top;
listBox1.Controls.Add(panel1);
listBox1.Controls.Add(panel2);
Button btn_1 = new Button();
btn_1 .Size = new Size(200, 30);
btn_1 .Location = new Point(5, 2);
btn_1 .ForeColor = Color.Blue;
btn_1 .BackColor = Color.Yellow;
btn_1 .Font = new Font("Sitka Text", 15F, (FontStyle)(FontStyle.Bold | FontStyle.Italic), GraphicsUnit.Point, (byte) 0);
btn_1 .Text = "testbutton";
panel1.Controls.Add(btn_1 );
}
And here a Picture to show that:
listbox_elements
I hope someone can help me here. :)
thanks and BR
Cusy
Super thank you!
I just found out that it is possible to realize the required with a TableLayoutPanel. :)
Since I already have about 7000 lines of code, I don't want to convert it to WPF. ;)
i think the Answer from "Jimi" with FlowLayoutPanel are also a Solution. :)
Thanks
BR Cusy
I have the problem that the content of the expander does not start at the left, how can I solve this?
As you can see on the picture, the contents of my expander are not shown to the far left. I have tried many settings, unfortunately I do not come to the solution. The expander itself also seems to have an edge opposite the stackpanel, which I also want to avoid.
Above the expander you can see a grid, which contains a text block with content. This grid has the right mass within the higher-level stack panel.
Expander exp = new Expander
{
Header = "TestExpander",
Width = spStackPanel.Width,
ExpandDirection = ExpandDirection.Down,
IsExpanded = true,
BorderBrush = Brushes.Yellow,
BorderThickness = new Thickness(1),
HorizontalAlignment = HorizontalAlignment.Left,
HorizontalContentAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
VerticalContentAlignment = VerticalAlignment.Top,
};
TextBox tx = new TextBox
{
Text = "testText"
};
exp.Content = tx;
spStackPanel.Children.Add(exp);
I can't comment so apologies for the somewhat vague answer, but it might help to share the styling and XAML used to create your screenshot. By default that isn't how an Expander looks, so my guess is that something in your styling or one of the higher level controls (e.g. StackPanel or Grid) is affecting the layout of the Expander.
Example being possibly a keyless style for TextBox that applies padding and/or horizontal alignment, which would then affect the content you're adding by creating a new TextBox in your snippet to give it that odd alignment.
I have written a test here, directly in a .cs file WITHOUT XML:
buttonGrid = new Grid();
buttonGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
buttonGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
Expander exp = new Expander()
{
Header = "Test Expander",
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0),
IsExpanded = true
};
TextBlock label = new TextBlock()
{
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0),
Text = "Test Text"
};
exp.Content = label;
Grid.SetRow(exp, 0);
buttonGrid.Children.Add(exp);
TextBlock text = new TextBlock()
{
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0),
Text = "Test Text",
Foreground = Brushes.White
};
Grid.SetRow(text, 1);
buttonGrid.Children.Add(text);
I know that an expander doesn't usually look like this, so I ask my question ;-)
maybe someone has me a tip to look for.
Whenever I set a TextBox's border style to "None" the bottom of text in the box is invisible. That means I can't see underscores and descenders of letters like y and j.
Here is the textbox with border
Here is the textbox without border
Any suggestions as to how this is solved would be appreciated.
This seemed to work for me:
public Form1()
{
InitializeComponent();
textBox1.Multiline = true;
textBox1.MinimumSize = new Size(0, 30);
textBox1.Size = new Size(textBox1.Size.Width, 30);
textBox1.Multiline = false;
}
Setting box to multi line to adjust size of the box then when its changed back keeps the size.
I have tried to create a radiobutton dynamically and add it to groupbox/form, but the whole text associated with the radiobutton is not getting displayed. When the radiobutton is added from the designer, the whole text is getting displayed. For dynamically adding radio button am I missing anything or are there any ways to do it?
Please find the sample code below:
public partial class Form1 : Form
{
private void SelectMicrophone_Load(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton r1 = new System.Windows.Forms.RadioButton(); //created a radiobutton
r1.Name = "Microphone(RealTex";
r1.Text = "Microphone(RealTex";
r1.Location = new System.Drawing.Point(15, 15);
this.groupBox1.Controls.Add(r1);
When you set the text property in the designer, it adjust the radio button to the new size to cover the width of the text. By default I think the width is 90 and with the text above it is resized to a width of 124. So when you create the object at runtime, it is probably just keeping the width to 90. You can however just set r1.Width = 124 before adding it to your controls collection.
Keep in mind that you may not know the length each time so you could either set the width to the maximum size that you need or use the TextRender's .MeasureText method to get the size of the text and then just add 20 to that to cover the graphic of the radio button circle that also appears and set the result of the X property to your width before adding the radiobutton to the collection.
RadioButton r1 = new RadioButton();
r1.Text = "This is short text";
//Measure the Text property and get the width and add 20 to accomodate the circle
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 15);
this.Controls.Add(r1);
//Just another RB with even longer text.
r1 = new RadioButton();
r1.Text = "This is even longer text that we want to show";
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 35);
this.Controls.Add(r1);
I'm a little confused as to why this isn't working, since I had it working in a prototype and the only big difference I think is that I use a custom TabItem and UserControl instead of the default ones. I'm trying to get the usercontrol that appears to be centered in the tab window, but it seems to be aligned left.
You hand this method the usercontrol you want to use and it formats it and sticks it in the tabcontrol. In a test solution I did of this earlier, setting scroll's horizontal and vertical alignment to stretch fixed this, but it's not working in this case. Is there some other setting or something else somewhere that would override this?
public void CreateNewTab(UserControlGeneric new_user_control, string tab_header)
{
//TabItem tab = new TabItem();
TabItemIndexed tab = new TabItemIndexed();
//The scrollviewer is created/setup to make sure the usercontrol gets scroll bars if the window if ever made smaller than the usercontrol
ScrollViewer scroll = new ScrollViewer();
//How you programatically set a scrollviewer's height and width to be "Auto"
scroll.Height = Double.NaN;
scroll.Width = Double.NaN;
scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scroll.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
scroll.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
scroll.Content = new_user_control;
tab.Content = scroll;
tab.Header = tab_header;
//If there aren't any tabs, then hide the "No Workspaces Open" notice (Since we're adding a tab)
if (!tabControl_main.HasItems) label_no_workspaces_open.Visibility = System.Windows.Visibility.Hidden;
tabControl_main.Items.Add(tab);
tabControl_main.SelectedItem = tab;
}