Ask Your Question
1

How to change color or font for some part of the text in the msgbox?

asked 2017-10-15 03:06:30 +0200

shma_lo gravatar image

There is message box in my calc macro msgbox(sometext, 48, someothertext) I want to highlight some part of (sometext) with color or with bold type. For example, I want in msgbox("My name is John Roberts", 48, "Family) "John" to be bold and "Roberts" to be red color. How can I do it?

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
3

answered 2018-01-08 15:34:26 +0200

librebel gravatar image

Hello @shma_lo,

To display a MessageBox with formatted text you could use the Xmessagebox interface, plus an injected RichText control to replace the normal plain text control.

When calling the below provided function MessageBox() in the following manner:

Dim strMsg As String : strMsg = "My name is " +chr(2)+ "John " +chr(2)+ "Roberts"
Dim aProps1(0) As New com.sun.star.beans.PropertyValue : aProps1(0).Name = "CharWeight" : aProps1(0).Value = 150.0
Dim aProps2(0) As New com.sun.star.beans.PropertyValue : aProps2(0).Name = "CharColor"  : aProps2(0).Value = rgb( 255, 10, 10 )
Dim aStyles() : aStyles = Array( Array(), aProps1, aProps2 )
MessageBox( strMsg, 2, "Family" , 1, aStyles, 170, 40 )

it produces the following dialog on my system Ubuntu 17.10:

Screenshot formatted MessageBox.png

To use the MessageBox() function, please copy-paste the entire code section below ( including the “empty” callback methods at the end ), into your Macro Library.

Global g_MessageBox As Object
Function MessageBox( strText$, Optional iMessageBoxType%, Optional strDialogTitle, Optional lMessageBoxButtons, Optional aTextPortionStyleProperties(), Optional lTextFieldWidth, Optional lTextFieldHeight, Optional rTextFieldFont, Optional lDialogPositionX, Optional lDialogPositionY, Optional lDialogBackgroundColor, Optional aButtonLabels(), Optional rButtonFont, Optional dButtonZoom, Optional iButtonPointer%, Optional oParentWindow As Object )
REM Display a customized XMessageBox with formatted text.
REM  <strText>      : The Message text to display.
REM                   TextPortions with different formatting can be separated using a Chr(2) character; each TextPortion can have its own Style Properties specified in the <aTextPortionStyleProperties> array.
REM                   TextPortions without a corresponding Style Properties array will be displayed in the default Font (<rTextFieldFont>).
REM                   NB. Style properties are reset after each TextPortion, and also after each paragraph break chr(13) and/or newline chr(10).
REM                   So if you want to include a Chr(13) or Chr(10) character inside your TextPortion, and there are more TextPortions following,
REM                   then that Chr(13) or Chr(10) character should be directly followed by a Chr(2) character, and a new StyleProperties Array should be added for it into the <aTextPortionStyleProperties> array.
REM  [OPTIONAL] <iMessageBoxType>   : Integer indicating which type of MessageBox to show; [DEFAULT]=0:
REM         0 = com.sun.star.awt.MessageBoxType.MESSAGEBOX      normal message box ( without icon ).
REM         1 = com.sun.star.awt.MessageBoxType.INFOBOX         message box to inform the user about a certain event.
REM         2 = com.sun.star.awt.MessageBoxType.WARNINGBOX      message box to warn the user about a certain problem.
REM         3 = com.sun.star.awt.MessageBoxType.ERRORBOX        message box to provide an error message to the user.
REM         4 = com.sun.star.awt.MessageBoxType.QUERYBOX        message box to query information from the user.
REM        NB. In the case that iMessageBoxType=INFOBOX, only the OK button will be shown, regardless of the specified <lMessageBoxButtons> value.
REM  [OPTIONAL] <strDialogTitle>    : String to appear in the MessageBox dialog titlebar; [DEFAULT]= Product Version String.
REM                                   Currently yields Error 448 if the Title string is wider than the Dialog Width.
REM  [OPTIONAL] <lMessageBoxButtons>: Long integer combination of com.sun.star.awt.MessageBoxButtons, indicating which buttons are to be shown; [DEFAULT]=65537:
REM         1=BUTTONS_OK;   2=BUTTONS_OK_CANCEL;   3=BUTTONS_YES_NO;   4=BUTTONS_YES_NO_CANCEL;   5=BUTTONS_RETRY_CANCEL;   6=BUTTONS_ABORT_IGNORE_RETRY;
REM         + one of : 65536 ...
(more)
edit flag offensive delete link more

Comments

On my Windows 10 system (LO 5.4.3.2), the size of rich text ("John" and "Roberts") is about twice as large as the other text. The positioning of the text is correct, however.

Jim K gravatar imageJim K ( 2018-01-08 20:30:05 +0200 )edit

Thanks for the feedback @Jim K, unfortunately i have no clue how this difference might be caused, and i lack the means to test it on Windows. As an immediate patch i would suggest adding a CharHeight property with Value=12 to both the aProps1() and aProps2() arrays.

librebel gravatar imagelibrebel ( 2018-01-08 20:38:55 +0200 )edit

@librebel Thanks for this code. Had same problem as @Jim K with height of characters on Linux Mint. Solution was your patch with Value = 11 (both 1 & 2).

Ratslinger gravatar imageRatslinger ( 2018-03-20 03:16:02 +0200 )edit

YW @Ratslinger, i should really look into that matter of the font sizes...

Without the aforementioned patch, does the problem still occur when you provide a FontDescriptor structure (filled) as the 8th argument to the Messagebox function?

librebel gravatar imagelibrebel ( 2018-03-20 04:41:24 +0200 )edit

Sorry, not my area. Not sure how to set. Will try to figure it out & test.

Ratslinger gravatar imageRatslinger ( 2018-03-20 05:17:31 +0200 )edit

yes something is going wrong there with the FontDescriptor... will look into it shortly, thanks for the feedback.

e.g.

Dim rFD as New com.sun.star.awt.FontDescriptor
rFD.Name = "Comic Sans MS"
rFD.Height = 12
librebel gravatar imagelibrebel ( 2018-03-20 05:43:09 +0200 )edit

@librebel Using descriptors from system (tried a few fonts like Ubuntu), the text is all the same height (without the patch) but now the "My name is" portion is in Bold & italics. Will play some more with it tomorrow.

Ratslinger gravatar imageRatslinger ( 2018-03-20 05:47:47 +0200 )edit

Running with:

Dim rFD as New com.sun.star.awt.FontDescriptor
rFD.Name = "Liberation Serif"
rFD.Height = 12

and no patches, the result looks good. Playing with the height, it only affects the "My name is" portion - this is using your sample at beginning of answer.

Ratslinger gravatar imageRatslinger ( 2018-03-20 05:57:38 +0200 )edit
0

answered 2017-10-15 09:08:48 +0200

Jim K gravatar image

Instead of msgbox, create a custom dialog. Use three label controls, one for each different type of formatting.

custom dialog

edit flag offensive delete link more

Comments

Thank you, but I don't want to use dialog for some reason

shma_lo gravatar imageshma_lo ( 2017-10-15 15:22:42 +0200 )edit
0

answered 2017-10-15 04:15:15 +0200

Ratslinger gravatar image

There is no way to do that using msgbox.

edit flag offensive delete link more

Comments

Thank you. I'm very sorry

shma_lo gravatar imageshma_lo ( 2017-10-15 15:23:40 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-10-15 03:06:30 +0200

Seen: 618 times

Last updated: Jan 08 '18