c# Image and text on a button, centered in the button? - c#

I have a button on a C# Windows Form form, and want to show an image and some text, side by side, and centered on the button. I tried aligning the image to the left and the text to the right, and I'm getting this (the periods are spaces):
|[IMAGE}.................Text|
But I want this:
|........[IMAGE] Text........|
My code looks like this:
btnChangeStatus.Text = "Change status to SUPPRESSED";
btnChangeStatus.TextAlign = ContentAlignment.MiddleRight;
btnChangeStatus.Image=Image.FromFile(#"J:\nomail.gif");
btnChangeStatus.ImageAlign = ContentAlignment.MiddleLeft;
I've searched here, and found lots of stuff for Java or HTML, but nothing for C#. Any ideas?
Thanks!

Set TextImageRelation to TextImageRelation.ImageBeforeText:
btnChangeStatus.TextImageRelation = TextImageRelation.ImageBeforeText;
btnChangeStatus.TextAlign = ContentAlignment.MiddleCenter;
btnChangeStatus.ImageAlign = ContentAlignment.MiddleCenter;
Specifies that the image is displayed horizontally before the text of a control.
UPDATE: You are right, though it sounds like this should do what you want, it's still a little to the left.
I tried around a little and using
btnChangeStatus.TextImageRelation = TextImageRelation.ImageBeforeText;
btnChangeStatus.TextAlign = ContentAlignment.MiddleRight; // <- right here
btnChangeStatus.ImageAlign = ContentAlignment.MiddleCenter;
leads to the desired result, but I can't tell why the button behaves like that.

Related

C# Form text-align not working

i have created this label in design.
label TextAlign = MiddleCenter;
label AutoSize = false;
it looks fine in design but when i run it always align Left. No idea what is going on!
// load user image
picCerImage.Image = Image.FromFile(Global.avatarPath);
// load user fullname;
lbeCerFullName.Text = Global.userFullName;
this code is from FormLoad event and here is a picture when i run:
Sorry about my English!
I think this will work on that:
set the TextAlign to TopRight
set the Anchor to top and left.
OK! I found the problem with help of Martheen. Trim the string before loading it! :)
You can always look for other answers to problems like yours, like, for example, in How do I align my text in a label to the right side?.
Anyway, maybe this will work:
Adjust the label size so it's larger than the text.
Set the label Autosize property to False
Set the label TextAlign to TopRight, MiddleRight or BottomRight, depending on your choice.
I hope this works for you. Good luck!

Aligning text of TextBox in c# Right and Bottom

I'm making a calculator in c#. I know the logic required to make a calculator. But the problem I'm facing is with the UI, I want to make the text in my TextBox Right and Bottom, while there is TextAlign property which makes the text in the TextBox align to right, I'm having trouble making it align to the bottom?.I really tried finding an answer.
but I want something like this
Also how to move the text in the textbox slightly upper when I press any operator?
Assuming the fact that you only use the TextBox for displaying the user input, a Label will allow for better use of what you want.
The Label supports a wider use of TextAlign so you could use
Label1.TextAlign = ContentAlignment.BottomRight;
If you want to create multiple lines inside the label, you can use Environment.NewLine to achieve that for example. But a StringBuilder and using AppendLine will probably work too.
If you really need to use a TextBox to achieve your needs, your best option would probably be to write a custom TextBox which supports your wanted text align.
Think this is possible with a RichTextBox but it would be a long work around. I would suggest using a Label. To align the text to the bottom right in a label you can use:
label.TextAlign = ContentAlignment.BottomRight;
To change the look of the Label you can use:
label.BackColor = Color.White;
label.BorderStyle = BorderStyle.FixedSingle;
To change the label from looking like a single line:
label.AutoSize = false;
label.Size = new Size(100,100)

Create Horizontal Line in FlowDocument programmatically

I am programmatically creating a FlowDocument, then translating it to Rich Text, so a user can edit it in a Rich Text box. I cannot figure out how to insert a horizontal line in the FlowDocument that will display across the whole width of the edit box (and subsequent rendered PDF).
I found this thread which (allegedly) shows how to do exactly what I want, in XAML:
Simple (I think) Horizontal Line in WPF?
And this one:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/bcd334c7-8e2b-4e59-a344-4376b30cf847/
I have attempted to replicate this programmatically, like this:
Line pLine = new Line();
pLine.Stretch = Stretch.Fill;
pLine.Stroke = Brushes.Black;
pLine.X2 = 1;
para.Inlines.Add( pLine );
But, this ends up displaying nothing at all in the resulting RTF edit box. The rest of the Rich Text is there. Just no line at all.
I also tried creating a Table and inserting the text I want to come after the horizontal line into a cell. Then I set a custom border, just for the top of the Table. That gives me a horizontal line, but it doesn't go all the way across the screen (after it's converted to RTF and shown in the RTF edit box), and it doesn't show at all after I render it into a PDF.
I have resorted to the complete hack of just inserting a Run of 60 underscore ('_') characters and a LineBreak. That "works", but it sucks. Can anybody tell me how to do this correctly?
i am using this little monster to insert a line at the current cursor position in a RichTextBox.
var ruler = new Line { X1 = 0, Y1 = 0, X2 = 1000, Y2 = 0, Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 2 };
new InlineUIContainer(ruler, richtextbox.CaretPosition.GetInsertionPosition(LogicalDirection.Forward));
I think you forgot to set the StrokeThickness.
And even if you set Stretch.Fill your line is only one device independent unit long.
My experience with Otto's method was that it draws a nice horizontal line in the editor but the line is not saved in the RTF file. My handy little O'Reilly "RTF Pocket Guide" yielded a block of RTF that does both. Here is the click handler for my "Insert Line Break" toolbar button...
private void InsertLineBreak_Click(object sender, RoutedEventArgs e)
{
MemoryStream stream =
new MemoryStream(ASCIIEncoding.Default.GetBytes(#"{\pard\brdrb\brdrs\brdrw10\brsp20\par}"));
rtbEditor.Selection.Load(stream, DataFormats.Rtf);
}

Tooltip positioning C#

I have a picturebox that has a mousehover event to show a tooltip based on the status of a service. This seems to be working, but it kind of just pops up where the mouse is and sometimes under the mouse, in the middle of the picture, which doesn't look right. I was reading http://msdn.microsoft.com/en-us/library/windows/desktop/aa511495.aspx#infotipsgl and it suggested to have the tooltip moved off to the side. This would be great, but I can't figure out how to move it.
ToolTip on toolTip1 is blank and on the mouseHover event I have tried using
toolTip.SetToolTip(this.pictureBox1, "Message text.");
and
toolTip.Show("Message text.", pictureBox1);
Thanks
ToolTip tooltip = new ToolTip();
tooltip.Placement = PlacementMode.Right;
tooltip.PlacementRectangle = new Rect(50, 0, 0, 0);
tooltip.HorizontalOffset = 10;
tooltip.VerticalOffset = 20;
See Here for more details.
For windows forms you can use this overload of Show method. It allows you to set position offset relative to control that has tool tip.
In wpf as Ravi Patel has already pointed you to article simply us:
<ToolTip HorizontalOffset="10"
VerticalOffset="20" .../>

C# have end of text priorities

I'm concatenating a string that sometimes is long enough for it not to fit in a label control. How can i make it autoscroll to the rightside so i always see the end of the string?
While I'm sure there are ways of doing, I have to ask, why? I think it would look and/or work very badly and probably confuse the user.
Why not have the text get trimmed with an ellipse (...) at the end and show a tooltip on the label?
using System.Windows.Forms;
var label = new Label();
label.AutoSize = false;
label.AutoEllipsis = true;
label.Text = "This text will be too long to display all together.";
var labelToolTip = new ToolTip();
labelToolTip.SetToolTip(label, label.Text);
Now the tooltip will show the full text when the user hovers over it. Since the text in the label will be truncated and end in an ellipse, the user should know to hover over for more info (usually the standard way).
The TextAlign property allows you to specify the alignment. If you right-justify it with this, the right side of the text will always be visible. However, if you want it to be left or center justified and still have the behavior you describe, I suspect you will need to perform some measurement using Graphics.MeasureString to determine if the text fits and change the alignment dynamically.
AFAIK there's no way to scroll a label. A hack would be to use a TextBox (read-only, turn off border) then use SendKeys.Send() to move the cursor to the end of the text. Something like:
textBox1.Focus();
SendKeys.SendWait("{END}");
To get the text to not show up as selected I had to change it's position in the tab order (so that it wasn't 1) but that may not be a problem in your case.

Categories

Resources