How do I right align controls in a StatusStrip? - c#

I am trying to right align a control in a StatusStrip. How can I do that?
I don't see a property to set on ToolStripItem controls that specifies their physical alignment on the parent StatusStrip.
How do I get Messages drop down to be right aligned? http://i.friendfeed.com/ed90b205f64099687db30553daa79d075f280b90

Found it via MSDN forums almost immediately after posting :)
You can use a ToolStripLabel to pseudo right align controls by setting the Text property to string.Empty and setting the Spring property to true. This will cause it to fill all of the available space and push all the controls to the right of the ToolStripLabel over.

For me it took two simple steps:
Set MyRightIntendedToolStripItem.Alignment to Right
Set MyStatusStrip.LayoutStyle to HorizontalStackWithOverflow

As an added note this is due to the fact that in the Win32 API a cell is either fixed width or fills the remaining space -1
int statwidths[] = {100, -1};
SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Hi there :)");
If memory serves me correctly you can have only one fill cell (-1) per statusbar.
You could also add a third middle cell and give this the fill property to get a more concistent looking StatusBar. Consistent because Messages has an inset to its left right where you'd expect it. A bit like the mspaint shot found on the MSDN page for StatusBars
I like the creative appreach though :D

You can display the Button at the end of the StatusStrip by using the logic below.
Add a ToolstripLabel to the StatusStrip
Set text as string.Empty
Set Padding for the ToolstripLabel
For example:
this.toolStripStatusLabel1.Padding = new Padding((int)(this.Size.Width - 75), 0, 0, 0);

Keep a Toolstrip label , set Spring property as true and for label align text in BottomLeft

I found that you can set the StatusStrip Layout to HorizontalStackWithOverflow.
Then, for each control on the StatusStrip that you want on the right side, set the control Alignment to Right.
I like this better since you don't need any extra or dummy controls to align.

If you set a status strip label control’s Spring property to true, then that label takes up any space not used by other controls in the StatusStrip.

Set the RightToLeft tool strip property to True.

Related

How to control the space between two labels during run time

Im using Visual Studio 2015 Community C#.
I have two labels on a Windows form suppose Label1 and Label2.
These labels will get filled up with user input namely first name and last name.
How to put even space between them so that during runtime the first name doesn't over lap the last name.
AbrahLincoln Abraham Lincoln
(Label1^)(^Label2) (^Label1) (^Label2)
For example: how to make this ^ INTO that >>>>>>>>>>>>^^
Because if I put space in the Form Design before runtime then for other names It will come like this: John(unnecessary space)Doe
Hope you have understood my problem.
Thanks for your time. :D
Controls are located in a form based on coordinates. Luckily for you these controls have properties that tell you the coordinate for the top, left, right, bottom of a control. So you could dynamically move the right label after setting the text.
Label2.Point = new Point(Label1.Right + 5, Y-coord);
An easier way would be to play about with the labels properties in the designer.
You could also try to anchor label1 to the right and label2 to the left. That way you should have a clean middle line, and as the text grows larger it pushes outwards on does not overlap inwards over each other.
However you need an object to anchor to and luckily the SplitContainer works excellent for this.
Also consider setting the autosize property to off and maxing the widths of the labels. Large enough for the string you expect.
Have you considered making it one label?
As in
theOnlyLabel.Text = $"{dataObject.FirstName} {dataObject.LastName}";
or, if you're using textboxes, something like
theOnlyLabel.Text = $"{txtFirstName.Text} {txtLastName.Text}";
Otherwise, I'm afraid, you'd have to realign these two labels every time your first or last name changes.

Adjust label height to height of string

So I'm making a C# Windows Forms application in which I have a label. This label's size is 100x100 by default*, but I want to automatically increase the label's height so that any string fits in it regardless of its "height". How would I do this? I haven't tried anything myself yet because I don't really have an idea of what to do. I'm just a beginner, after all!
Thanks in advance.
*100x100 is just an example, the real size is different (I'm not sure yet what it'll be)
Thanks everyone, I managed to get what I want by setting the label's MaximumSize property. (Answered by #LarsTech in comments)
AutoSize defaults to true - so the default behaviour of the textbox should resize to any font size/content that is in use.
The label control in Windows Forms is a container which accepts your input strings. Therefore, if you do change its initial values it will get to its content size anyways. So, just change its Text Property.
You could try it first.
here's an example code you can use. It uses the AutoSize property.
If you label is called Label1 you can change it like this:
Label1.AutoSize = true;
Label1.Text = "The text in this label is longer than the set size.";
And it will automatically change it.

Vertically align text within a textbox when AutoSize=false

I'm trying to extend the standard TextBox control in the System.Windows.Forms namespace. One of the things I'm trying to accomplish is to modify padding on the top and bottom of the TextBox (the spacing between the text and the border).
Doing it on the left/right sides is pretty simple, but I'm having a hard time getting anything to work on the top/bottom.
Here are a couple of my requirements (if possible):
Avoid extending anything besides System.Windows.Forms.TextBox (no UserControls)
I want to keep MultiLine = false
This code allows me to resize vertically, and it adds a left padding:
public class TextBoxTest : TextBox
{
public TextBoxTest()
{
base.AutoSize = false;
NativeMethods.SendMessage(Handle, NativeMethods.EM_SETMARGINS, NativeMethods.EC_LEFTMARGIN, 20);
base.Height = 55;
base.Width = 150;
base.Text = "This is a test";
}
}
This will look like:
I also tried EM_SETRECT but it requires a MultiLine TextBox.
Is a top/bottom margin (or center vertically) possible - without using a UserControl and keeping MultiLine=false?
If so, can someone point me in the right direction?
UPDATE, to clarify, the reason I want to inherit from TextBox, and not a UserControl or Panel is so it passes the duck test (if it looks like a duck, quacks like a duck, etc). I want if (myControl is TextBox) ... to evaluate to true. There may be a way to do that, I haven't done much with Type Converters and maybe that's the path I should be taking.
I want this to be a true extension of a TextBox. In other words, it can do just about everything a textbox can do plus some, but no limitations due to the fact that it's not of type "TextBox". It seems like there's gotta be a way to adjust it somehow, it supports left and right margins but seemingly not upper/lower margins. Thanks again
You cannot change the padding property of Windows forms textbox from your code.
Instead you can place a textbox in a container, like a panel
remove the border of the textbox, make sure that the BackColor property for both is same
change the Dock property of the textbox to Fill
and then apply padding to the panel.

Align label in UITableViewCell on the right with Auto Layout

I want to use auto layout for may table view cell content. This worked so far. Now I want to align a UILabel on the right side of the table view. If I do this, it isn't aligned right. Instead it is completely on the left side. So it seems the width of the table view is not calculated when the constraint is applied. I tried two different approaches:
No. 1:
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:[iNumberLabel]-|", (NSLayoutFormatOptions)0, null, viewsDictionary));
No. 2:
this.ContentView.AddConstraint (NSLayoutConstraint.Create (iNumberLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.Right, 1, 0));
The constraints are added in UpdateConstraints in my table view cell subclass. The code is in C# but that doesn't matter. I would be glad if you could provide a solution.
This format string: "H:|-[iNumberLabel]-|"
tells the system to pin the label to both sides of the cell with the default padding, so the label will be nearly the width of the cell. You should get rid of the spacing to the left side, if you want it right aligned,
"H:[iNumberLabel]-|"
I don't know why the second method didn't work. If that's the only horizontal constraint that you had, it should have worked.
In addition to pinning it to the right side with:
"H:[iNumberLabel]-|"
You should also rightAlight the text with:
cell.textLabel.textAlignment = NSTextAlignmentRight;
Edit: Only working for iOS 8. Currently no solution for iOS 7.
This was the culprit
this.ContentView.TranslatesAutoresizingMaskIntoConstraints = false;
I had this set when I created and added my labels as subview. When this is turned off the width of the cell seems to not get correctly laid out. Perhaps one has to add some additional constraints for the ContentView, but I don't know which (content view to super view?).
Auto resizing mask has to be enabled for the content view.

resize form in code to hide hidden items

i have a program i made that can pead replays from a game then get data out of them i am trying to add support for tag replays so i need 2 more list boxes and 4 more buttons on the form but i want them hidden when they aren't being used(when a tag replay isnt opened) i have tried autosize with a minimal and maximal width-height and it didnt work dont know what else to try oh and im using a table layout panel to organize everything
heres some shots to show what i want
Here's something to get you started.
Your controls resize with the form because the table layout is anchored to the form from the bottom.
So, what I just tried and could work for you is to temporarily turn off the bottom anchor, change the form height, then bring back the bottom anchor as it was.
Since you haven't shared any code, I'm using typical control names:
var oldanchor = tableLayoutPanel1.Anchor; // memorize the old anchor
tableLayoutPanel1.Anchor = AnchorStyles.Left | AnchorStyles.Top; // reset anchor
this.Height = 200; // change the form's height
tableLayoutPanel1.Anchor = oldanchor; // bring back the old anchor

Categories