Message Boards Message Boards

2
|
3870 Views
|
5 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Highlighting selected text in MessageDialog

I'm trying to do something that seems like it ought to be easy, but I've found no (obvious) simple solution. As a program is running, interim results and comments are added to a string (call it StringCum), that - at the discretion of the user - may be shown via a call to MessageDialog[StringCum,...]. So far, so good.

What I'd like to do, however, is periodically highlight certain text. For example, if a special event, Event1, occurs, I'd like that portion of the cumulative string to be highlighted (say, as FontColor->Red). The command, MessageDialog[ StringCum <> Style["Event1 has occurred", FontColor -> Red]] highlights the text but (1) print "<>" as though it was itself a string, and (2) does not alloow me to concatenate any further strings at some later point; i.e., I'd like to define StringCum =StringCum <>Style["Event1 has occurred", FontColor -> Red]], I get an error. And if I try defining a string: StringCum =StringCum <>ToString[Style["Event1 has occurred", FontColor -> Red]]], everything works except that the color highlighting disappears.

In short, I'd like to highlight selected parts of a string that is being displayed via MessageDialog,

Anything anyone can suggest?

Thanks! (first time posting here...I've done a search on what I thought were similar issues, but could not find a match).

POSTED BY: andy ilachinski
5 Replies

There is an alternative way of joining formatted strings:

txt = StringTake[ExampleData[{"Text", "ToBeOrNotToBe"}, "FormattedText"], 200];

StringJoin[
 ToString[Style[#, RandomInteger[{11, 23}], RandomColor[]], 
    StandardForm] & /@ StringSplit[txt, " " -> " "]]

enter image description here

POSTED BY: Vitaliy Kaurov

But, since message is now a Row[...], the concatenation can't go through since it's no longer a string

You can try Join, like this, to see if it works for you

nextMessage = Style["the oven is getting hot!", Red];
message = Row[{"Would like to say that ", importantMsg, " and needs cooling"}];
MessageDialog[message];

and now

nextMessage = {"..please hurry"};
message = Row[Join[First@message, nextMessage]];
MessageDialog[message];

and now

nextMessage = {Style["..I am still waiting..", Red]};
message = Row[Join[First@message, nextMessage]];
MessageDialog[message];

enter image description here

POSTED BY: Nasser M. Abbasi

May be you can use Styled strings? Need to use Row to build styled strings. Something like

enter image description here

importantMsg = Style["the oven is getting hot!", Red];
message = Row[{"Would like to say that ", importantMsg, " and needs cooling"}];
MessageDialog[message]
POSTED BY: Nasser M. Abbasi

Nasser, thanks for the quick reply!

That's already a help, but with still this lingering issue...as the main program keeps running, it keeps track of a cumulative message string (continually updated): i.e., if the current message (to be printed via MessageDialog) is your message = Row[{"Would like to say that ", importantMsg, " and needs cooling"}];, I still need - at some later point - to have something like message <> "here is another string (possibly with its own highlighting)" to be available to MessageDialog. But, since message is now a Row[...], the concatenation can't go through since it's no longer a string.

Hope that makes sense, and my problem is clear. Essentially I need what your great suggestion does for this one message, but I need to be able to keep concatenating strings to this (some of which may have highlights, some not).

POSTED BY: andy ilachinski

Nasser, that did it! Thank you very much for the quick answers and follow-up. Much appreciated :-)

POSTED BY: andy ilachinski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract