In .net, is there a way for my CustomButton.vb to have a CustomButton.resx? - vb.net

I have a .NET class, CustomButton, that inherits Control. So I have a CustomButton.vb file that looks like
Public Class CustomButton
Inherits Control
Public Sub New()
Me.InitializeComponent()
End Sub
End Class
with all the properties that I want to override. When I use the class, I go to existing and link it into the current project. The class maybe linked into more than one project.
If you create a custom form and embed an image into it, a .resx file is created.
So my question….
In one of my sub/function I want something such as Me.BackgroundImage = x where x will be an image. If I did Me.BackgroundImage = My.Resources.x then I would have to include x into every project that I linked the custom class into. Is there a way for my CustomButton.vb to have a CustomButton.resx? If so I would I access an image in it call Red.x?
Does this make sense?

There are three files you have to get included in any project, CustomButton.vb, CustomButton.resx and CustomButton.Designer.vb. The latter one is hidden in VB.NET but required to get the My.Resources syntax going. Click the Show All Files button in the Solution Explorer toolbar to see it, expand the .resx node.
A class library project ought to be high on your list of preferred approaches. Also a hard requirement to get it permanently in the toolbox.

What Hans Passant said, got me headed in the right direction.
I not sure how the original .vb file was started, but there was no .designer.vb or .resx. I copied a .resx from a form and renamed it to my .vb file. With the .designer.vb I saw two of the same class names. When I excluded the .designer.vb file one of them when away.
Here is how I got it to work. Taking what #Hans Passant said I looked at the form and how it reference the resource.
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(xxxx))
Where xxxx is the name of the class. Then added the .png file to the resouce and gave it a name yyyy.
Me.BackgroundImage = CType(resources.GetObject("yyyy"), System.Drawing.Image)

Related

put the code in a different file and then "include" it?

In my vb.net project, there are tons of buttons and various items. I would like to keep this code in the form1.vb file. One specific button tough, executes a very long piece of code. What I would like to do is put this code in a different file and then "include" it inside the button. I was thinking of a class, but it looses the connection to all the global variables and the stuff needed for that code. I do not really NEED to put this in a different file, its just something that sits there, that i will not look at again and being so long its in the way. Is there a way to put this in a different file and then include it sort of like it is done in php? I am on visual studio 2015 community edition
Use a partial class such as this:
Partial Public Class Form1
Private Sub doStuff()
Windows.Forms.MessageBox.Show("This is where your long code should go")
End Sub
End Class
Partial classes add more functionality to your class but they can be contained in a seperate file.
http://visualbasic.about.com/od/usingvbnet/a/partclses.htm

The class MyApplication can be designed, but is not the first class in the file [duplicate]

I'm a newbie on vb.net, and I was in progress with my first application... and found some example code in the msdn so I just replaced my Form1.vb file content with the content from the MSDN. When I roll back the changes, and tried to compile my old code then hundreds of errors appeared, and when I switch to the Form1[Design] tab I see this:
The class Form1 can be designed, but is not the first class in the
file. Visual Studio requires that designers use the first class in the
file. Move the class code so that it is the first class in the file
and try loading the designer again.
I'm really new on vb.net and the visual studio itself, and I dont know what to do in this case, is my work destroyed or what?
That's because you added some class or other code above the class definition in form1.vb. Remove that.
What worked for me is editing both Form1.vb and Form1.Designer.vb and placing at the beginning of both files: Namespace Whatever and at the end of both files: End Namespace. The "Whatever" can be any name not already used in the program (or the name of an existing Namespace that you're already using).
You added another class in your form and that is the reason of the error. I had ran into same issue. I had added another class in the form and that caused this error. To resolve, I moved the new class to a module(created new module) and then access the class in the required form.

How to set the class of a vb.NET control after it's been created already

I have a Windows Form with 7 Picture Boxes on it that are called PropButton1 through to PropButton7. I know they aren't buttons but I'm using them as buttons anyway (normal buttons aren't suitable for this purpose).
I want to add a custom "File Path" property to the Picture Boxes. To do this I've created a separate class that inherits the PictureBox class:
Public Class PropButton
Inherits PictureBox
Private SoundFilePath As String
Public Property SoundFile() As String
Get
Return SoundFilePath
End Get
Set(value As String)
SoundFilePath = value
End Set
End Property
End Class
I want to convert the original Picture Boxes from PictureBox to PropButton so I can read and write to things like PropButton1.SoundFilePath and I preferably want to do this without having to delete all of my Picture Boxes and start again. Is there a way to do this?
In Visual Studio look to the right in the Solution Explorer. It has a toolbar button to Show all files. Click it and you will see that you can expand the tree nodes for the forms and they contain three files. One that contains your source code, and another one called a Designer. The Designer file is automatically generated by Visual Studio and in most cases it should not be touched.
When you open the designer file you see all the initializations of the controls on your form and their declarations. Here you can easily change the declarations of your pictureboxes so that they are created as PropButtons instead.
Just be careful what you change here, because it can mess up the Visual Studio designer. But it is good to know what happens behind the scenes.
Look here first:
Change this...
...to this.
Yes, that is possible with the text editor. The Visual Basic IDE hides too much information, first thing you want to do is click the "Show All Files" icon in the Solution Explorer window. That adds a node next to your form in the same window, open it and double-click the Designer.vb file. Note the InitializeComponent() method and the declarations at the bottom of the file, you see the PictureBoxes being declared and initialized.
You can now simply Edit+Replace "System.Windows.Forms.PictureBox" with "PropButton".
Ensure you have a good backup before you do this.

How to create nested user controls in VB.net?

I have the following classes in my vb.net application:
Form1
Usercontrol1
LnkLabel
Usercontrol1 is a user control , and doesnt contain any extra code. LnkLabel is a class that inherits Forms.Label. Its code is goven below:
Public class LnkLabel
Inherits Label
Sub clk handles me.click
Process.start(text)
End sub
End class
When I add an Instance of LnkLabel to usercontrol1, i get an error "type LnkLabel is not defined"
There are three instances of the error in uc1.designer.vb.How can I solve these Errors?
Note:
Visual Studio 2010
.Net FW 3.5
Edit:
The usercontrol1 donot contain any code that might be causing the error. It is just a new usercontrol added to the project.
LnkLabel is added to UC1 by the designer, not by using code at runtime.
The class name is LnkLabel, and not "LinkLabel".
I find that the easiest way to resolve this type of issue is to open the Designer.vb file directly.
To do this, choose Show All Files from the Project menu, then expand UserControl2. Double-click on the UserControl2.Designer.vb file.
You should also be able to get there by double-clicking on the error in the compile errors list.
Once there, search for the definition of UserControl1 or uc1, whatever it may be called (ensure you are in the type definition area, not the property assignment area).
Looking at the definition may give you an instant clue as to the problem (is it in the wrong namespace; did the name of the user control change after you created it, but the change was not propagated to this form; etc).
If it is not obvious what the issue is, use VS intellisense to help you get the right class. I usually clear the previous type definition and start typing the name I know it should be (i.e. UserControl), then select the appropriate value from Intellisense.
Selecting a different class (or correcting the class selection) will require a change to the control instatiation code and may also require a change to some of the properties (I usually just remove the properties I am unsure of and update the control directly in the designer).
Before you switch back to the designer, ensure that you save your changes and, if possible, compile the app.

Creating a Partial Class for a Form

I would like to create a partial class for my form. I have a lot of events, and it gets messy, so I would like to break up sections into their own files.
The problem: When I create a partial class of my form, say:
Partial Public Class Form1
End Class
Visual Studio decides I need another form for this partial class.
Questions:
1. How do I create a partial class for a form?
2. If I cant do that, how can I break up all the events in my form into different files?
Yeah, it does. As soon as you drop a control on this phantom form, you'll get the design-time code (InitializeComponent) generated into that source code file. This is compatibility behavior for .NET 1.x, it didn't support the Partial keyword. Which will break the build, there are now two of them. It is somewhat avoidable with careful clicking, but you know it's going to happen sooner or later.
Other things go wrong too btw, the designer can no longer track an event handler when you move it from one file to another. And will readily let you add another, a much trickier source of bugs.
This just doesn't work very well, abandon hope of relying on it to solve your problem.
The generic diagnostic is that a convoluted user interface begets convoluted code. But that ship has sailed, no doubt. A more structural solution is pursuing the MVC model, separating the data from the view of the data. You'll still have a lot of event handlers but they won't do anything more than calling a method of a class that does the real work. Whose source code can of course live in another source code file. The typical hangup is that Windows Forms has no support whatsoever built in for this, you have to craft it by hand. Nothing similar to the MVVM model in WPF.
Something that can work well is isolating control + code into a separate UserControl. You have to do so carefully though, you don't want to have to add a bunch of properties and events that expose internal controls.
Sometimes I create partial classes for better readibility, especially when I have very large classes.
But when I click on the partial class, then the VS IDE will open the form editor showing me an empty form. If I do not care, than I could damage the main form (it seems to be a bug of VS 2008/2010)
A possibility could be using DesignerCategoryAttribute Class
Mark the partial class with the attribute "code".
<System.ComponentModel.DesignerCategory("code")>
Partial Class Form1
End Class
In this way when you click on the file, you open the class in the code editor.
Of course this will apply to all files, also to the main form file.
If you want to edit again your form in the form editor, you have to quote the attribute:
'<System.ComponentModel.DesignerCategory("code")>
Some more details here.
While it does not answer the original question, I found using regions made my code a little more manageable/readable.
#Region "RegionA"
#End Region
I orginally called this method a "hack", thus the comment below.
Not sure what you mean be "Visual Studio decides you need another form", however, are you sure the new Form1 partial class is declared in the corresponding original namespace?
All partial classes for a given .NET type must be declared in the same namespace of course (whatever files they're stored on).
I appreciate the answers given by Hans and I'm not disputing these at all. It is curious though that in Visual Studio 2010, when you create a form called say Main you get a Main.designer.vb which is a partial class. It says 'Partial Class Main' at the top. This class doesn't open a form when clicked. It also includes reference to Event Handlers. So I was wondering how do they get around this? Is there a way to create one of these 'special' partial classes that work as we would expect.
I noticed that when I created a Form Partial class, that the icon went from a class icon to a form icon. The icon associated with the Main.designer.vb file looks like a class icon with a arrow.
what worked for me (VS 2010) was naming Form1 class, already saved in Form1.vb with its own designer (Form1.Designer.vb) as:
Public Class Main 'saved in Form1.vb
VS updated the name in the designer as:
Partial Class Main 'saved in Form1.Designer.vb
then I created another "partial class" with the same name:
Partial Class Main 'saved in Main.vb
Whether I am editing Form1.vb or Main.vb VS shows me on the top navigation pan all the routines, functions, subs, even background workers and timers. For event handlers, to avoid the loophole mentioned earlier (you click on a control in the layout designer and a brand new event handler will be created in the original Form1.vb) I go:
Partial Public Class Main 'in Form1.vb file
Private Sub SomeControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SomeControl.Click
Call SomeControlClick(sender, e)
End Sub
End Class
Partial Public Class Main 'then in Main.vb file
Private Sub SomeControlClick(ByVal sender As Object, ByVal e As System.EventArgs)
'blah blah
End Sub
End Class