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)
Related
I have been trying to find how to change a rich text box into ONE color specifically, not multiple colors in one. Google has been no help, because it assumes I am looking for multiple lines of random colors. I saw nothing here, but I'm sure there is something on here, but I have yet to figure out how to ask the question properly I guess. So I ask it here. How do I do it? My code only displayed the color name in the rich text box, and changed the color of the start button which is not at all what I want. The eventual idea will be that everything that is typed that is the same, will be of that color. I figured this would be the simplest way of accomplishing that.
This is what I typed:
if(TypeHere.Text == DisplayText.Text)
{
DisplayText.Text = Convert.ToString(ForeColor = System.Drawing.Color.Blue);
}
That obviously didn't work like I thought it would. I'm sure I'm either missing a crucial step, or not entirely understanding how colors work on WF. I placed this in the TypeHere. The output of this in DisplayText is:
Color[Blue]
It wouldn't let me just set DisplayText to equal the color implicitly. I realize why it displays the name in the textbox, but I don't understand why it changes the color of the text in my button? I've tried it both as a method, and a couple of lines of code in the text box. So it's obviously not where I place the code, but something else.
Set the ForeColor property of the RichTextBox. This will change the font color in the RichTextBox.
richTextBox1.ForeColor = Color.Blue;
I'm making an RPG-style text-based game. Currently it is in a Console Application, but I would rather move it to a Windows Form Application, for some added features and fanciness. This brings up a few issues.
Colors differentiate the types of messages, so color is important. I found this on another question:
int length = richTextBox.TextLength; // at end of text
richTextBox.AppendText(mystring);
richTextBox.SelectionStart = length;
richTextBox.SelectionLength = mystring.Length;
richTextBox.SelectionColor = Color.Red;
This seems to add the color after the text. Is there a better way to go about colors? Also, what if I want multiple different colors on one line? That complicates things. (It would be best if I could use color codes to designate the colors in the strings themselves, something like &&4this is red &&fthis is white &&0this is black, but I suppose that might just over-complicate things.)
Currently, the input box is a simple text box. When you press enter, nothing happens. I want enter to clear the text box and output a string that I can work with (not necessarily add to the rich text box).
What I assumed in my comment is true, just tested it. Change your code to this:
Color prevColor = richTextBox.SelectionColor;
richTextBox.SelectionColor = Color.Red;
richTextBox.AppendText(mystring);
richTextBox.SelectionColor = prevColor;
I need a certain portion of my text in RTB to be highlighted not in the sense of changing the font style/color, but in the sense of making a block selection with a particular color. This is similar to how Visual Studio highlights a line during debug mode.
How can I accomplish this feature using RTB or rather, is it even possible? If it isn't possible, I'd like to hear another way of performing the above task.
Yes you can set the BackColor of a RichTextBox Selection using the RichTextBox.SelectionBackColor Property.
int blockStart = 1; //arbitrary numbers to test
int blockLength = 15;
richTextBox1.SelectionStart = blockStart;
richTextBox1.SelectionLength = blockLength;
richTextBox1.SelectionBackColor = Color.Yellow;
I think you are looking for ScintillaNET.
On the other hand if you want to do this by yourself in RTB then you can do it by first finding the lineNumber using TextBoxBase.Lines property. Then ...
//Select the line from it's number
startIndex = richTextBox.GetFirstCharIndexFromLine(lineNumber);
richTextBox.Select(startIndex, length);
//Set the selected text fore and background color
richTextBox.SelectionColor = System.Drawing.Color.White;
richTextBox.SelectionBackColor= System.Drawing.Color.Blue;
Here I have created CustomRichTextBox to achieve this.
The source code a long with scenario is explained here. If you interested then you can reuse this usercontrol directly without worry about much
Scenario
https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/customwpfrichtextboxwithcolorchangeandhighlightfunctionality
source code:
https://github.com/boobalaninfo/CustomRichTextBoxWithHighligh
I need to adjust height of the textbox so it is suitable for touchscreen.
I understand people recommend Multiline = "true" but if I do that, text inside of the box is justified with top which is not proper in my application.
I tried to adjust font size, but the size should be ridiculously huge to fit the height for my need.
Is there any other way to increase the height of textbox?
Try this:
textBox1.AutoSize = false;
It won't show up in the intellisense, but it will work.
To have it work with the designer, you would have to make your own TextBox:
public class TextBoxEx : TextBox {
public TextBoxEx() {
this.AutoSize = false;
}
}
Trick steps:
Set the multi-line = true
No need to change the Font size.
change the Max-length . so that it should not enter the next line.
For what I wanted to do using a label instead with BorderStyle=Fixed3D and AutoSize=False did the trick.
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.