How Can Format Text Inside TextBoxes In WinForms - c#

as you know in web applications there are some third party libraries that we can use for convert a text box to an editor like below :
tinymce
but what about win forms?
how can i format text inside Text Boxes in win forms?
i mean i want to convert regular text boxes in a form to an editor by setting a property(is it possible or not?).
how can i do that?

public void CreateMyRichTextBox()
{
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Dock = DockStyle.Fill;
richTextBox1.LoadFile("C:\\MyDocument.rtf");
richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);
richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SaveFile("C:\\MyDocument.rtf", RichTextBoxStreamType.RichText);
this.Controls.Add(richTextBox1);
}

Related

Making richtextbox bold in C# .Net

I'm trying to make bold text in richTextBox. Text I want to make bold is in a second row "Cpu usage" in code.
Can you edit this code to make it bold?
double cpuUsage = Math.Round(perfCpuCount.NextValue(), 2);
MonitorLog.AppendText(string.Format("Cpu usage: {0}%\r\n\n", cpuUsage));
Change the SelectionFont to include the bold style, and write the text that should be bold, then switch it back to regular:
MonitorLog.SelectionFont = new Font(MonitorLog.Font, FontStyle.Bold);
MonitorLog.AppendText("CPU Usage:");
MonitorLog.SelectionFont = new Font(MonitorLog.Font, FontStyle.Regular);
MonitorLog.AppendText(string.Format(" {0}%\r\n\n", cpuUsage));

changing font style of textbox

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".

How do I change the font of only the highlited text?

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;
}
}

How to show DateTimePicker control on messagebox using c#?

Is it possible to show DateTimePicker control on messagebox to select date in Console applciation.
Not on a standard MessageBox. You need to create your own form to display.
`public MyClass()
{
// Create a new DateTimePicker
DateTimePicker dateTimePicker1 = new DateTimePicker();
Controls.AddRange(new Control[] {dateTimePicker1});
MessageBox.Show(dateTimePicker1.Value.ToString());
dateTimePicker1.Value = DateTime.Now.AddDays(1);
MessageBox.Show(dateTimePicker1.Value.ToString());
}
`
This a method to access the date picker control in your message box.
What have you done exactly? Can you explain the requirement a little more elaborate?
MessageBox is a convenient function. That convenience comes at the cost of customization. MessageBox does not provide a way to add your own controls to it; you must create your own form.
Note that even in console applications you can still create windows forms.
I know its late but if someone is looking how to do it. Use CustomMessageBox insetead of MessageBox. Tutorial can be found Here
TimePicker timePicker = new TimePicker()
{
Margin = new Thickness(0, 10, 0, 10)
};
StackPanel myStackPanel = new StackPanel();
myStackPanel.Orientation = System.Windows.Controls.Orientation.Vertical;
myStackPanel.Children.Add(timePicker);
//Create a new custom message box
CustomMessageBox messageBox = new CustomMessageBox()
{
Content = myStackPanel,
LeftButtonContent = "Ok",
RightButtonContent = "Cancel",
IsFullScreen = false
};
messageBox.Show();

C# How to change font of a label

A form with a label and a button 'Options'. By clicking the button a new form opens with 2 radio buttons 'Font1' and 'Font2', and two buttons 'Apply' and 'Cancel'. Upon selecting one of the radio buttons and clicking 'Apply' will make the label on the first form change the font face. The problem is how to change the font as in from say Tahoma to Arial or to any other font face of the label.
Options form code for apply button, which if was clicked will return dialogresult.ok == true and change the font of the label on the first form:
private void btnApply_Click(object sender, EventArgs e)
{
if (radioFont1.Checked)
{
mainForm.lblName.Font.Name = "Arial"; 'wrong attempt
}
this.DialogResult = DialogResult.OK;
}
Declaration of the label on first form so that it is visible to second form:
public static Label lblName = new Label();
...
private void mainForm_Load(object sender, EventArgs e)
{
lblName = lblBarName;
}
Font.Name, Font.XYZProperty, etc are readonly as Font is an immutable object, so you need to specify a new Font object to replace it:
mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);
Check the constructor of the Font class for further options.
You can't change a Font once it's created - so you need to create a new one:
mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);
You need to create a new Font
mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);
I noticed there was not an actual full code answer, so as i come across this, i have created a function, that does change the font, which can be easily modified. I have tested this in
- XP SP3 and Win 10 Pro 64
private void SetFont(Form f, string name, int size, FontStyle style)
{
Font replacementFont = new Font(name, size, style);
f.Font = replacementFont;
}
Hint: replace Form to either Label, RichTextBox, TextBox, or any other relative control that uses fonts to change the font on them. By using the above function thus making it completely dynamic.
/// To call the function do this.
/// e.g in the form load event etc.
public Form1()
{
InitializeComponent();
SetFont(this, "Arial", 8, FontStyle.Bold);
// This sets the whole form and
// everything below it.
// Shaun Cassidy.
}
You can also, if you want a full libary so you dont have to code all the back end bits, you can download my dll from Github.
Github DLL
/// and then import the namespace
using Droitech.TextFont;
/// Then call it using:
TextFontClass fClass = new TextFontClass();
fClass.SetFont(this, "Arial", 8, FontStyle.Bold);
Simple.
this.lblMessage.Font = new Font("arial", this.lblName.Font.Size);

Categories