I want to change font style for a textbox when I click the button.For this my code is below and it is okay;
protected void Button1_Click(object sender, System.EventArgs e)
{
TextBox1.Font.Size = FontUnit.XLarge;
TextBox1.ForeColor = System.Drawing.Color.Crimson;
TextBox1.BackColor = System.Drawing.Color.Snow;
TextBox1.BorderColor = System.Drawing.Color.HotPink;
}
But Can I do where I select of write which is in the textbox it changes only that part.For example textbox1.Text="Computer Programs" and the user select only "Computer" part of textbox1.Only "Computer" part must be changed.
From the sounds of it you want to format only part of the text? For that you'll need to look at a RichTextBox control.
With a RichTextBox you can make use of text selections and set formatting on only those areas:
RichTextBox rich = new RichTextBox();
rich.Text = "Here is some text for the Rich Text Box";
rich.SelectionStart = 0;
rich.SelectionLength = 4;
rich.SelectionFont = new Font(rich.Font, FontStyle.Bold);
After you're done setting your font to your selection you should set the font immediately afterwards back to the original so that you don't continue the style outside the area you wanted to apply it:
rich.SelectionStart = rich.SelectionStart + rich.SelectionLength;
rich.SelectionLength = 0;
rich.SelectionFont = rich.Font;
This should result in "Here is some text for the Rich Text Box" changing to look like: "Here is some text for the Rich Text Box".
Related
I am creating a custom control, a custom looking TextBox designed to only allow numeric input. It consists of a plain control, with a TextBox as one of it's properties:
class NumericControl : Control
{
private TextBox text;
//rest of the code
}
This TextBox is drawn inside of the control. It all works very nicely, except that when you press enter it makes this horrible DING noise. To fix this, I thought I'd make the TextBox multiline. However, when I do this, because the font size of the TextBox is changed to resize it, the blinking cursor inside the TextBox appears to disappear, which is a problem because then you can't tell by looking at it whether or not it has focus. If I don't change the font size of the TextBox, the cursor appears and acts normally, however I need the font size to change based on the height of the control, otherwise it doesn't look any good.
The code setting the TextBox properties is as follows, and resides within the constructor of my control:
text = new TextBox();
text.AutoSize = false;
text.Left = 10;
text.Top = 2;
text.Text = "0";
text.Multiline = true;
text.BorderStyle = BorderStyle.None;
text.TextChanged += text_TextChanged;
text.LostFocus += text_LostFocus;
this.Controls.Add(text);
The font size is changed inside the OnPaint event, and looks like this:
text.BackColor = Enabled ? Background : SystemColors.Control; //Fixes an issue I had with disabled TextBox BackColor being able to be changed
text.Font = new Font(TextBox.DefaultFont.FontFamily, (float)(rc.Height * 0.7 - 2), FontStyle.Regular);
text.Width = this.Width - 21;
text.Height = this.Height - 4;
How do I both resize the textbox (and font size), and make it multiline while retaining the blinking cursor when it has focus?
I'm making a word processor, and want the user to have the ability to bold only the text you select on windows phone with the tap/hold/drag. I know how to detect what text is selected by the user with Filebox.SelectedText (filebox is the name of the textbox), but have no idea where to go from there - how do I take the text that is selected and ONLY make that selected text bolded?
Note: I am using ComponentOne's Rich Textbox control.
You can make use of the RichTextBox.Selection property.
For example:
private void ModifySelectedText()
{
// Determine if text is selected in the control.
if (richTextBox1.SelectionLength > 0)
{
// Set the color of the selected text in the control.
richTextBox1.SelectionColor = Color.Red;
// Set the font of the selected text to bold and underlined.
richTextBox1.SelectionFont = new Font("Arial",10,FontStyle.Bold | FontStyle.Underline);
// Protect the selected text from modification.
richTextBox1.SelectionProtected = true;
}
}
I have a problem, im making me own custom SharePoint webpart.
everything is going well, but the problem is that i can't figure out how to change the location of the textboxes and labels.
anyone knows how i can change the locations?
I am trying to accomplish it in C#.
problem SOLVED.
With the help of component ids. set position of that particular component.
"How to change the location of the textboxes and labels"
In this example i'm using a Button (Action performed on Button Click) and i am also adding how to Generate a TextBox and a Label (When you press this Button).
Just because this is usually a common process within setting locations to a control.
private void button1_Click(object sender, EventArgs e)
{
// Settings to generate a New TextBox
TextBox txt = new TextBox(); // Create the Variable for TextBox
txt.Name = "MyTextBoxID"; // Identify your new TextBox
// Create Variables to Define "X" and "Y" Locations
var txtLocX = txt.Location.X;
var txtLocY = txt.Location.Y;
//Set your TextBox Location Here
txtLocX = 103;
txtLocY = 74;
// This adds a new TextBox
this.Controls.Add(txt);
// Now do the same for Labels
// Settings to generate a New Label
Label lbl = new Label(); // Create the Variable for Label
lbl.Name = "MyNewLabelID"; // Identify your new Label
// Create Variables to Define "X" and "Y" Locations
var lblLocX = lbl.Location.X;
var lblLoxY = lbl.Location.Y;
//Set your Label Location Here
lblLocX = 34;
lblLoxY = 77;
// Adds a new Label
this.Controls.Add(lbl);
}
}
Note: This is just an example and will not work after postback.
I hope this answers to your and everyone's question.
What is the easiest way to recreate the effect where a text box displays a certain string (in italics and different font) until the user has clicked into the control and/or written his own text into the field? For an example, look at the "search" box at the top right of SO.
I have tried consuming the Paint event:
private void textEdit1_Paint(object sender, PaintEventArgs e)
{
if (textEdit1.Text.Length == 0 && !textEdit1.Focused)
{
textEdit1.Font = new Font(textEdit1.Font, FontStyle.Italic);
textEdit1.Text = "123";
}
else
{
textEdit1.Font = new Font(textEdit1.Font, FontStyle.Regular);
textEdit1.Text = string.Empty;
}
}
However, that's not working. By default, it shows no text, and if I click into it, I seem to get an infinite loop of setting the text to "123" and string.empty, until I give another control focus.
So, is that approach even the best, and if yes, what's the correct 2nd condition instead of .Focused?
Try the TextEdit.Properties.NullValuePrompt property. This property provides the text displayed grayed out when the editor doesn't have focus, and its edit value is not set to a valid value.
First of all, you shouldn't use the paint event, you should use the FocusChanged event if you want to do it by modifying the text property. However, the simplest method is not to modify the text property, but draw a string on top, like this:
private void textEdit1_Paint(object sender, PaintEventArgs e)
{
if (textEdit1.Text.Length == 0 && !textEdit1.Focused)
{
Font some_font = new Font(...parameters go here...);
Brush some_brush = Brushes.Gray; // Or whatever color you want
PointF some_location = new PointF(5,5); // Where to write the string
e.Graphics.WriteString("some text", some_font, some_brush, some_location);
}
}
So, if there is no text, and text box is not focused, draw this string. There are many overloads of the WriteString function, so you can pick which one you want.
You can use the Enter event. Set Text property to "search" for example. Use your font like others reported. Then catch the Enter event and set the Text property to string.empty.
textedit1.Text = "search";
private void textEdit1_Enter(object sender, EnterEventArgs e)
{
textedit1.text = string.empty;
}
But i think the best practice is the NullValuePrompt.
In Visual C#.NET:
How do I add/append text directly to where the user's cursor is in a Rich Text Box?
For example, if the user clicked a button, and their cursor was somewhere in the rich text box, text would be immediately added to the location of their cursor.
Use the SelectedText property:
textBox.SelectedText = "New text";
This will overwrite any selected text they have though. If you don't want that you can first set the SelectionLength property to 0:
textBox.SelectionLength = 0;
textBox.SelectedText = "New text";
rtb.SelectionStart += rtb.SelectionLength;
rtb.SelectionLength = 0;
rtb.SelectedText = "asdf";
This moves the cursor just past the end of the current selection, then adds "asdf" at the end.