Issue with spell check class - vb.net

I 'am trying to use this code (posted 11 years ago !), and I have problems with the vb.net version. I have solved a lot of compilation errors, but I can't solve this one :
MyBase.Child = box
Which returns the error 'Cannot convert a value of type 'TextBox' to 'UIElement'. I tried casting and a lot of other things, but as a real newbie I don't understand what could be wrong. And as a newbie on this forum, I can't ask within a comment in the original question...

In the question you linked to, the accepted answer contains code for a class that inherits ElementHost, which is a WinForms class that exists specifically to host WPF controls. That means that the TextBox in that code is a WPF TextBox. Note the namespace imports in that code. It's the System.Windows.Controls.TextBox control that is being used. I suspect that you either don't have that namespace imported or have the System.Windows.Forms namespace imported preferentially and thus, in your code, TextBox is actually being interpreted as System.Windows.Forms.TextBox.

Related

Inheritance problem with System.Windows.Forms.Form

I'm studying an older UI customization project (VB.NET Windows forms) by someone else and seem to be running into some kind of problem relating to inheritance. I can duplicate the issue easily, as I'll show, but why I'm baffled is that the project I'm studying is coded the exact same way but doesn't seem affected by this.
Example Code
Let's say we want to create our own form class like so, which would have special properties, event handlers etc.:
Public Class MyFormBase
Inherits System.Windows.Forms.Form
End Class
And then in both our Form1.vb and Form1.designer.vb, we add:
Inherits MyNamespace.MyFormBase
thus replacing the inheritance in Form1.designer.vb which previously said
Inherits System.Windows.Forms.Form
The Error Message
When I try to view the design view of Form1, it is blocked with this error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: Form1 --- The base class 'MyFormBase' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
But, It Works Here...
When I look at the code for the VB.NET Project I'm studying, it's coded exactly the same way, and yet it works/runs without any problem at all. They both inherit from RJMainForm:
form.designer.vb:
Partial Class MainFormDemo
Inherits RJCodeUI_M1.RJMainForm
and form.vb:
Public Class MainFormDemo
Inherits RJMainForm
He said that he created his project originally in VS2012. I tried making the Framework versions match (thus referencing the exact same library files), adding the exact same references etc.
Conclusion
Why does this work in his solution but not mine? I've been through every config file and everything else, but can't find any explanation.
UPDATE #1
The older project that I was learning from included the actual .vb class files, so I was referencing the classes in them when doing my inheritance.
As a test, I did what Jimi suggested and created an Inherited Form. But you have to reference a DLL, which I didn't have - I had several dozen .vb class files spread out over multiple directories. It took some doing, but I managed to turn all of that into a DLL and use it as the basis of an Inherited Form. This Inherited Form approach works without issue and is a great long term solution, since I can now re-use the DLL.
Unfortunately, this brings me no closer to understanding what the actual problem is in my original approach, so I'm still hoping someone will be able to explain what's wrong with my original example code.

DocumentFormat for OpenXML

I'm trying to create an Excel file to save using OpenXML and I've found some guidelines on Microsoft website, talking about declaration of OpenXML through importing modules. I added at the beginning of the code:
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Spreadsheet
Imports DocumentFormat.OpenXml.Packaging
I'm having back three warnings, each per line: BC40056 Namespace or type specified in the Imports 'DocumentFormat.OpenXml' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. Besides, I'm also having issues while compiling the code itself, because every time I recall DocumentFormat, I receive back an error about "not declared" variable. Which could be the problem? Thanks in advance all are gonna answer me.

Visual Studio 2015 (VB.NET) IntelliSense not showing variables, methods and properties

Up to now IntelliSense listed all public variables, methods and properties of a class, when I used the name somewhere else, but now it only shows a few properties and methods. The problem also occurs with forms and other controls.
There is a post, which dealed already with this issue (link to the post), but it does not provide a working solution. Maybe someone knows the reason for this behaviour.
Edit: Here is a screenshot with an example:
From your screenshot the problem is easier to spot. You are using the Default Instance of a form. As Hans said, intellisense not displaying here is a probably a minor bug that Microsoft will likely not fix. Your code is easier to fix however. Don't use the default instance; it should look like this:
dim f as new Form1
f.Sub1 '<--- should get intellisense here now.
You should avoid using default instance of forms. In my experience, they cause nothing but problems. Creating a form instance, storing it in a local variable or field, and passing those references around is easy enough. It's also a much better practice that will make your code stronger overall.

Class appears to be used without an instance. It is possible? VB.net [duplicate]

This question already has answers here:
Objects implicitly instantiated in vb.net?
(2 answers)
Why is there a default instance of every form in VB.Net but not in C#?
(2 answers)
Closed 7 years ago.
Short version: A class is being used without a variable of it's type being instantiated. How?
I have a huge legacy program which was converted from VB6 to VB.net.
It is compiling and many aspects work, but there is a problem related to an MDI (Multiple-Document Interface) display. I have placed other test forms under the parent MDI form, and they display correctly. The form in question does not display. (And, of course, it is the MOST important, and a most intricately complex form. I wish I could re-write it, but there is no possibility of that.)
There is a class, MDI1, which is used over 7,000 times in the code by many, many other classes. The MDI1 class is used extensively in the form which does not display. Wherever MDI1 is used, it is always referred to as simply MDI1 or Namespace.MDI1 . As far as I can tell, it is never instantiated as an object. It is as though it is a singleton, somehow, but I see nothing making it one.
The header for the class follows:
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class MDI1
Inherits System.Windows.Forms.Form
Dim MDI_Activated As Boolean
Public Sub New()
MyBase.New()
InitializeComponent_Renamed()
End Sub
...
Can anyone tell me what may be going on here?
Every place where it is used that I've tried to check in Visual Studio by right clicking and selecting "go to definition" takes me right back to the class definition (line 4 of the above code), and never to a variable of type
MDI1. I have searched the entire source (using both Visual Studio and grepwin outside of Visual Studio) and can find no variable instantiated with type MDI1.
I don't understand how the calls to the class are working, without a variable of that type.
Help would be greatly appreciated.
I am using Visual Studio 2010 Professional, the latest version to which I have access.
I am assuming that you are not aware of Default Form Instance in VB.NET and hence the surprise. Its a common problem since this is missing in C#.
For winforms application in VB.NET, each form has a default instance and can be referred to simply by its form name (class name). You can ofcourse create your own instances like any other class and choose not to use the default instance.
This feature is particularly useful for legacy applications that have been migrated from VB6 to VB.NET since default form instance was a norm in VB6.
You can read more about Default Form Instances here:
https://msdn.microsoft.com/en-us/library/ms233839.aspx
And
https://msdn.microsoft.com/en-us/library/aa289529(v=vs.71).aspx

System.Data.Function is not available in this context because it is 'Friend'

I'm currently trying to create a NEWID() function in my DataContext in LINQ using the solution here, however, when adding the partial class to my DataContext.vb or a separate DataContextPartial.vb, I get the error System.Data.Function is not available in this context because it is 'Friend'.
I've come across this when accessing data types before and that was in easy fix of setting it to Public, but I'm not sure where the properties for function could be or how to change them.
The code I have is converted to VB.NET from the C# in the linked answer above:
Partial Public Class CMSModelDataContext
<[Function](Name:="NEWID", IsComposable:=True)> _
Public Function Random() As Guid
Throw New NotImplementedException()
End Function
End Class
Thanks for any help in advance.
I can't remember offhand whether VB applies the "Attribute" suffix automatically. Try this instead:
<FunctionAttribute(Name:="NEWID", IsComposable:=True)>
... and make sure you have an import for System.Data.Linq.Mapping.
EDIT: It looks like VB does apply the Attribute suffix, so I suspect you were missing an import or a reference. However, specifying FunctionAttribute explicitly will at least help you to verify this by removing the "false positive" of System.Data.Function.
I believe you should
Import System.Data.Linq.Mapping
because FunctionAttribute resides there.
You didn't import the namespace, and compiler went to look for the class in the wrong direction. Trying its best and seeing that you have imported System.Data, compiler assumed you want to use System.Data.Function which is an internal (Friend) class in System.Data.dll assembly, hence the error.
One can wonder what exactly is the purpose of this error message. If the class isn't accessible anyway, why even bothering to tell about it? I think the reason is you could've referenced your own assembly forgetting to make some of types Public. It makes sense that compiler warns you that it sees the class but you just can't use it. It also makes sense applying same rules to all references, including framework libraries, although obviously you can't modify anything in there.
I would argue that FunctionAttribute is not a particularly good choice of name because it's begging for wrong namespace imports and related confusion.