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.
Unfortunately, activation email could not send to your email. Please try again.

Textnode adjust size to text

Thread ID:

Created:

Updated:

Platform:

Replies:

128400 Jan 19,2017 04:28 AM Jan 25,2017 11:01 PM Windows Forms 7
loading
Tags: Diagram
Blanca Calderon
Asked On January 19, 2017 04:28 AM

Hi,

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

Naganathan Ganesh Babu [Syncfusion]
Replied On January 20, 2017 02:17 AM

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 


Blanca Calderon
Replied On January 20, 2017 03:52 AM

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

Naganathan Ganesh Babu [Syncfusion]
Replied On January 20, 2017 05:08 AM

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 


Blanca Calderon
Replied On January 20, 2017 06:27 AM

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();




Naganathan Ganesh Babu [Syncfusion]
Replied On January 23, 2017 01:43 AM

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 


Blanca Calderon
Replied On January 25, 2017 05:57 AM

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



Kameshwaran R [Syncfusion]
Replied On January 25, 2017 11:01 PM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;