I have created a TextBox on the code
RichTextBox tb3 = new RichTextBox();
Then I wrote an other code (in the same void) to change it's font:
tb3.Font.Size(FontSize.Text);
But the following error appeares : "non-invocable member 'Font Size' cannot be used like a method.
Note: "FontSize" is an ID for a textbox.
How can I do it without creating a new Font?
Please help!
tb3.Font.Size isn't a method, it is
public float Size { get; }
and you can't change it so.
if you want to change font see it -> link1
and it link2
you should create new font, for example:
tb3.Font = new Font(tb3.Font.FontFamily, 23, FontStyle.Regular);
Related
I am using this simple example from MSDN
to insert lines in a RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
myFlowDoc.Blocks.Add(myParagraph);
RichTextBox myRichTextBox = new RichTextBox();
myRichTextBox.Document = myFlowDoc;
I want to apply a chosed color to the lines of text, but how to do it?
The Paragraph or Run classes doesn't have any direct method to change the color.
EDIT
I don't want to use all the awkard SelectionStart, SelectionEnd stuff as posted on the linked post!.
My case is different and is much more simple: the solution posted from mm8 explains it and is very elegant.
One single line of code and that is!
Please see the answer!
The Paragraph or Run classes doesn't have any direct method to change the color.
The Run class inherits from TextElement and this class has a Foreground property that you can set to a Brush:
Run myRun = new Run("This is flow content and you can ") { Foreground = Brushes.Red };
Bold myBold = new Bold(new Run("edit me!") { Foreground = Brushes.Gray });
You can get/set text color via Foreground property of the rich text box. As bellow example, I changed the text color of rich text box to blue:
myRichTextBox.Foreground = Brushes.Blue;
Happy coding!
I have an application in C# that would print invoices and payslips. The client have sent me a template which would be used for the day to day operations. I don't know how to print to it, though I already know how to print a programmatically made text file which contains the information from an access database.
How do I print the information on this kind of template? (This is only something I [found on Google][1] and is a good candidate for a simple invoice printing) The document template I have also have a LOGO..
Do it by mail merge in Word. Using this technique you create Word document. Inside document you create placeholders for text. And from code you fill placeholders with whatever you want.
For example:
In word document type ctrl + F9
Right click on field and Edit field
Choose MergeField
On field name type FirstName
Add code:
.
var document = new Document("document.docx");
var sqlCommand = "SELECT TOP 1 userName FirstName FROM Users";
var table = GetTable(sqlCommand, String.Empty);
document.MailMerge.Execute(table);
I've been using the PrintDocument and PrintPreview objects. That use the the Graphics class. When print is called you get an "PrintEventArgs e" object. Then you can use e.Graphics to have access to things like e.Graphics.DrawString, .DrawImage etc.
I built a whole print objects class that overrides print. So I have a detail box that has different fonts, or a logo, a header, leagal jargon etc. Each one of these has it's own class. I put them all in a big list and I call printThis(List); and it will take each print function and the coordinates and make me a form.
Inherited object
class formHdr : printObject
{
private string headerText;
public formHdr(string hText)
: base()
{
headerText = hText;
}
public override void printThis(System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString(headerText, FRHEADER, Brushes.Black, BaseX, BaseY);
}
}
Base class
abstract class printObject
{
protected Font FTHEADER;
protected Font NRML;
protected Font DETAIL;
protected Font FRHEADER;
protected Font DETHEADER;
protected Font LEGAL;
protected Font LEGAL2;
public int baseX, baseY;
public int boxSX, boxSY;
public printObject()
{
baseX = 0;
baseY = 0;
boxSX = 0;
boxSY = 0;
FTHEADER = new Font("Arial", 12, FontStyle.Bold);
NRML = new Font("Calibri", 10);
DETAIL = new Font("Consolas", 8);
FRHEADER = new Font("Arial", 16, FontStyle.Bold);
DETHEADER = new Font("Calibri", 10, FontStyle.Bold);
LEGAL = new Font("Arial", 8);
LEGAL2 = new Font("Arial", 10);
}
public virtual void printThis(PrintPageEventArgs e) { }
Object creation
mainHead = new formHdr("Bill of Lading/Weigh slip Original");
mainHead.BaseX = 225;
mainHead.BaseY = 20;
bol.Add(mainHead);
Maybe this can get you started? I'm still tweaking it and will be interested in other responses.
I'm getting the following exception when changing my application font, because I use a strike out in a part of my application, and some fonts don't support it:
I change my application font using a font dialog. I need to check if the selected font supports the strikeout style after assigning it to my application.
What is the recommended way to do this? I know I could create a font with the style and catch the exception, but is there a more elegant way to do it?
Thanks in advance.
EDIT: The user selects a font, not necesary strikeout. In that moment I need to check if the font supports the style strikeout, because I create a strikeout font in a part of my application. If the font don't support the strikeout style would not allow the user to choose that font.
Updated : (to reflect update in the initial post):
InstalledFontCollection ifc = new InstalledFontCollection();
for (int i = 0; i < ifc.Families.Length; i++)
{
if (ifc.Families[i].IsStyleAvailable(FontStyle.StrikeOut))
{
//add particular font with this family to your "font selector"
}
}
If you are using the standard Font class, then you can use the Font.Strikeout property:
//Gets a value that indicates whether this Font specifies a horizontal line through the font.
public bool Strikeout { get; }
Finally I used the following:
private bool SupportStrikeout(Font font)
{
try
{
using (Font strikeout = new Font(font, FontStyle.Strikeout))
{
return true;
}
}
catch (ArgumentException)
{
return false;
}
}
For my single line Textbox, I set is Border = None. On doing this, the height turns very small. I can't programamtically set the height of the textbox. If I set any border, then again its fine, but I don't want any border. Even the text is not visible completely - so the font size is already bigger the the textbox height.
I tried creating a custom textbox, and set the Height of it, but it has no effect. How to handle this situation? Any help is highly appreciated.
There is a simple way not to create a new class.
In Designer.cs file:
this.textBox1.AutoSize = false;
this.textBox1.Size = new System.Drawing.Size(228, 25);
And that's all.
TextBox derives from Control, which has an AutoSize property, but the designers have hidden the property from the PropertyGrid and Intellisense, but you can still access it:
public class TextBoxWithHeight : TextBox {
public TextBoxWithHeight() {
base.AutoSize = false;
}
}
Rebuild and use.
TextBox controls automatically resize to fit the height of their Font, regardless of the BorderStyle you choose. That's part of the defaults used by Visual Studio.
By changing the Multiline, you can override the Height.
this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif",
26.25F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.textBox1.Location = new System.Drawing.Point(373, 502);
// this is what makes the height 'stick'
this.textBox1.Multiline = true;
// the desired height
this.textBox1.Size = new System.Drawing.Size(100, 60);
Hope this helps.
I just created this case in an empty project and don't see the result you are describing.
When the BorderStyle is none, the display area of the Textbox auto-sizes to the font selected. If I then set Multiline = true, I can change the height portion of the Size property and the change sticks.
Perhaps another portion of your code is modifying the height? A resize event handler perhaps?
My suggestions:
Post the relevant portions of your code
Try to reproduce the issue in an empty WinForms project (as I just did)
I find the best solution is to subclass the Textbox and expose the hidden AutoSize there:
public class TextBoxWithHeight : TextBox
{
public bool Auto_Size
{
get { return this.AutoSize; }
set { this.AutoSize = value; }
}
}
Now you can set the Autosize on or off using the object inspector in the visual designer or in code, whatever you prefer.
Just select your textbox and go to properties then increase your font size.. DONE !!!
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);