How to set 100% table`s width for all new tables by default?

1 Answer 122 Views
RichTextBox
Emin Sadigov
Top achievements
Rank 1
Iron
Veteran
Iron
Emin Sadigov asked on 06 Aug 2021, 02:54 PM

Hello,

Is any way to set default width for new tables, which are creating via RichTextBox UI?

I tried to set OnLoad the following, but unsuccessfully:

Editor.Document.Style.TableProperties.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);

StyleDefinition normalWebStyle = Editor.Document.StyleRepository.GetValueOrNull(RadDocumentDefaultStyles.NormalWebStyleName, true);

normalWebStyle.TableProperties.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);

1 Answer, 1 is accepted

Sort by
0
Accepted
Tanya
Telerik team
answered on 09 Aug 2021, 09:55 AM

Hello Emin,

You can apply the desired change through the CommandExecuted event. When the InsertTableCommand is executed, find the new table and set the desired width to it:

private void RadRichTextBox_CommandExecuted(object sender, CommandExecutedEventArgs e)
{
    if (e.Command is InsertTableCommand)
    {
        Table table = this.radRichTextBox.Document.CaretPosition.GetCurrentTable();
        if (table != null)
        {
            table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
        }
    }
}

Hope this helps.

Regards,
Tanya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Emin Sadigov
Top achievements
Rank 1
Iron
Veteran
Iron
commented on 09 Aug 2021, 11:31 AM

Hello Tanya,

Thank you for this example. I haven`t found GetCurrentTable(); in the 2016 version, may be it released later?

I`ve used the following code (Also used for ShowInsertTableDialogCommand):

private void Editor_CommandExecuted(object sender, CommandExecutedEventArgs e)
{
	if (e.Command is InsertTableCommand || e.Command is ShowInsertTableDialogCommand)
	{
		Table table = Editor.Document.CaretPosition.GetCurrentTableBox()?.AssociatedTable;
		if (table != null)
		{
			table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
		}
	}
}

Just one issue, not critical for me. If CaretPosition already was in the Table, and ShowInsertTableDialogCommand return Cancel, existing table width will be changed.

Tanya
Telerik team
commented on 10 Aug 2021, 10:39 AM

Hello Emin,

Indeed, the GetCurrentTable() method has been introduced in a later version. However, it internally performs the same logic as the one you are currently using.

I can also confirm that such a side effect is present. The event is fired no matter whether a table is actually inserted, thus I can only suggest tracking the number of tables currently inside the document. If this number is increased, a new table has been inserted and you will need to adjust its size.

Hope this is helpful.

Tags
RichTextBox
Asked by
Emin Sadigov
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
Tanya
Telerik team
Share this question
or