ListBox AllowHTML in c# - c#

is there a way to display different TextFormat/Style per item in ListBox
Example:
Data1: value
Data2: value world
Data3: value hello
i tried this but i cant make the next string in bold
private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Rectangle rec = e.Bounds;
string[] temp = lbDetails.Items[e.Index].ToString().Split(':');
e.Graphics.DrawString(temp[0],
new Font("Arial",8, FontStyle.Regular),
Brushes.Black,
rec,
StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
SOLUTION
private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
e.DrawBackground();
string[] temp = lbDetails.Items[e.Index].ToString().Split(':');
SizeF prevStr = e.Graphics.MeasureString(temp[0] + ": ", new Font("Arial", 8, FontStyle.Regular));
SizeF nextStr = e.Graphics.MeasureString(temp[1], new Font("Arial", 8, FontStyle.Bold));
RectangleF firstRec = new RectangleF(e.Bounds.X, e.Bounds.Y, prevStr.Width, prevStr.Height);
RectangleF secondRec = new RectangleF(prevStr.Width, e.Bounds.Y, nextStr.Width, nextStr.Height);
e.Graphics.DrawString(temp[0] + ": ",
new Font("Arial", 8, FontStyle.Regular),
Brushes.Black,
firstRec,
StringFormat.GenericDefault);
e.Graphics.DrawString(temp[1],
new Font("Arial", 8, FontStyle.Bold),
Brushes.Black,
secondRec,
StringFormat.GenericDefault);
//e.Graphics.DrawRectangle(Pens.Red, firstRec.X, firstRec.Y, firstRec.Width, firstRec.Height);
e.DrawFocusRectangle();
}
catch (Exception ex)
{
}
}

You need to draw two strings, the second one using a bold font. A full example:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
listBox1.Items.AddRange(new object[] { "Data1: value", "Data2: hello world", "Data3: hello hello"});
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e) {
var box = (ListBox)sender;
e.DrawBackground();
if (e.Index < 0) return;
string[] temp = box.Items[e.Index].ToString().Split(':');
int size = (int)(TextRenderer.MeasureText(e.Graphics, temp[0], box.Font).Width + 0.5);
var rc = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int)size, e.Bounds.Height);
var fmt = TextFormatFlags.Left;
TextRenderer.DrawText(e.Graphics, temp[0] + ":", box.Font, rc, e.ForeColor, fmt);
if (temp.Length > 1) {
using (var bold = new Font(box.Font, FontStyle.Bold)) {
rc = new Rectangle(e.Bounds.Left + size, e.Bounds.Top, e.Bounds.Width - size, e.Bounds.Height);
TextRenderer.DrawText(e.Graphics, temp[1], bold, rc, e.ForeColor, fmt);
}
}
e.DrawFocusRectangle();
}
}
Produces:

You can do it as mention in this Link. I know you are trying it differently, but it can help you.
There is a other link as well. This link describe how you can display items in a ListBox with different font style.
Here is other good link as well.

Related

how can i add page number to each copy from printDocument in c#

Please can you help me on how I can add the page number when printing several copies of a document?
I am using Windows Forms, and printdocument, in my printpage I insert a counter but it only respects it if I generate an impression at the same time, and if I put 5 copies it sends me the same page number in the 5 pages and I want the page number to appear consecutively in the copies
This is my code:
private int pagenum;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
using (var g = e.Graphics)
{
using (var fnt = new Font("Courier New", 10, FontStyle.Bold))
{
string content = Contenido.Text;
Barcode codigo = new Barcode();
codigo.Alignment = AlignmentPositions.CENTER;
codigo.LabelFont = new Font(FontFamily.GenericMonospace, 8, FontStyle.Bold);
codigo.IncludeLabel = true;
Image img = codigo.Encode(TYPE.CODE128, content, Color.Black, Color.White, 250, 70);
pictureBox.Image = img;
var f = new Font("Courier New", 8, FontStyle.Bold);
g.DrawString("SEMayr", fnt, Brushes.Black, 147, 10);
g.DrawString("1er. Turno", fnt, Brushes.Black, 150, 40);
g.DrawImage(pictureBox.Image, 98, 168, 201, 55);
e.Graphics.DrawString("Pg " + pagenum, f, Brushes.Black, 215, 230);
pagenum++;
var caption = textBox1.Text;
g.DrawString(caption, fnt, Brushes.Black, 118, 70);
}
}
}
private void button2_Click(object sender, EventArgs e){ try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
// Especifica que impresora se utilizara!!
pd.PrinterSettings.PrinterName = "Datamax";
PageSettings pa = new PageSettings();
pa.Margins = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.Margins = pa.Margins;
PaperSize ps = new PaperSize("Custom", 403, 244);
pd.DefaultPageSettings.PaperSize = ps;
pd.PrinterSettings.Copies = (short)Convert.ToInt32(label4.Text);
pd.Print();
}
catch (Exception exp)
{
MessageBox.Show("Printing " + exp.Message);
}
}
Well, copies are supposed to be identical. So when you set PrinterSettings.Copies = n you instruct the printer to output n copies of what you have in the PrintPage event handler, including the page number, it won't increment for each copy.
To keep the counter, don't set the PrinterSettings.Copies property and use the e.HasMorePages to print the contents n times.
Therefore (read the comments):
// +
using System.Drawing.Printing;
//
int copies;
int pageNum;
private void button2_Click(object sender, EventArgs e)
{
pageNum = 0;
copies = int.Parse(label4.Text);
// If this does not change, then no need
// to have it in the PrintPage event.
// Otherwise keep it as is.
Barcode codigo = new Barcode
{
Alignment = AlignmentPositions.CENTER,
LabelFont = new Font(FontFamily.GenericMonospace, 8, FontStyle.Bold),
IncludeLabel = true
};
Image img = codigo.Encode(TYPE.CODE128, Contenido.Text, Color.Black, Color.White, 250, 70);
pictureBox.Image = img;
using (PrintDocument pd = new PrintDocument())
{
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
pd.PrinterSettings.PrinterName = "Datamax";
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 403, 244);
pd.Print();
}
// If the Barcode implements IDisposable interface...
// codigo.Dispose();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
copies--;
pageNum++;
// Don't dispose of the e.Graphics object.
// You didn't create it then its not your call.
var g = e.Graphics;
using (var fnt = new Font("Courier New", 10, FontStyle.Bold))
using (var f = new Font("Courier New", 8, FontStyle.Bold))
{
g.DrawString("SEMayr", fnt, Brushes.Black, 147, 10);
g.DrawString("1er. Turno", fnt, Brushes.Black, 150, 40);
g.DrawImage(pictureBox.Image, 98, 168, 201, 55);
g.DrawString($"Pg {pageNum}", f, Brushes.Black, 215, 230);
g.DrawString(textBox1.Text, fnt, Brushes.Black, 118, 70);
e.HasMorePages = copies > 0;
}
}
Further, consider drawing the strings in rectangles with the StringFormat class. See the Graphics.DrawString method overloads.

CheckedListBox: How to increase size of checkbox and change color of tick mark

I want to override default look of CheckedListBox as below:
Please Note the increased size of checkbox and colored tick mark.
For this you need to create own custom control by inheriting CheckedListbox and need to override OnDrawItem(DrawItemEventArgs e) event
Below is the Code :
class BigCheckedListBox : CheckedListBox
{
public BigCheckedListBox()
{
ForeColor = Color.Turquoise;
Font = new Font("Segoe UI", 12f);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
var b = e.Bounds;
var state = GetItemChecked(e.Index) ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal;
Size glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state);
int checkPad = (b.Height - glyphSize.Height) / 2;
var pt = new Point(b.X + checkPad, b.Y + checkPad);
Rectangle rect = new Rectangle(pt, new Size(20, 20));
e.Graphics.DrawRectangle(Pens.Green, rect);//This is for Checkbox rectangle
//This is for drawing string text
using (SolidBrush brush = new SolidBrush(ForeColor))
e.Graphics.DrawString(this.Items[e.Index].ToString(), Font, brush, pt.X + 27f, pt.Y);
if (state == CheckBoxState.CheckedNormal)
{
using (SolidBrush brush = new SolidBrush(ForeColor))
using (Font wing = new Font("Wingdings", 17f, FontStyle.Bold))
e.Graphics.DrawString("ü", wing, brush, pt.X-4, pt.Y-1); //This is For tick mark
}
}
}
Hope this will serve the purpose.

How to change the background color of unused space tab in C# winforms?

Ex
|Tab1|Tab2|Tab3| { }
| |
| |
| |
| |
|_____________________|
I am able to change the backcolor and forecolor of Tab.. but I want to change the color of that { } -- > Empty space is this possible to do that. .. It shows default winforms color..help me in dis..
private void Form1_Load(object sender, EventArgs e)
{
}
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Font fntTab;
Brush bshBack;
Brush bshFore;
if ( e.Index == this.tabControl1.SelectedIndex)
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.Black;
//bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
//bshFore = Brushes.Blue;
}
else
{
fntTab = e.Font;
bshBack = new SolidBrush(Color.Red);
bshFore = new SolidBrush(Color.Aqua);
//bshBack = new SolidBrush(Color.White);
//bshFore = new SolidBrush(Color.Black);
}
string tabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;
recTab = new Rectangle( recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
}
Try adding the following code to your DrawItem event handler. Don't forget to set the DrawMode to "OwnerdrawFixed".
You might have to tweak it a bit to cover some margins which aren't painted.
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
SolidBrush fillbrush= new SolidBrush(Color.Red);
//draw rectangle behind the tabs
Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
Rectangle background = new Rectangle();
background.Location = new Point(lasttabrect.Right, 0);
//pad the rectangle to cover the 1 pixel line between the top of the tabpage and the start of the tabs
background.Size = new Size(tabControl1.Right - background.Left, lasttabrect.Height+1);
e.Graphics.FillRectangle(fillBrush, background);
}
'This answer is much better than prior one. But the tabCtrl is not defined. It has to be tabControl1 control.
I think the only way to give that space a color is to override the OnPaintBackground method of the window, so just paste this on your form (window)
you must also change the Appearance Property to "Normal"
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
RectangleF emptyspacerect = new RectangleF(
lasttabrect.X + lasttabrect.Width + tabControl1.Left,
tabControl1.Top + lasttabrect.Y,
tabControl1.Width - (lasttabrect.X + lasttabrect.Width),
lasttabrect.Height);
Brush b = Brushes.BlueViolet; // the color you want
e.Graphics.FillRectangle(b, emptyspacerect );
}
for me it's working perfectly
you can also create a custom tabcontrol as you did
public class mytab : TabControl
{
public mytab()
: base()
{
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
}
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Font fntTab;
Brush bshBack;
Brush bshFore;
if (e.Index == this.SelectedIndex)
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.Black;
//bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
//bshFore = Brushes.Blue;
}
else
{
fntTab = e.Font;
bshBack = new SolidBrush(Color.Red);
bshFore = new SolidBrush(Color.Aqua);
//bshBack = new SolidBrush(Color.White);
//bshFore = new SolidBrush(Color.Black);
}
string tabName = this.TabPages[e.Index].Text;
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;
recTab = new Rectangle(recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
Rectangle r = this.GetTabRect(this.TabPages.Count - 1);
RectangleF tf =
new RectangleF(r.X + r.Width,
r.Y-5, this.Width - (r.X + r.Width)+5, r.Height+5);
Brush b = Brushes.BlueViolet;
e.Graphics.FillRectangle(b, tf);
}
}

Set TabPage Header Color

Greetings,
I have a tab control and I want to have 1 of the tabs have it's text color changed on a event.
I've found answers like C# - TabPage Color event
and C# Winform: How to set the Base Color of a TabControl (not the tabpage)
but using these sets all colors instead of one.
So I was hoping there is a way to implement this with the tab I wish to change as a method instead of a event?
Something like:
public void SetTabPageHeaderColor(TabPage page, Color color)
{
//Text Here
}
If you want to color the tabs, try the following code:
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
private Dictionary<TabPage, Color> TabColors = new Dictionary<TabPage, Color>();
private void SetTabHeader(TabPage page, Color color)
{
TabColors[page] = color;
tabControl1.Invalidate();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
//e.DrawBackground();
using (Brush br = new SolidBrush (TabColors[tabControl1.TabPages[e.Index]]))
{
e.Graphics.FillRectangle(br, e.Bounds);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2, e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
Rectangle rect = e.Bounds;
rect.Offset(0, 1);
rect.Inflate(0, -1);
e.Graphics.DrawRectangle(Pens.DarkGray, rect);
e.DrawFocusRectangle();
}
}
For WinForms users reading this - This ONLY works if you set your tab control's DrawMode to OwnerDrawFixed - the DrawItem event never fires if it's set to Normal.
To add to Fun Mun Pieng's answer which works beautifully on Horizontal tabs, if you were to use Vertical tabs (like I was) then you would need something like this:
private void tabControl2_DrawItem(object sender, DrawItemEventArgs e)
{
using (Brush br = new SolidBrush(tabColorDictionary[tabControl2.TabPages[e.Index]]))
{
// Color the Tab Header
e.Graphics.FillRectangle(br, e.Bounds);
// swap our height and width dimensions
var rotatedRectangle = new Rectangle(0, 0, e.Bounds.Height, e.Bounds.Width);
// Rotate
e.Graphics.ResetTransform();
e.Graphics.RotateTransform(-90);
// Translate to move the rectangle to the correct position.
e.Graphics.TranslateTransform(e.Bounds.Left, e.Bounds.Bottom, System.Drawing.Drawing2D.MatrixOrder.Append);
// Format String
var drawFormat = new System.Drawing.StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
// Draw Header Text
e.Graphics.DrawString(tabControl2.TabPages[e.Index].Text, e.Font, Brushes.Black, rotatedRectangle, drawFormat);
}
}
I will echo the point that ROJO1969 made, if this is in WinForms - then you must set DrawMode to OwnerDrawFixed.
Special thanks goes out to this wonderful blog entry which described how to do a rotation of text on a form.
private void MainForm_Load(object sender, EventArgs e)
{
...
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
...
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
// Draw the background of the control for each item.
//e.DrawBackground();
if (e.Index == this.tabControl1.SelectedIndex)
{
Brush _BackBrush = new SolidBrush(tabControl1.TabPages[e.Index].BackColor);
Rectangle rect = e.Bounds;
e.Graphics.FillRectangle(_BackBrush, (rect.X) + 4, rect.Y, (rect.Width) - 4, rect.Height);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black,
e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2,
e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
}
else
{
// 파스톤계 배경색 없앨려면 FromArgb 를 없애면 된다.
Brush _BackBrush = new SolidBrush(Color.FromArgb(50, tabControl1.TabPages[e.Index].BackColor));
Rectangle rect = e.Bounds;
e.Graphics.FillRectangle(_BackBrush, rect.X, (rect.Y)-0, rect.Width, (rect.Height)+6);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black,
e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2,
e.Bounds.Top + 5);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
If any one need to put color for tab header use try this. My tab name tabControl
tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl.DrawItem += tabControl1_DrawItem;
declear this under main class
then,
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Color tabTextColor = Color.FromArgb(0x000001);
var color = Color.FromArgb(tabTextColor.R, tabTextColor.G, tabTextColor.B);
TextRenderer.DrawText(e.Graphics, tabControl.TabPages[e.Index].Text, e.Font, e.Bounds, color);
}
declare this function it will generate output
final out

how can i change font size in thermal printing (lipi lwt 150) using C#.net

I am using C#.net and GDI printing code to print to thermal printer LIPI LWT 150. But, I can't change my font size. I'm using Arial 9pt and 5pt, but it comes out in default size.
Does anybody have any idea about this?
I am using C# code like this:
public frmSale()
{
InitializeComponent();
printingDoc.PrintPage += new PrintPageEventHandler(Form_PrintPage);
}
//initalizing print document
private void PrintSettings()
{
printingDoc.DefaultPageSettings.Margins = new Margins(3, 3, 3, 3);
PaperSize pSize = new PaperSize();
pSize.Width = 275;
printingDoc.DefaultPageSettings.PaperSize = pSize;
// Claculating the PageWidth and the PageHeight
PageHeight = printingDoc.DefaultPageSettings.PaperSize.Height;
PageWidth = printingDoc.DefaultPageSettings.PaperSize.Width;
// Claculating the page margins
LeftMargin = printingDoc.DefaultPageSettings.Margins.Left;
TopMargin = printingDoc.DefaultPageSettings.Margins.Top;
RightMargin = printingDoc.DefaultPageSettings.Margins.Right;
BottomMargin = printingDoc.DefaultPageSettings.Margins.Bottom;
printAreaWidth = PageWidth - RightMargin - LeftMargin;
}
private void Form_PrintPage(object o, PrintPageEventArgs e)
//Here we Begin All the printing Process...
{
PrintSettings();
CurrentY = (float)printingDoc.DefaultPageSettings.Margins.Top;//0;
PrintEstHeader(e.Graphics);
DrawEstGridData(e);
}
//Printing Function
private void PrintEstData(Graphics g, string stringData, StringAlignment alignment, Font fnt, Color clr, bool newLine)//,int starting,int maxWidth)
{
StringFormat stringFormat = new StringFormat();
stringFormat.Trimming = StringTrimming.Word;
stringFormat.FormatFlags = StringFormatFlags.NoWrap |
StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
stringFormat.Alignment = alignment;
RectangleF Rect = new RectangleF((float)LeftMargin, CurrentY,
(float)PageWidth - (float)RightMargin - (float)LeftMargin,
g.MeasureString(stringData, fnt).Height);
g.DrawString(stringData, fnt,
new SolidBrush(clr),
Rect, stringFormat);
CurrentY += newLine ? g.MeasureString(stringData, fnt).Height : 0;
}
private void PrintEstHeader(Graphics g)
{
PrintEstData(g, "----------------------------------------------", StringAlignment.Near, new Font("Arial", 9), Color.Black, true);//,LeftMargin,printAreaWidth);
PrintEstData(g, "Estimate" + " " + "Rate :" + ncRate.Value.ToString("0.00"), StringAlignment.Near, new Font("Arial", 9, FontStyle.Bold), Color.Black, true);//, LeftMargin, 76);
PrintEstData(g, "----------------------------------------------", StringAlignment.Near, new Font("Arial", 9), Color.Black, true);//, LeftMargin, printAreaWidth);
PrintEstData(g,"|ITEM |"+"WEIGHT|"+ "STN WT|"+"M.C. %|"+"Total|", StringAlignment.Near, new Font("Arial", 5), Color.Black, true);//,LeftMargin,42);
PrintEstData(g, "----------------------------------------------", StringAlignment.Near, new Font("Arial", 9), Color.Black, true);//, LeftMargin, printAreaWidth);
}
Based on the printer specification it looks like it does not support arbitrary fonts, but only two inbuilt fixed width fonts. Unless you can print bitmaps to the printer or create user characters I doubt if you will be able to do more than select Font A or Font B.

Categories

Resources