Editor Pane in IWpfTextView is not Editable - c#

I am trying to create a Visual Studio extension that will open a document window containing a custom control of mine that hosts an editor pane. I can get the document to load, the correct content type loads with my custom content extensions (classifier, quick info), a window with my custom control showing the editor pane loads, but the text cannot be modified. I can select and highlight text, but no keys work, no commands, and no mouse input other than text selection.
The following is what I have in my Package class:
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[ProvideEditorExtension(typeof(MyCustomEditorFactory), ".cust")]
[Guid(MyCustomExtensionsPackage.PackageGuidString)]
public sealed class MyCustomExtensionsPackage : AsyncPackage
{
public const string PackageGuidString = "ec2c4646-d0cc-42c6-b0a6-d0ff3e318cef";
#region Package Members
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
RegisterEditorFactory(new MyCustomEditorFactory(this));
}
#endregion
}
I have verified that the RegisterEditorFactory method is called, and that my editor factory is called when files with the .cust extension are opened.
The following is my editor factory:
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Utilities;
using System;
using System.ComponentModel.Composition;
using System.Runtime.InteropServices;
[ComVisible(true)]
[Guid(EditorFactoryGuidString)]
public sealed class MyCustomEditorFactory : IVsEditorFactory
{
public const string EditorFactoryGuidString = "38AD95BA-8891-46A2-A5EA-25F5F36EEAE0";
private MyCustomExtensionsPackage _package;
private Microsoft.VisualStudio.OLE.Interop.IServiceProvider _vsServiceProvider;
[Import]
public IContentTypeRegistryService ContentTypeRegistry { get; set; }
[Import]
public ITextEditorFactoryService TextEditorFactory { get; set; }
public MyCustomEditorFactory(MyCustomExtensionsPackage package)
{
_package = package;
}
public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
{
_vsServiceProvider = psp;
return (VSConstants.S_OK);
}
public int Close()
{
return (VSConstants.S_OK);
}
public int MapLogicalView(ref Guid rguidLogicalView, out string pbstrPhysicalView)
{
pbstrPhysicalView = null;
return (VSConstants.LOGVIEWID_Primary == rguidLogicalView ? VSConstants.S_OK : VSConstants.E_NOTIMPL);
}
public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW)
{
ThreadHelper.ThrowIfNotOnUIThread();
ppunkDocView = IntPtr.Zero;
ppunkDocData = IntPtr.Zero;
pbstrEditorCaption = string.Empty;
pguidCmdUI = VSConstants.GUID_TextEditorFactory;
pgrfCDW = 0;
int retVal = VSConstants.E_FAIL;
if ((grfCreateDoc & (VSConstants.CEF_OPENFILE | VSConstants.CEF_SILENT)) != 0)
{
IVsTextLines textBuffer = null;
if (punkDocDataExisting == IntPtr.Zero)
{
IComponentModel mef = _package.GetService<SComponentModel, IComponentModel>();
mef.DefaultCompositionService.SatisfyImportsOnce(this);
IVsEditorAdaptersFactoryService eafs = mef.GetService<IVsEditorAdaptersFactoryService>();
textBuffer = eafs.CreateVsTextBufferAdapter(_vsServiceProvider, ContentTypeRegistry.GetContentType("CUST")) as IVsTextLines;
string fileText = System.IO.File.ReadAllText(pszMkDocument);
textBuffer.InitializeContent(fileText, fileText.Length);
string[] roles = new string[]
{
PredefinedTextViewRoles.Analyzable,
PredefinedTextViewRoles.Editable,
PredefinedTextViewRoles.Interactive,
PredefinedTextViewRoles.Document,
PredefinedTextViewRoles.PrimaryDocument
};
IWpfTextView dataView = TextEditorFactory.CreateTextView(eafs.GetDataBuffer(textBuffer), TextEditorFactory.CreateTextViewRoleSet(roles));
dataView.Options.SetOptionValue(DefaultTextViewHostOptions.LineNumberMarginName, true);
dataView.Options.SetOptionValue(DefaultTextViewHostOptions.ShowCaretPositionOptionName, true);
dataView.Options.SetOptionValue(DefaultTextViewHostOptions.ChangeTrackingName, true);
dataView.Options.SetOptionValue(DefaultTextViewOptions.ViewProhibitUserInputName, false);
IWpfTextViewHost wpfHost = TextEditorFactory.CreateTextViewHost(dataView, false);
MyCustomEditor editor = new MyCustomEditor(wpfHost);
ppunkDocData = Marshal.GetIUnknownForObject(textBuffer);
ppunkDocView = Marshal.GetIUnknownForObject(editor);
retVal = VSConstants.S_OK;
}
else
{
//code for document already open
}
else
{
retVal = VSConstants.E_INVALIDARG;
}
}
return (retVal);
}
}
There's a lot to unpack there, but ultimately I'm just
Creating an IVsTextBuffer from an IVsEditorAdaptersFactoryService
Loading the contents of the text buffer
Creating an IWpfTextView with the IVsTextBuffer from a ITextEditorFactoryService
Creating an IWpfTextViewHost with the IWpfTextView from the same ITextEditorFactoryService
Creating my my custom window pane and passing in the IWpfTextViewHost
Returning the IVsTextBuffer as the document data and my custom window pane as the document view
My custom window pane code is simply:
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text.Editor;
using System.Runtime.InteropServices;
[ComVisible(true)]
public sealed class MyCustomEditor : WindowPane
{
private IWpfTextViewHost _wpfHost;
public MyCustomEditor(IWpfTextViewHost wpfHost)
{
_wpfHost = wpfHost;
Content = new MyCustomEditorControl(wpfHost.HostControl);
}
}
I've omitted the code for the MyCustomEditorControl for the sake of brevity, but just assume it's a simple WPF UserControl with a ContentPresenter. The constructor just sets Content property of the ContentPresenter with the wpfHost.HostControl that's passed in.
All of this works insofar as displaying the contents of a file, but I just can't edit anything. It's like the input bindings aren't wired up or enabled, but I can't find what properties to set or enable on which objects, and the documentation is rather poor on this topic. I've gone through the Visual Studio extensions walkthroughs on MSDN, but it seems to be a mish-mash of pre-WPF and post-WPF APIs with no clear guidance on what the authoritative approach is.
Admittedly, I may be naive in assuming that the IWpfTextView would automatically handle user input, but in my defense it's created from an ITextEditorFactoryService and having "TextEditor" in the name carries certain implications.
I'm going to go through a rather exhaustive list of things I've tried and checked to hopefully help narrow down the possibilities or where my mistake is:
After the document window is open, I've checked the Running Document Table and there is an entry for it that is automatically created after CreateEditorInstance exits. It links to the data buffer that's returned, the correct file path, and has an edit lock but no read lock. I've inspected the RDT when C# files are opened in a normal editor window and those have read and write locks. I tried manually setting a read lock on the RDT as well, the counter increments, but that doesn't seem to make a difference.
I've inspected the return result from ITextBuffer.CheckEditAccess on the data buffer and it returns true so the text buffer is reporting it's editable.
The textview roles DO have an effect on the final rendered IWpfTextViewHost. If I add or omit roles, the appearance and functionality changes. For example, adding or removing "ZOOMABLE" adds or removes the dropdown box with the control zoom level. However, "EDITABLE" has no effect on editability.
The editor options DO have an effect. For example, adding or removing the line number margin options does show/hide the line numbers. However, ViewProhibitUserInputName has no effect on editability.
I don't create a code window with CreateVsCodeWindowAdapter because the resulting IWpfTextViewHost that can be retrieved from GetWpfTextViewHost is already parented to another control. If I disconnect it with (IWpfTextViewHost.HostControl.Parent as Border).Child = null, then the contents of IWpfTextViewHost.HostControl gets disposed of for some reason. The IWpfTextViewHost.HostControl loads and it displays a window with margins and scrollbars, but the content is blank and if I click into the left margin of the content pane then Visual Studio throws an ObjectDisposedException. Please note I'm not nulling out the host control itself or its contents, I'm only telling its existing parent it no longer has a child.
I've inspected the editor GUID property of the IVsWindowFrame that opens my document and it's set to the value returned in the pguidCmdUI parameter, which I believe is correct.
I'm not certain if the value being set for the pguidCmdUI parameter in the editor factory CreateEditorInstance is correct. I'm aware it's used for command routing, which might explain why I can't type or use the mouse, but I'm not sure what the correct value should be. I read somewhere it's supposed to be the GUID of the factory that created the editor, so I set it to the text editor factory GUID since that's what created the IWpfTextView. I tried setting it to my editor factory GUID at the top of the file, as well as typeof(IWpfTextView).GUID, Guid.Empty, and assorted other GUIDs, but there's no change.
If I pass my IWpfTextView into IVsEditorAdapterFactoryService.GetViewAdapter, I get back null.
If I create a IVsTextView from IVsEditorAdapterFactoryService.CreateVsTextViewAdapter, I can have both an IVsTextView and an IWpfTextView, but the two don't know about each other and I don't know see any way to map them. I don't even know if they need to be mapped, or if IVsTextView needs to exist or is just the legacy text view interface.
When I inspect the created IVsTextView, it's base type is a SimpleTextViewWindow that is an undocumented class that's internal to Microsoft's Visual Studio implementation, it has WpfTextView and WpfTextViewHost properties, but they're both null (or throw an exception that they can't be read) and are unsettable. Microsoft obviously has some internal voodoo magic to map IVsTextViews to WpfTextViews in their own code, but I don't know what it is.
I'm aware of other code samples that create an IVsInvisibleEditor and get the data buffer from that, but that seems kind of hacky. I mean, it technically works and no one seems to know of any alternative because the documentation in this area is so poor, so I'm not judging or criticizing, it just seems like that shouldn't be necessary. It's my (possibly incorrect) understanding that an invisible editor is a virtual editor that is created for documents that are opened in memory but not hosted in a window, but I'm hosting mine in a window. Additionally all of the working code samples I could find are all copy/pasting from the same Microsoft VSIX code example. In that example, the virtual editor is created in a tool window that's created under the assumption that a document is already open in an existing editor window. So that tool window is creating a virtual editor side-by-side to a physical one for a tool window. If I follow the same invisible editor approach, then I'm effectively creating a virtual editor in memory and then a second physical document editor window. So basically, I'm creating two editors for one document. That doesn't seem right. Again, it technically works, but it seems off.
If the expectation is that we have to implement IOleCommandTarget on our editor window and manually handle every single keystroke, shortcut, and mouse button and manipulate the underlying text buffer directly, then that is going to be the most depressing thing ever. That would mean they tout being able to use WPF, but then give us a document view without a WPF control that natively handles text editing even though they exist in the framework.
Sorry for the ridiculously long post, but I wanted to provide as much information as I possibly could about where I'm at and what I've tried.

Regarding CreateVsCodeWindowAdapter, see the "official" explanation how to make it work: https://developercommunity.visualstudio.com/t/projectionbuffertutorial-gives-error-in-dev16/498617

Related

Programmatically access "Find All References" data in Visual Studio 2019

I am attempting to use automation in Visual Studio 2019 to help with repetitive tasks as part of a large code merge. I've researched quite a lot on this and am aware that using Roslyn can get me the information I need, but since I am performing other automation tasks in the IDE, I would rather persevere at extracting everything from the IDE as it is presented to me. I got quite a few bits working, but can't seem to be able to manipulate the Find All References tool windows. I can invoke the dialog using dte.ExecuteCommand("Edit.FindAllReferences"). I can also access the Window object from EnvDTE.Windows that corresponds to an existing "Find All References" window, but can't do much with it or the Window.Object property. I am also able to get the IVsWindowFrame object for an existing "Find All References" window, but again I am unable to do very much with it. The way to go appears to be to acquire IFindAllReferencesService but for me, GetService() always returns null. I've since tried the VCmd extension which lets you code macros against the IDE. I found that the macro code in this extension runs in the same process as the IDE - I don't know if it's for this reason or not, but the same GetService() code inside a macro returns a valid interface (Microsoft.VisualStudio.ErrorListPkg.FindAllReferencesService) which is what I need to progress being able to get references information out of a tool window.
I have the following code so far:
using EnvDTE80;
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
//using Microsoft.VisualStudio.Shell;
//using Microsoft.VisualStudio.Data.Framework;
namespace Util2
{
public static class StackOverflowQuestion1
{
[DllImport("ole32.dll")]
private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc);
[DllImport("ole32.dll")]
private static extern void GetRunningObjectTable(int reserved, out IRunningObjectTable prot);
public static object GetInstanceBySolutionFileName(string FileName)
{
GetRunningObjectTable(0, out var rot);
rot.EnumRunning(out var enumMoniker);
enumMoniker.Reset();
var fetched = IntPtr.Zero;
var moniker = new IMoniker[1];
CreateBindCtx(0, out var bindCtx);
object comObject;
string displayName;
EnvDTE.DTE dte;
while (enumMoniker.Next(1, moniker, fetched) == 0)
{
moniker[0].GetDisplayName(bindCtx, null, out displayName);
if (!displayName.StartsWith("!VisualStudio.DTE.16.0:"))
continue;
rot.GetObject(moniker[0], out comObject);
dte = (EnvDTE.DTE)comObject;
if (dte.Solution.FullName == FileName)
return comObject;
}
return null;
}
public static void Test()
{
Microsoft.VisualStudio.Shell.FindAllReferences.IFindAllReferencesService findAllReferencesService;
//IServiceProvider serviceProvider;
var dte = GetInstanceBySolutionFileName(#"C:\YOUR PROJECT PATH\YOUR_SOLUTION_FILE.sln");
var serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte);
//serviceProvider = new Microsoft.VisualStudio.Data.Framework.ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte);
findAllReferencesService = serviceProvider.GetService(
typeof(Microsoft.VisualStudio.Shell.FindAllReferences.SVsFindAllReferences)) as
Microsoft.VisualStudio.Shell.FindAllReferences.IFindAllReferencesService;
if (findAllReferencesService == null)
MessageBox.Show("NULL!");
else
MessageBox.Show("OK!");
//MessageBox.Show(findAllReferencesService.GetType().ToString());
}
}
}
I am "supposed" to be using packages instead of automation from another application, but the concept of packages and their purpose don't fit what I am trying to achieve. I am trying to create a bunch of code snippets to help me automate a large merge, after that I will not need it. Also, there is a possibility I will need to manipulate 2 VS instances, so even the fact that the code works inside a package, it should not work if that package attempts to hook into another VS instance (i.e. it will have the same problem as my application).
For what it's worth, here's the code that can get me the IVsWindowFrame for the Find All Reference window(s):
var shell = (IVsUIShell)serviceProvider.GetService(typeof(SVsUIShell));
var guid = new Guid("a80febb4-e7e0-4147-b476-21aaf2453969");
shell.FindToolWindowEx((uint)__VSFINDTOOLWIN.FTW_fFindFirst, ref guid, 0, out var windowFrame);
Note, the GUID comes from EnvDTE.Windows.Item().ObjectKind for a references window. Here is a little more about such a window, as an example:
Caption='txtTest_TextChanged' references
Kind=Tool
ObjectKind={A80FEBB4-E7E0-4147-B476-21AAF2453969}
Type=vsWindowTypeToolWindow
As a hack for now, I will call the extension's macro from my application (e.g. VCmd.Command01) which in turn will locate the Find All References window (or search for references for symbol at cursor) then write the results somewhere (e.g. file) and my application would read the results.
EDIT: My hack didn't work as planned and even though it had mileage in the end, I found a quicker way (sort of). After executing command Edit.FindAllReferences, ensure the Visual Studio window itself is focused, the 'Find references' tool/tab would be in a focused state, so I sent it a CTRL+A then CTRL+C using SendInput - all the details I need are in the clipboard which achieves my goal. Similar could be achieved by sending keys to navigate the box, but I haven't got the need for that yet. Obviously this is quite hacky and being able to use IFindAllReferences from another application would be preferrable. Using the macro extension VCMD, I was able to acquire IFindAllReferencesService ref, and was able to call StartSearch(). Looking in more detail, StartSearch merely initialises the window that it returns which is returned as IFindAllReferencesWindow. I haven't tried this, but I think it's possible to hold on to the IFindReferencesWindow interface returned by StartSearch (passing it the exact same label name as it would appear in the IDE e.g. "'button7' references"), then perform Edit.FindAllReferences command, then access the IFindAllReferencesWindow's properties to hopefully get the data. But there were other difficulties...so Clipboard method wins for now :)

EditorWindow.GetWindow doesn't work

This is my code:
public class CrazyWindow: EditorWindow
{
[MenuItem("Window/CrazyWindow")]
public static void Window()
{
EditorWindow.GetWindow(typeof(CrazyWindow));
Debug.Log("It should have appeared!");
}
string test = "";
public void OnGUI()
{
test = EditorGUILayout.TextField ("Text Field", test );
}
}
I'm using Unity3D v. 4.3.4f1 (free version) on Windows 7.
I have no idea why this is happening, as I can see in tutorials in the internet, that's how it should be done. The script is also in the Editor folder.
I'm able to click on the option "CrazyWindow" in the window menu, and I also get the Debug message informing me that the window should be working, but nothing happens besides that. No window is created at all!
What might be the cause of my problem?
Problem solved.
As Bart mentioned, I was using a custom Editor Layout, which was the case for the window not showing.
I just switched to one of the factory editor layouts and: ta dah, the window was there...
Pretty buggy thought.
Try renaming the 'CrazyWindow' part in the MenuItem and of the class itself. Unity remembers whether a window is visible or not and somehow something goes wrong there. Probably it thinks your window is visible (in cache) while actually it is not.
As Bart said, it remembers useless things
Just make it remember what we want it to
private void OnLostFocus() {
GetWindow<CrazyWindow>().Close();
}

How to create custom User Control derived from CommonDialog class

I would like to extend the FolderBrowserDialog to have the option to include subfolders for selected Folder (add Checkbox to set to include or not). I found out that I can't extend the basic FolderBrowserDialog as it is a sealed class.
So I thought the simplest solution would be to create a user control, that is derived from CommonDialog (the same class as is FolderBrowserDialog), copy the code from standard FolderBrowserDialog and just alter it a little bit so it would also have the "Include Subfolders" Checkbox.
But when I copied the code from default FolderBrowserDialog it gives me an error:
missing partial modifier on declaration of type [my_class_name] another partial declaration of this type exists c#
And it points out to the "[my_class_name].Designer.cs" file.
namespace my_custom_folder_open
{
// Summary:
// Prompts the user to select a folder. This class cannot be inherited.
[DefaultEvent("HelpRequest")]
[DefaultProperty("SelectedPath")]
[Designer("System.Windows.Forms.Design.FolderBrowserDialogDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public sealed class UserControl2 : CommonDialog
{
// Summary:
// Initializes a new instance of the System.Windows.Forms.FolderBrowserDialog
// class.
public UserControl2();
// Summary:
// Gets or sets the descriptive text displayed above the tree view control in
// the dialog box.
//
// Returns:
// The description to display. The default is an empty string ("").
[Browsable(true)]
[DefaultValue("")]
[Localizable(true)]
public string Description { get; set; }
//
// Summary:
// Gets or sets the root folder where the browsing starts from.
//
// Returns:
// One of the System.Environment.SpecialFolder values. The default is Desktop.
//
// Exceptions:
// System.ComponentModel.InvalidEnumArgumentException:
// The value assigned is not one of the System.Environment.SpecialFolder values.
[Browsable(true)]
[Localizable(false)]
public Environment.SpecialFolder RootFolder { get; set; }
//
// Summary:
// Gets or sets the path selected by the user.
//
// Returns:
// The path of the folder first selected in the dialog box or the last folder
// selected by the user. The default is an empty string ("").
[Browsable(true)]
[DefaultValue("")]
[Localizable(true)]
public string SelectedPath { get; set; }
//
// Summary:
// Gets or sets a value indicating whether the New Folder button appears in
// the folder browser dialog box.
//
// Returns:
// true if the New Folder button is shown in the dialog box; otherwise, false.
// The default is true.
[Browsable(true)]
[DefaultValue(true)]
[Localizable(false)]
public bool ShowNewFolderButton { get; set; }
// Summary:
// Occurs when the user clicks the Help button on the dialog box.
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler HelpRequest;
// Summary:
// Resets properties to their default values.
public override void Reset();
protected override bool RunDialog(IntPtr hWndOwner);
}
}
Where could be a problem?
Btw I've created the project as Windows Forms Control Library..
You are seriously on the wrong track with this. These dialogs are Components, not controls. They are very thin wrappers around the dialogs that are built into Windows. Those dialogs themselves don't know anything about .NET and are written in unmanaged code. The only reason they are a Component and not just a plain class is to allow you to drop one on a form. Helpful to set a few properties with the designer.
Maybe the term "CommonDialog" was misleading. Microsoft called it "common" only because they are commonly used dialogs in a GUI program. And encouraged using the built-in ones so that every program has a very similar way to, say, open a file.
It doesn't make much sense to derive from CommonDialog because there is no need to create a custom dialog. Because Windows only has the built-in ones and they are already wrapped by their respective .NET classes. Your plan is going to wreck on what the native FolderBrowserDialog can do. Which does not include showing checkboxes. It was sealed for a good reason.
Hans is quite correct that you cannot solve your problem by deriving a new class from CommonDialog. What you can do though is to use the raw IFileDialog component in folder picker mode. You'll also need to use IFileDialogCustomize to add your check box. Since this is just COM it's actually very simple to use from .net.

Where to find the default Winforms icon in Windows?

I assume this is a shared resource somewhere in Windows. Rather than making a copy for each app, is there a way to use this icon just like all Winforms apps use it?
How is this specified for Winforms apps by default? I don't see any reference of any icons in code or project settings. Just that it uses the "default icon".
It is stored as a resource in the System.Windows.Forms.dll assembly. You could get a copy with Reflector. Open the assembly, open the Resources node, all the way down to "wfc.ico". Right-click, Save As. Not sure why you'd want to use it, given that it is the default.
You set a custom icon for your application with Project + Properties, Application tab, Icon setting. Each form has its own Icon property.
If you have Visual Studio 2010 installed then there is a large collection of icons (potentially including the application icon/s), check out the following directory:
%ProgramFiles%\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1033
There may be a similar directory for previous VS versions, take a look if needs be.
EDIT:
On doing a search in the folder of the unzipped file for app there are two notable results:
Application.ico and ApplicationGeneric.ico + its *.png counterpart.
If you have VS 2010 and any of the icons in here are suitable, I believe you don't need to copy a single one - you should be able to include the file indirectly (as a shared/linked file) when adding using the Existing Item... dialog; you do this by selecting the arrow next to Add button and selecting the Add As Link option.
What I can't see working as desired is simply overwriting these files in an attempt to apply a global change.
It is stored as a resource in the System.Windows.Forms.dll assembly. You could get a copy with reflection as folow:
public static class FormUtils
{
private static Icon _defaultFormIcon;
public static Icon DefaultFormIcon
{
get
{
if (_defaultFormIcon == null)
_defaultFormIcon = (Icon)typeof(Form).
GetProperty("DefaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null, null);
return _defaultFormIcon;
}
}
public static void SetDefaultIcon()
{
var icon = Icon.ExtractAssociatedIcon(EntryAssemblyInfo.ExecutablePath);
typeof(Form)
.GetField("defaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
.SetValue(null, icon);
}
}
public static class FormExtensions
{
internal static void GetIconIfDefault(this Form dest, Form source)
{
if (dest.Icon == FormUtils.DefaultFormIcon)
dest.Icon = source.Icon;
}
}
So as you can see in the code you have in this way the same Icon.Handle. The same reference.
Form.DefaultIcon is an internal lazy loaded static property in class Form.
You can also override the default Winforms icon for your application. In Program.cs i use:
FormUtils.SetDefaultIcon();
This function will then override the default icon with the icon specified in your Application properties, the icon of your executable.
You can just use the Save method:
C#:
string IcoFilename = "C:\\Junk\\Default.ico";
using (System.IO.FileStream fs = new System.IO.FileStream(IcoFilename, System.IO.FileMode.Create))
{
this.Icon.Save(fs);
}
Visual Basic:
Dim strFilename As String = "C:\Junk\Default.ico"
Using fs As New System.IO.FileStream(strFilename, IO.FileMode.Create)
Me.Icon.Save(fs)
End Using
I had a problem which was similar, but different. Rather than needing to get the default icon, I needed to check to see whether the icon on a form was set or if it was left as the default. While I could have used reflection to get it, I ended up using a simpler solution:
private static Icon defaultIcon = new Form().Icon;
// ...
if(this.Icon == defaultIcon)
{
// ...
}

Silverlight C# - ComponentOne Spellchecker not loading dictionary

This may be a long shot, but I'm using ComponentOne's Spellchecker control for Silverlight. I made a test project, added a plain textbox and a button to it, added the references to the C1.Silverlight and C1.Silverlight.SpellChecker bits, and added the dictionary file to my project.
In the code, I called up the spellchecker on button1's click event and it worked SPLENDIDLY. The spellchecker dialog shows up, and works exactly as it should.
Since that test was successful, I then tried to implement this into my existing project. I've had no success for absolutely NO reason that I can determine, since I used the EXACT SAME code.
Here's the code I use to call the component:
using C1.Silverlight;
using C1.Silverlight.SpellChecker;
using C1.Silverlight.Resources;
public partial class MainPage : UserControl
{
C1SpellChecker spellChecker = new C1SpellChecker();
public MainPage()
{
InitializeComponent();
spellChecker.MainDictionary.LoadAsync("C1Spell_en-US.dct");
}
private void btnSpelling_Click(object sender, RoutedEventArgs e)
{
var dlg = new C1SpellDialog();
spellChecker.CheckControlAsync(txtArticle, false, dlg);
}
The references to C1.Silverlight and C1.Silverlight.Spellchecker are added to this project as well, and the dictionary as been added in the same fashion as well. The issue seems to be that for whatever reason the dictionary is not loading, because the spellChecker.Enabled method returns whether or not the main dictionary has been loaded. If I call MessageBox.Show("SpellChecker Enabled = " + spellChecker.Enabled.ToString()); it shows false, even though the call to load the dictionary is there (as you can see).
What would cause the dictionary to not load? Have I added it to my project incorrectly somehow?
EDIT: I suspect that I have added the dictionary to the project incorrectly, because the ComponentOne reference states:
If C1SpellChecker cannot find the
spelling dictionary, it will not throw
any exceptions. The Enabled property
will be set to false and the component
will not be able to spell-check any
text.
I just don't know what's wrong though because it was added in the same way that it was in the test project (Right clicked on the project.web->Add->Existing Item)
As always, thank you!
-Sootah
You could add the dictionary to the Silverlight app as an embedded resource and then load it using this code:
public MainPage()
{
InitializeComponent();
// load C1SpellChecker dictionary from embedded resource
var asm = this.GetType().Assembly;
foreach (var res in asm.GetManifestResourceNames())
{
if (res.EndsWith(".dct"))
{
using (var s = asm.GetManifestResourceStream(res))
{
sc.MainDictionary.Load(s);
break;
}
}
}
}
I think this post is duplicated in our forum as well, but will answer first here. Please try this:
1) Try to access the .dct file using your browser. If you cannot see it, it's probably because your web server is not serving that type of files. You need ton configure the web server to allow it.
2) verify the URL you are using is correct.http://helpcentral.componentone.com/CS/silverlight_161/f/78/p/86955/241328.aspx#241328
3) Check you are setting everything correctly: http://helpcentral.componentone.com/CS/silverlight_161/f/78/p/81924/227790.aspx#227790
Hope this helps!

Categories