I am trying to figure out a way to use the StringFormat class to add new line to a given TreeNode text. For example, the string ID1:123456, ID2:789000, can be split by comma and insert a newline in between, so it has two lines.
I understand there is no need to make a TreeNode text into multiple lines, but this is required by my supervisor and I have no choice.
My current solution is to override a DrawNode function, and use the DrawString function to customize TreeNode text format, but my question is I don't know how to insert newline at this stage.
private void TreeView_SO_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.DrawDefault = false;
string drawString = e.Node.Text;
Font drawFont = ((TreeView)sender).Font;
SolidBrush drawBrush = new SolidBrush(Color.Black);
StringFormat drawFormat = new StringFormat();
// what to put in here to insert a new line?
e.Graphics.DrawString(drawString, drawFont, drawBrush, e.Node.Bounds, drawFormat);
}
Update
Thank you for everyone's help. I ended up finding this is quite easy, thanks to EskeRahn's codes posted on: WINDOWS TREEVIEW WITH E.G. MULTI-LINE TREE CONTENT. This code is quite useful; however, I only need part of EskeRnhn's code to perform my ideal operation. So my code ended up like this,
private void TreeView_SO_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.DrawDefault = false;
string drawString = e.Node.Text;
Font drawFont = ((TreeView)sender).Font;
SolidBrush drawBrush = new SolidBrush(Color.Black);
Rectangle eNodeBounds = NodeBounds(e.Node);
e.Graphics.DrawString(drawString, drawFont, drawBrush, eNodeBounds);
}
private Rectangle NodeBounds(TreeNode node)
{
if (node?.TreeView != null && node?.Text != null && (0 < node.Bounds.Location.X || 0 < node.Bounds.Location.Y))
{
using (Graphics g = node.TreeView.CreateGraphics())
{
SizeF textSize = g.MeasureString(node.Text, node.NodeFont ?? node.TreeView.Font);
return Rectangle.Ceiling(new RectangleF(PointF.Add(node.Bounds.Location,
new SizeF(0, (node.TreeView.ItemHeight - textSize.Height) / 2)),
textSize));
}
}
else
{
return node?.Bounds ?? new Rectangle();
}
}
, where I don't need to worry about the the StringFormat class at all, because I already override the a DrawNode function, so when I pass the string to TreeNode text, I just need to add newline symbols. I have tested multiple cases and this piece of code works well.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)