Does anyone know of a working example for putting the WPF syntax editor in a winform? I've searched all over, and even chatGPT, but it can't provide a working example.
Hello Sean,
Thank you for contacting Infragistics. This is documented on MSDN
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-a-wpf-composite-control-in-windows-forms?view=netframeworkdesktop-4.8
After you've added the assemblies for the XamSyntaxEditor, adding the component to a Windows Forms project is fairly straightforward.
eg,
public partial class Form1 : Form { private ElementHost _ctrlHost; public XamSyntaxEditor syntaxEditor; public TextDocument document; public Form1() { InitializeComponent(); _ctrlHost = new ElementHost(); syntaxEditor = new XamSyntaxEditor(); _ctrlHost.Child = this.syntaxEditor; document = new Infragistics.Documents.TextDocument(); document.InitializeText("Hello, World!"); syntaxEditor.Document = document; _ctrlHost.Width = 800; _ctrlHost.Height = 450; this.Controls.Add(_ctrlHost); }
Let me know if you have any questions.
Thanks, that did the trick. One more thing... I'm trying to use it as a sql parser and I'm using the code on this page...
Supported Languages - Infragistics WPF Help
syntaxEditor.Document.Language = TSqlLanguage.Instance;
But it tells me...
Cannot resolve symbol 'TSqlLanguage'
I'd also like to show line numbers.
Hello and thank you for following up.
All you need is this line of code and the TSQL assembly
WindowsFormsApp13.zip
Alright, well I thought it was a step closer, but there seems to be this loop I can't get out of. I added that assembly and the error's changed.
I have this line...SyntaxEditor.Document.Language = TSqlLanguage.Instance;
And I'm getting this... FormMain.cs(73, 43): [CS0012] The type 'LanguageBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'Infragistics4.Documents.TextDocument.v23.1, Version=23.1.20231.52, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb'.
OK, so I added that reference. Now I get this error:FormMain.cs(73, 43): [CS0029] Cannot implicitly convert type 'Infragistics.Documents.Parsing.TSqlLanguage' to 'Infragistics.Documents.Parsing.LanguageBase'
On top of that, I get these additional errors:FormMain.cs(24, 12): [CS0433] The type 'TextDocument' exists in both 'Infragistics4.Documents.TextDocument.v23.1, Version=23.1.20231.52, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb' and 'InfragisticsWPF.Documents.TextDocument, Version=23.1.20231.41, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb'
and... (lines 52 and 53)Cannot convert source type 'TextDocument' to target type 'Infragistics.Documents.TextDocument'
I've attached my class file and a screenshot of my references.
using System; using System.Drawing; using System.Reflection; using System.Resources; using System.Windows.Forms; using System.Windows.Forms.Integration; using Core.License; using Core.License.Interfaces; using Infragistics.Controls.Editors; using Infragistics.Documents; using Infragistics.Documents.Parsing; using QB; using SQLHelpers; using UserControls; using WindowsTools; namespace ConnParameterQB; public partial class FormMain : Form { private ElementHost _ctrlHost; public XamSyntaxEditor SyntaxEditor; public TextDocument Document; private ISQLConn _sqlConn = new SQLConn(); private ISQLQueries _query = new SQLQueries(); private ResourceManager _rm; private bool _saveToFile = true; private bool _saveToClipboard = true; private Bitmap _picSaveToFile; private Bitmap _picSaveToClipboard; public FormMain() { InitializeComponent(); CreateParsingDoc(); SetClipboardIcons(); } private void CreateParsingDoc() { //The ElementHost is needed to host a WPF control in a winform. _ctrlHost = new ElementHost(); ////////////////////BEGIN Add Editor///////////////// //Create the syntaxEditor and add it to the host. SyntaxEditor = new XamSyntaxEditor(); _ctrlHost.Child = this.SyntaxEditor; ////////////////////END Add Editor/////////////////// ////////////////////BEGIN Add Document///////////////// //The editor needs a doc inside. Document = new Infragistics.Documents.TextDocument(); SyntaxEditor.Document = Document; ////////////////////END Add Document/////////////////// ////////////////////BEGIN Add To Group///////////////// //Now add the host the the form. this.Controls.Add(_ctrlHost); //And since this is a non-visual control, we're putting it in a //group container so we can visually control the size. grpDoc.Controls.Add(_ctrlHost); ////////////////////END Add To Group/////////////////// ////////////////////BEGIN Set Properties///////////////// HostProperties(); SyntaxProperties(); DocumentProperties(); ////////////////////END Set Properties/////////////////// } private void SyntaxProperties() { SyntaxEditor.Document.Language = TSqlLanguage.Instance; //syntaxEditor.FontSize = 12; //SyntaxEditor.AllowDrag = true; SyntaxEditor.IsLineNumberMarginVisible = true; //syntaxEditor.Background = new SolidColorBrush(Colors.Red); // syntaxEditor.Width = Double.MaxValue; // syntaxEditor.Height = Double.MaxValue; } private void HostProperties() { _ctrlHost.Dock = DockStyle.Fill; _ctrlHost.AutoSize = true; //You can't use fill and anchor at the same time. //_ctrlHost.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top); } private void DocumentProperties() { } //private void ucSLAServerSelect_ComboSLA_ValueChanged(object sender, System.EventArgs e) //{ // ucSLAServerSelect.lblCollection.Text = "I did this"; //} private void btnGetQuery_Click(object sender, EventArgs e) { var query = new QueryBuilder(uC21.comboSLA.Text, "DBFileProperties").Query; Document.InitializeText(query); } private void SetClipboardIcons() { _rm = new ResourceManager("ConnParameterQB.Images", Assembly.GetExecutingAssembly()); ToggleSaveToFile(); ToggleSaveToClipboard(); } private void picSaveToFile_Click(object sender, EventArgs e) { ToggleSaveToFile(); } private void picSaveToClipboard_Click(object sender, EventArgs e) { ToggleSaveToClipboard(); } private void ToggleSaveToFile() { _saveToFile = !_saveToFile; _picSaveToFile = _saveToFile ? (Bitmap)_rm.GetObject("SaveToFileSmallActive") : (Bitmap)_rm.GetObject("SaveToFileSmallInactive"); picSaveToFile.Image = _picSaveToFile; } private void ToggleSaveToClipboard() { _saveToClipboard = !_saveToClipboard; _picSaveToClipboard = _saveToClipboard ? (Bitmap)_rm.GetObject("ClipboardActive") : (Bitmap)_rm.GetObject("ClipboardInActive"); picSaveToClipboard.Image = _picSaveToFile; } }
Hello,
Try adding the InfragisticsWPF.Documents.TextDocument.TSql assembly. How come you are using the WinForms text document library too?
Dude, it totally works now. Thanks for taking the extra time on this one.
The new app runs fine without errors. Just make sure to include all the assemblies. I noticed you've been mixing WPF4 and the versionfree assemblies.
For context here is what I did, I added the WPF4 syntaxeditor dll and removed the version free ones and everything ran without errors.
Ok, I just uploaded a new version. If you run ConnParameterQB you should get the error at hand and not all the others.
I get the following warning in Visual Studio, and when I rebuild the solution I get two errors
Because the app won't recompile I get the following error prompt on run.
Done.