in Education by
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)

1 Answer

0 votes
by
You can split the text by comma, and then join using new line. var splitText = e.Node.Text.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries); var newString = string.Join(System.Environment.NewLine, splitText);

Related questions

0 votes
    So I have a for loop that I want to output a new line in a textview every single time it goes ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    Having a problem getting a TreeView control to display node images. The code below works sometimes but fails ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I want to add multiple columns in UITableView. I have created one CustomeTableCell with two lines used - ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I want to add multiple columns in UITableView. I have created one CustomeTableCell with two lines used - ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    How can you draw straight horizontal and vertical lines with the help of the line tool ? Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    10.The………tool lets you draw straight lines and curves. Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    I'm trying to change the color of text lines containing "Accepted" to green and all others to red ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    It took me forever to reduce the problem to this. I cannot express the amount of frustration I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    The Problem is simple, The below shown is an input text box. i've set the value of the input box ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    The Problem is simple, The below shown is an input text box. i've set the value of the input box ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    The standard font determines the width of the text lines in the __________ (a) Function (b) Package (c) ... Networks of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which button is used to apply formatted text effects to multiple lines? Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    first..sorry for my poor english and my big noob question..the problem is... I need create a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    A bag contains 6 red and 9 blue balls. Two successive drawing of four balls are made such that the balls are not ... (d) None of these Select the correct answer from above options...
asked Nov 19, 2021 in Education by JackTerrance
...