英文:
C# make specific ListBox item text bold and remove some items
问题
I can help with the translation:
如何移除“红色标记”区域并将“蓝色标记”区域在我的列表框中加粗?
我希望在项目MAT和LBL1之间有两个空项目。如何实现?
我尝试过:
不知道怎么做... 但试着将它加粗...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
}
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox lb1 = (ListBox)sender;
var itemText = lb1.Items[e.Index].ToString();
var textBold = FontStyle.Bold;
// 从Client到MAT(或从Client到第一个空项目)使索引加粗
e.DrawBackground();
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), new Font("Arial", 10, FontStyle.Bold), Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
英文:
How do I remove the "red marked" area and make the "blue marked" area bold in my listbox?
And I would like to have two empty items between the item MAT and LBL1. How do I make it possible?
I tryed:
No idea how.. but try to bold it...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
}
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox lb1 = (ListBox)sender;
var itemText = lb1.Items[e.Index].ToString();
var textBold = FontStyle.Bold;
// Make index bold from Client to MAT(or Client to first empty item)
e.DrawBackground();
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), new Font("Arial", 10, FontStyle.Bold), Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
答案1
得分: 1
I actually found a solution. Delete some items and make some bold. It works fine!
void document_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
int leading = 1;
int leftMargin = 25;
int topMargin = 25;
// a few simple formatting options..
StringFormat FmtRight = new StringFormat() { Alignment = StringAlignment.Far };
StringFormat FmtLeft = new StringFormat() { Alignment = StringAlignment.Near };
StringFormat FmtCenter = new StringFormat() { Alignment = StringAlignment.Near };
StringFormat fmt = FmtLeft;
//set bold for the first 6 lines
using (Font font = new Font("Arial", 12f, FontStyle.Bold))
{
SizeF sz = e.Graphics.MeasureString("_|", Font);
float h = sz.Height + leading;
for (int i = 0; i < 6; i++)
e.Graphics.DrawString(listBox9.Items[i].ToString(), font,
Brushes.Black, leftMargin, topMargin + h * i, fmt);
}
//make the rest regular
using (Font font = new Font("Arial", 12f))
{
SizeF sz = e.Graphics.MeasureString("_|", Font);
float h = sz.Height + leading;
for (int i2 = 6; i2 < listBox9.Items.Count; i2++)
e.Graphics.DrawString(listBox9.Items[i2].ToString(), font,
Brushes.Black, leftMargin, topMargin + h * i2, fmt);
}
}
listBox9.BeginUpdate();
string MAT = "MAT:";
string KNR = "KNR";
string SPANNUNG = "SPANNUNG";
//select the item, from where the deletion should start
for (int i = 0; i < listBox9.Items.Count; i++)
{
if (listBox9.Items[i].ToString().Contains(KNR))
{
listBox9.SetSelected(i + 1, true);
}
else if (listBox9.Items[i].ToString().Contains(MAT))
{
listBox9.ClearSelected();
listBox9.SetSelected(i + 1, true);
}
else if (listBox9.Items[i].ToString().Contains(SPANNUNG))
{
listBox9.ClearSelected();
listBox9.SetSelected(i + 1, true);
}
}
//select all items between "SPANNUNG" and "LBL1"
for (int i2 = listBox9.SelectedIndex; i2 < listBox9.Items.Count; i2++)
{
listBox9.SetSelected(i2, true);
if (listBox9.Items[i2 + 1].ToString().Contains("LBL1"))
{
break;
}
}
// delete all selected items between "SPANNUNG" and "LBL1", that I don't need
var selected = listBox9.SelectedItems.Cast<Object>().ToArray();
var selectedIndices = new List<int>(listBox9.SelectedIndices.Cast<int>());
selectedIndices.Reverse();
selectedIndices.ForEach(index => listBox9.Items.RemoveAt(index));
// insert two spaces between "SPANNUNG" and "LBL1"
for (int i3 = 0; i3 < listBox9.Items.Count; i3++)
{
if (listBox9.Items[i3].ToString().Contains("LBL1"))
{
listBox9.SetSelected(i3, true);
listBox9.Items.Insert(listBox9.SelectedIndex, "");
listBox9.Items.Insert(listBox9.SelectedIndex, "");
break;
}
}
listBox9.ClearSelected();
listBox9.EndUpdate();
这是您提供的代码的翻译。
英文:
I actually found a solution. Delete some items and make some bold. It works fine!
> void document_PrintPage(object sender, PrintPageEventArgs e)
> {
> e.Graphics.PageUnit = GraphicsUnit.Millimeter;
> int leading = 1;
> int leftMargin = 25;
> int topMargin = 25;
>
> // a few simple formatting options..
> StringFormat FmtRight = new StringFormat() { Alignment = StringAlignment.Far };
> StringFormat FmtLeft = new StringFormat() { Alignment = StringAlignment.Near };
> StringFormat FmtCenter = new StringFormat() { Alignment = StringAlignment.Near };
>
> StringFormat fmt = FmtLeft;
>
> //set bold for the first 6 lines
> using (Font font = new Font("Arial", 12f, FontStyle.Bold))
> {
> SizeF sz = e.Graphics.MeasureString("|", Font);
> float h = sz.Height + leading;
>
> for (int i = 0; i < 6; i++)
> e.Graphics.DrawString(listBox9.Items[i].ToString(), font,
> Brushes.Black, leftMargin, topMargin + h * i, fmt);
> }
>
> //make the rest regular
> using (Font font = new Font("Arial", 12f))
> {
> SizeF sz = e.Graphics.MeasureString("|", Font);
> float h = sz.Height + leading;
>
> for (int i2 = 6; i2 < listBox9.Items.Count; i2++)
> e.Graphics.DrawString(listBox9.Items[i2].ToString(), font,
> Brushes.Black, leftMargin, topMargin + h * i2, fmt);
> }
> }
>
> listBox9.BeginUpdate();
>
> string MAT = "MAT:";
> string KNR = "KNR";
> string SPANNUNG = "SPANNUNG";
>
> //select the item, from where the deletion should start
> for (int i = 0; i < listBox9.Items.Count; i++)
> {
> if (listBox9.Items[i].ToString().Contains(KNR))
> {
> listBox9.SetSelected(i + 1, true);
> }
> else if (listBox9.Items[i].ToString().Contains(MAT))
> {
> listBox9.ClearSelected();
> listBox9.SetSelected(i + 1, true);
> }
> else if (listBox9.Items[i].ToString().Contains(SPANNUNG))
> {
> listBox9.ClearSelected();
> listBox9.SetSelected(i + 1, true);
> }
> }
>
> //select all items between "SPANNUNG" and "LBL1"
> for (int i2 = listBox9.SelectedIndex; i2 < listBox9.Items.Count; i2++)
> {
> listBox9.SetSelected(i2, true);
> if (listBox9.Items[i2 + 1].ToString().Contains("LBL1"))
> {
> break;
> }
> }
>
> // delete all selected items between "SPANNUNG" and "LBL1", that I don't need
> var selected = listBox9.SelectedItems.Cast<Object>().ToArray();
> var selectedIndices = new List<int>(listBox9.SelectedIndices.Cast<int>());
> selectedIndices.Reverse();
> selectedIndices.ForEach(index => listBox9.Items.RemoveAt(index));
>
> // insert to spaces between "SPANNUNG" and "LBL1"
> for (int i3 = 0; i3 < listBox9.Items.Count; i3++)
> {
> if (listBox9.Items[i3].ToString().Contains("LBL1"))
> {
> listBox9.SetSelected(i3, true);
> listBox9.Items.Insert(listBox9.SelectedIndex, "");
> listBox9.Items.Insert(listBox9.SelectedIndex, "");
> break;
> }
> }
>
> listBox9.ClearSelected();
> listBox9.EndUpdate();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论