Dialog: How to format Text in TextField like in SpellCheck dialog?

There is example of formatted text in Spelling Dialog → red text and normal text
formatted-text

But how to format Text in Text Field in Dialog? There is method setAttributes, but without success :-(.

Sub Start
	on local error goto chyba
	dim oDoc as object, oDlg as object, oDlgModel as object,oWindow as object, oToolkit as object
	oDoc=StarDesktop.LoadComponentFromUrl("private:factory/swriter", "_blank", 0, array()) 'new Writer window
	oDlg=CreateUnoService("com.sun.star.awt.UnoControlDialog") 'Main Dialog
	oDlgModel=CreateUnoService("com.sun.star.awt.UnoControlDialogModel")
	with oDlgModel
		.Width=100
		.Height=100
		.PositionX=50
		.PositionY=50
	end with	
	oDlg.setModel(oDlgModel)
	oWindow=CreateUnoService("com.sun.star.awt.Toolkit")
	oToolkit=oDoc.CurrentController.Frame.ContainerWindow
	oDlg.createPeer(oWindow, oToolkit)
	oDlg.addTopWindowListener(CreateUnoListener("Ldlg_", "com.sun.star.awt.XTopWindowListener")) 'listener for closing cross

	dim oEdit as object, sName$, o as object, oAccess as object 'Text Field
	oEdit=oDlg.Model.createInstance("com.sun.star.awt.UnoControlEditModel")
	sName="TextField1"
	with oEdit
		.Name=sName
		.Width=80
		.Height=80
		.PositionX=10
		.PositionY=10
		.Text="hello world"
	end with
	oDlg.Model.insertByName(sName, oEdit)
	o=oDlg.getControl(sName)
	o.Editable=true 'enable the methods from "com.sun.star.accessibility.XAccessibleEditableText"
	
	oAccess=o.AccessibleContext
	dim bb(12) as new com.sun.star.beans.PropertyValue 
	'xray oo.getCharacterAttributes(1, array()) 'get array of properties
		bb(0).Name="CharBackColor" : bb(0).Value=RGB(0,0,255)
		bb(1).Name="CharColor" : bb(1).Value=RGB(255,0,0)
		bb(2).Name="CharFontCharSet" : bb(2).Value=0
		bb(3).Name="CharFontFamily" : bb(3).Value=0
		bb(4).Name="CharFontName" : bb(4).Value=""
		bb(5).Name="CharFontPitch" : bb(5).Value=0
		bb(6).Name="CharFontStyleName" : bb(6).Value=""
		bb(7).Name="CharHeight" : bb(7).Value=0
		bb(8).Name="CharPosture" : bb(8).Value=0
		bb(9).Name="CharScaleWidth" : bb(9).Value=0
		bb(10).Name="CharStrikeout" : bb(10).Value=0
		bb(11).Name="CharUnderline" : bb(11).Value=0
		bb(12).Name="CharWeight" : bb(12).Value=150

	msgbox oAccess.setAttributes(1, 2, bb) 'WHY NOT FUNCTIONAL?
	oAccess.replaceText(3, 4, "12") 'but other method from "com.sun.star.accessibility.XAccessibleEditableText" is functional
	exit sub
chyba:
	bug(Err,Erl,Error,"Start")
End Sub

Sub bug(sErr$, sErl$, sError$, s$)
	msgbox("Error: " & sErr & chr(13) & sError & chr(13) & "Řádek: " & sErl & chr(13), 16, s)
	stop
End Sub

Sub Ldlg_windowClosing(optional oEvt as object)
	on local error goto bug
	oEvt.Source.dispose()
	exit sub
bug:
	bug(Err, Erl, Error, "Ldlg_windowClosing")
End Sub
Sub Ldlg_disposing
End Sub
Sub Ldlg_windowOpened
End Sub
Sub Ldlg_windowClosed
End Sub
Sub Ldlg_windowMinimized
End Sub
Sub Ldlg_windowNormalized
End Sub
Sub Ldlg_windowActivated
End Sub
Sub Ldlg_windowDeactivated
End Sub

Hello,
Although an indirect solution, see this post where something similar was performed with a Message Box → How to change color or font for some part of the text in the msgbox? - #6 by librebel

1 Like

You can change the control type to RichTextControl in your example:

Sub Start
	on local error goto chyba
	dim oDoc as object, oDlg as object, oDlgModel as object,oWindow as object, oToolkit as object
	oDoc=StarDesktop.LoadComponentFromUrl("private:factory/swriter", "_blank", 0, array()) 'new Writer window
	oDlg=CreateUnoService("com.sun.star.awt.UnoControlDialog") 'Main Dialog
	oDlgModel=CreateUnoService("com.sun.star.awt.UnoControlDialogModel")
	with oDlgModel
		.Width=100
		.Height=100
		.PositionX=50
		.PositionY=50
	end with	
	oDlg.setModel(oDlgModel)
	oWindow=CreateUnoService("com.sun.star.awt.Toolkit")
	oToolkit=oDoc.CurrentController.Frame.ContainerWindow
	oDlg.createPeer(oWindow, oToolkit)
	oDlg.addTopWindowListener(CreateUnoListener("Ldlg_", "com.sun.star.awt.XTopWindowListener")) 'listener for closing cross

	dim oEdit as object, sName$, o as object, oAccess as object 'Text Field
	oEdit=oDlg.Model.createInstance("com.sun.star.awt.UnoControlEditModel")
	sName="RichControl"
	oEdit=CreateUnoService("com.sun.star.form.component.RichTextControl")
	with oEdit
		.Name=sName
	    .RichText=True	
	end with
	
	oDlg.Model.insertByName(sName, oEdit)
	o=oDlg.getControl(sName)
	o.SetPosSize 5, 5, 100, 30,15
	o.Editable=true 'enable the methods from "com.sun.star.accessibility.XAccessibleEditableText"
    Dim bb(0) as new com.sun.star.beans.PropertyValue 
	bb(0).Name="CharColor" : bb(0).Value=RGB(255,0,0)	
	oEdit.appendTextPortion("Hello, ", bb)
	oEdit.appendTextPortion("World ", Array())
	Exit Sub
chyba:
	bug(Err,Erl,Error,"Start")
End Sub

Sub bug(sErr$, sErl$, sError$, s$)
	msgbox("Error: " & sErr & chr(13) & sError & chr(13) & "Řádek: " & sErl & chr(13), 16, s)
	stop
End Sub

Sub Ldlg_windowClosing(optional oEvt as object)
	on local error goto bug
	oEvt.Source.dispose()
	exit sub
bug:
	bug(Err, Erl, Error, "Ldlg_windowClosing")
End Sub
Sub Ldlg_disposing
End Sub
Sub Ldlg_windowOpened
End Sub
Sub Ldlg_windowClosed
End Sub
Sub Ldlg_windowMinimized
End Sub
Sub Ldlg_windowNormalized
End Sub
Sub Ldlg_windowActivated
End Sub
Sub Ldlg_windowDeactivated
End Sub
1 Like

I saw the “com.sun.star.form.component.RichTextControl” in @Ratslinger link today morning, but I hadn’t a time to explore it in detail at morning. But I hope it will be easy and it is :-). Thanks a lot.

1 Like