.NET RichText Control

Started by JTaylor, February 10, 2014, 08:50:15 AM

Previous topic - Next topic

JTaylor

I need a simple implementation of a RichText control and assume there is one in .NET?   Was going to use Stan's post displaying a .NET ListView(grid?) as a starting point but can't find it.   Could someone give me a nudge in the right direction.   The new .NET stuff is still a bit arcane to me and usually have trouble getting headed in the right direction.   Thanks.

Jim

Deana

While it is possible create a form using WinBatch's CLR support, the big problem is that WinBatch has no mechanism for supporting delegates and no delegates means no event handling.  Without events you would be hard pressed to respond to user interaction with in your control. 

Here is the forum post that I think you are referring to: http://forum.winbatch.com/index.php?topic=200.msg709#msg709

Sorry I don't currently have a sample code using System.Windows.Forms in WinBatch. If I am able to track anything down I will post it here.
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

Here is a code sample of a very simple messageBox:

Code (winbatch) Select
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrOption - Load assembly into the WinBatch process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectClrOption("use","System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrNew - Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MB = ObjectClrNew('System.Windows.Forms.MessageBox')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Display results
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MB.Show('Text', 'Caption')
Exit

Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor

Events are not an issue.   I have a situation where I need the user to be able to paste formatted text (often from a web page or MSWord) and then simply exit and I save the results.   I have tried numerous options but have common situations where when I return it from the database for display there is nothing.   I think it has something to do with odd characters in the text and have been looking for a way to paste that cleans it up a bit so it works.  Seems to work if I paste to MSWord and save to RTF format and then open that document and paste it in.   Have spent many hours trying many different things/controls over the years but no joy so far.   Open to other suggestions.

Thanks.

Jim

Deana

Here is my first attempt at Windows Forms....

Code (winbatch) Select
;***************************************************************************
;**   Use a Windows .Net Form with a RichTextBox Control
;**
;** Purpose: Accept RichTextBox Input
;** Inputs:
;** Outputs: Results written to output file
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.02.10
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

outfile = 'c:\temp\tmp.rtf'

_True = ObjectType('BOOL',-1)
_False = ObjectType('BOOL',0)
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrOption - Load assembly into the WinBatch process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectClrOption("use","System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
ObjectClrOption( "use", "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrNew - Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1 = ObjectClrNew('System.Windows.Forms.Form')
Button1 =  ObjectClrNew('System.Windows.Forms.Button')
RichTextBox1 =  ObjectClrNew('System.Windows.Forms.RichTextBox')
enumStartPos = ObjectClrNew('System.Windows.Forms.FormStartPosition')
enumDialogResult = ObjectClrNew( 'System.Windows.Forms.DialogResult')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Form1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the caption bar text of the form.   
Form1.Text = "RichTextBox"
; Set the accept button of the form to button1.
Form1.AcceptButton = button1
; Set the start position of the form to the center of the screen.
Form1.StartPosition = ObjectClrType( 'System.Windows.Forms.FormStartPosition', enumStartPos.CenterScreen ) ;CenterScreen

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Button1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the text of button1 to "OK".
button1.Text = "OK"
; Set the position of the button on the form.
button1.Location = ObjectClrNew('System.Drawing.Point',100,100)
; IMPORTANT: The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form.
button1.DialogResult = ObjectClrType( 'System.Windows.Forms.DialogResult', enumDialogResult.OK )   

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define RichTextBox1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;RichTextBox1.LoadFile( 'C:\Program Files\Internet Explorer\en-US\eula.rtf'  )
;RichTextBox1.Visible = _True
RichTextBox1.Text = "hello"

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Add Controls
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1.Controls.Add(Button1)
Form1.Controls.Add(RichTextBox1)

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Display the form as a modal dialog box.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ret = Form1.ShowDialog()

Pause('Contents',RichTextBox1.Text)
;FilePutW(outfile,  RichTextBox1.Text)

Button1.Dispose()
RichTextBox1.Dispose()

Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor

Thank you very much....this gives me a starting point.

Jim

Deana

Been doing a little more work on the code. This more closely fits your needs.
Code (winbatch) Select

;***************************************************************************
;**   Use a Windows .Net Form with a RichTextBox Control
;**
;** Purpose: Accept RichTextBox Input
;** Inputs: 
;** Outputs: Results written to screen and an output file
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.02.10
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

infile = 'C:\Temp\Hello World.rtf'
outfile = DirScript():'Result.rtf'

If FileExist( outfile ) then FileDelete( outfile )

_True = ObjectType( 'BOOL', -1 )
_False = ObjectType( 'BOOL', 0 )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrOption - Load assembly into the WinBatch process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectClrOption( 'use', 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' )
ObjectClrOption( 'use', 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ObjectClrNew - Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1 = ObjectClrNew('System.Windows.Forms.Form')
Button1 =  ObjectClrNew('System.Windows.Forms.Button')
RichTextBox1 =  ObjectClrNew('System.Windows.Forms.RichTextBox')
; Enumerations
enumStartPos = ObjectClrNew('System.Windows.Forms.FormStartPosition')
enumDialogResult = ObjectClrNew( 'System.Windows.Forms.DialogResult')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Form1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the caption bar text of the form.   
Form1.Text = 'RichTextBox'
; Set the start position of the form to the center of the screen.
Form1.StartPosition = ObjectClrType( 'System.Windows.Forms.FormStartPosition', enumStartPos.CenterScreen ) ;CenterScreen
; Set the accept button of the form to button1.
Form1.AcceptButton = button1
; Obtain the Width of the form for later use with controls
formwidth = Form1.Width


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define RichTextBox1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Position  RichTextBox
RichTextBox1.Location = ObjectClrNew('System.Drawing.Point',0,10)
; Define width to width of the form
RichTextBox1.Width = formwidth
; Specify tabindex
RichTextBox1.TabIndex  = 0
; Fill RichTextBox1
RichTextBox1.LoadFile( infile )
; *OR*
;RichTextBox1.Text = 'Hello'

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Define Button1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Set the position of the button on the form.
button1.Location = ObjectClrNew('System.Drawing.Point',100,110)
; Set the text of button1 to 'OK'.
button1.Text = 'OK'
; Specify tabindex
button1.TabIndex  = 1
; IMPORTANT: The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form.
button1.DialogResult = ObjectClrType( 'System.Windows.Forms.DialogResult', enumDialogResult.OK )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Add Controls
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Form1.Controls.Add(Button1)
Form1.Controls.Add(RichTextBox1)

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Display the form as a modal dialog box.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ret = Form1.ShowDialog()

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Get Results
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pause( 'Text Contents of RichTextBox1', RichTextBox1.Text )
Pause( 'RTF Contents of RichTextBox1', RichTextBox1.Rtf )
seltext = RichTextBox1.SelectedText
if seltext != '' then Pause( 'User SelectedText of RichTextBox1', seltext )
;Save the contents of RichTextBox to a file
RichTextBox1.SaveFile(outfile)

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Clean up
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Button1.Dispose()
RichTextBox1.Dispose()
Form1.Dispose()

; Open RTF file in Default Application
If FileExist(outfile) then ShellExecute(outfile, '', '', @NORMAL, '')

Exit


Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor


kdmoyers

Darn it -- I get "1299: dll: DLL not found" on that first ObjectClrOption('use'  line.

I'm on Windows 7, winbatch 2014A.

Anyone know some other magic numbers I can try on that 'use' line?

-Kirby

The mind is everything; What you think, you become.

Deana

Quote from: kdmoyers on February 11, 2014, 01:02:45 PM
Darn it -- I get "1299: dll: DLL not found" on that first ObjectClrOption('use'  line.

I'm on Windows 7, winbatch 2014A.

Anyone know some other magic numbers I can try on that 'use' line?

-Kirby

I am also testing on Windows 7. It appears I have installed a newer .NET Framework. 

On your system take a look at the following directory: C:\Windows\assembly\System.Windows.Forms

Now locate its subdirectory. What is its name? This will give you an idea of what values should be specified in your ObjectClrOption USE statement.

What is it named on your system?
Deana F.
Technical Support
Wilson WindowWare Inc.

td

Try changing the version from Version=4.0.0.0 to Version=3.0.0.0, Version=2.0.0.0, or Version=1.5.0.0.  3.0.0.0 should work on Windows 7.

Also note that as of dotNet 4.0 the GAC is located in %WINDIR%\Microsoft.NET\Assembly instead of %WINDIR%\Assembly.  Older versions of dotNet are still in %WINDIR%\Assembly even if you have installed 4.0 or later.  The down side is the new location does not have the nifty shell extension that allows you to easily view the version, language and pk info.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

(I got it to work with 2.0.0.0 but it failed later with a bad parameter)
(I found and installed dotnet 4 and then the sample code worked as posted.)

This is a very cool sample, but tricky to make good use of. 
Thanks for posting!!!
The mind is everything; What you think, you become.

stanl

Works for me. Great Job Deana!

That being said. As the default form is re-sizeable, does .NET permit re-sizing the individual controls to fit with the resized outline. I say this realizing the limitations of using windows forms in terms of events etc...

Deana

Quote from: stanl on February 12, 2014, 07:55:54 AM
Works for me. Great Job Deana!

That being said. As the default form is re-sizeable, does .NET permit re-sizing the individual controls to fit with the resized outline. I say this realizing the limitations of using windows forms in terms of events etc...

Hopefully the following .NET links answer your question.
http://msdn.microsoft.com/en-us/library/system.windows.forms.autosizemode(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.getautosizemode(v=vs.110).aspx
Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

Quote from: Deana on February 12, 2014, 08:34:17 AM
Hopefully the following .NET links answer your question.
http://msdn.microsoft.com/en-us/library/system.windows.forms.autosizemode(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.getautosizemode(v=vs.110).aspx

They more or less beg the question - how to implement

In your RTF code, you can
Form1.AutoSize = _True

but then trying
button1.AutoSizeMode = Form1.AutoSizeMode.GrowAndShrink

will error.

Deana

It appears AutoSize Property and the GetAutoSizeMode/SetAutosize mode only relate to how the control resizes its self based on its content, not the size of the form. I haven't found a way to dynamically resize all the embedded controls.

I would expect the you would need someway to be notified the form has been resized. Currently Delegates and Event Callbacks are not supported in the WinBatch dotNet implementation. This limits the effectiveness of UI related classes.

If you must have dialog resizing, then your better off sticking with WIL Dialogs.
Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

Quote from: Deana on February 12, 2014, 01:09:54 PM
It appears AutoSize Property and the GetAutoSizeMode/SetAutosize mode only relate to how the control resizes its self based on its content, not the size of the form. I haven't found a way to dynamically resize all the embedded controls.

Understood. So forget the re-sizing. To avoid creating a Windows form that cannot be resized, you do something like set  border = FixedWidth, and create your own form with the forms top, width (and possibly other sizing parameters).

Now, I am just meandering... if the windows form wants pixels, might there be a way to design a dialog in WB and parse that code into pixels so the resultant form looks like the dialog - or create a dialog with a reportview with a couple of buttons, and transfer the sizing to a Windows.Form grid.

Sounds crazy and after posting and re-reading I may have to agree.

stanl

Quote from: stanl on February 13, 2014, 12:04:20 PM
Sounds crazy and after posting and re-reading I may have to agree.

Yeah crazy. I can get the dimensions with VS.