We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Textnode adjust size to text

Hi,

I would like, when I introduce a text, to adjust the size of the textnode to the size of the text inside. 

7 Replies

NG Naganathan Ganesh Babu Syncfusion Team January 20, 2017 07:17 AM UTC

Hi Blanca, 
 
Thanks for contacting Syncfusion support. 
 
We have created a sample to achieve your requirement. we suggest you to use Diagram.Controller’s “NodeDoubleClick” event and TextNode’s “SizeToText()” method to resize the TextNode based on the given size of the text. Please refer to the below code example and sample. 
 
Code example: 
 
[C#] 
 
diagram1.EventSink.NodeDoubleClick += EventSink_NodeDoubleClick; 
 
void EventSink_NodeDoubleClick(NodeMouseEventArgs evtArgs) 
{ 
Node node = evtArgs.Node; 
if (node is TextNode) 
{ 
Form2 Diaglog = new Form2(); 
Diaglog.NodeText = ((TextNode)node).Text; 
 ((TextNode)node).Text = ""; 
if (Diaglog.ShowDialog(this) == DialogResult.OK) 
{ 
TextNode tn = node as TextNode; 
diagram1.Controller.TextEditor.EndEdit(false); 
tn.Text = Diaglog.NodeText; 
using (Graphics gp = Graphics.FromHwnd(IntPtr.Zero)) 
{ 
SizeF size = gp.MeasureString(tn.Text, tn.FontStyle.CreateFont()); 
tn.SizeToText(size); 
} 
} 
} 
} 
 
Sample: 
 
 
Regards, 
 
Naganathan K G 



BC Blanca Calderon January 20, 2017 08:52 AM UTC

As always I couldn't open the simple.

Anyway I've inserted this part in my code but it doesn't work.

using (Graphics gp = Graphics.FromHwnd(IntPtr.Zero)) 
{ 
SizeF size = gp.MeasureString(tn.Text, tn.FontStyle.CreateFont()); 
tn.SizeToText(size); 
} 

Attachment: Video_1484905803_c5968b2a.zip


NG Naganathan Ganesh Babu Syncfusion Team January 20, 2017 10:08 AM UTC

Hi Blanca, 
 
Sorry for the inconvenience caused. 
 
As always I couldn't open the simple. 
Please find the modified sample. 
 
Sample: 
 
 
Anyway I've inserted this part in my code but it doesn't work. 
 
using (Graphics gp = Graphics.FromHwnd(IntPtr.Zero))  
{  
SizeF size = gp.MeasureString(tn.Text, tn.FontStyle.CreateFont());  
tn.SizeToText(size);  
}  
 
While using dialog box to edit the text in the TextNode the default functionality of Diagram’s TextNode editing also enabled. So, the length of the text is taken from the older value (or DefaultSize of the TextNode) which is already present in the label and the new text length is not taken which is the reason for the reported problem. So, we suggest you to invoke Diagram.Controller.TextEditor’s “EndEdit()” by adding false as an argument will resolve the reported issue.  
 
Code example: 
 
[C#] 
                    diagram1.Controller.TextEditor.EndEdit(false); 
 
using (Graphics gp = Graphics.FromHwnd(IntPtr.Zero)) 
{ 
SizeF size = gp.MeasureString(tn.Text, tn.FontStyle.CreateFont()); 
tn.SizeToText(size); 
} 
 
 
Regards, 
 
Naganathan K G 



BC Blanca Calderon January 20, 2017 11:27 AM UTC

Ok, this works but I have another problem.

When I change the textnode.text I want to invoke Diagram.Controller.TextEditor’s “EndEdit()” = true (when close the form that I use to modify my textnode for example), but it doesn't work.

TextNode tn = ((TextNode)nodeText) as TextNode;

diagram1.Controller.TextEditor.EndEdit(false);//BC 20-01-2017 Para que ajuste el tamaño del contador al texto que corresponda

if (txtDigits.Text != "" && txtDigits.Text != "0" && txtDecimals.Text != "" && txtUnits.Text != "")

{

if (txtDecimals.Text == "0")

{

tn.Text = "*-" + ec.calcularCeros(Int32.Parse(ec.cambiarEspacio_Ceros(txtDigits.Text))) + " " + txtUnits.Text;

}

else

{

tn.Text = "*-" + ec.calcularCeros(Int32.Parse(ec.cambiarEspacio_Ceros(txtDigits.Text))) + "." + ec.calcularCeros(Int32.Parse(ec.cambiarEspacio_Ceros(txtDecimals.Text))) + " " + txtUnits.Text;

}

}

else

{

if (txtDigits.Text != "" && txtDigits.Text != "0" && txtDecimals.Text != "")

{

if (txtDecimals.Text == "0")

{

tn.Text = "*-" + ec.calcularCeros(Int32.Parse(ec.cambiarEspacio_Ceros(txtDigits.Text)));

}

else

{

tn.Text = "*-" + ec.calcularCeros(Int32.Parse(ec.cambiarEspacio_Ceros(txtDigits.Text))) + "." + ec.calcularCeros(Int32.Parse(ec.cambiarEspacio_Ceros(txtDecimals.Text)));

}

}

}

//BC 20-01-2017 Para que ajuste el tamaño del contador al texto que corresponda

using (Graphics gp = Graphics.FromHwnd(IntPtr.Zero))

{

SizeF size = gp.MeasureString(tn.Text, tn.FontStyle.CreateFont());

tn.SizeToText(size);

}

//[blanca][20-01-2017] Que los contadores no sean editables

diagram1.Controller.TextEditor.EndEdit(true);

this.Close();





NG Naganathan Ganesh Babu Syncfusion Team January 23, 2017 06:43 AM UTC

Hi Blanca, 
  
 
When I change the textnode.text I want to invoke Diagram.Controller.TextEditor’s “EndEdit()” = true (when close the form that I use to modify my textnode for example), but it doesn't work. 
 
As we have updated previously, the default functionality of Diagram’s TextNode Editing is enabled and if we set “EndEdit(true)” then the default text in TextNode is taken instead of assigning the text from the newly created form. So, we suggest you not to invoke Diagram.Controller.TextEditor’s “EndEdit(True)” method and we suspect invoking of this method is the problem for the reported issue.    
 
 
  
Regards, 
 
Naganathan K G 



BC Blanca Calderon January 25, 2017 10:57 AM UTC

Ok, I think that I've found the solution, thank you.




KR Kameshwaran R Syncfusion Team January 26, 2017 04:01 AM UTC

Hi Blanca, 
 
Thanks for the update and Please get in touch with us if you would require any further assistance.  
  
Regards,  
                  
Kameshwaran R.  
 


Loader.
Up arrow icon