put the code in a different file and then "include" it? - vb.net

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

Related

Regarding vba classes coded in Visual Studio VB.net

I'm trying to start coding my macros for Excel in Visual Studio, it needs changing some things but I think I'm ok as for normal modules go.
My question is, how do I code classes there?
When I try to code a new class it forces me an argument called Of:
Public Class FNMT(Of)
Public Sub TratarDatos(arr As Object, Dict As Object)
End Sub
End Class
And says it's expecting an identifier. How would classes work in Visual Studio? I couldn't use properties neither, it was saying that I can't use the Let/Get.
Can't find anything about this either here nor in google. Any insights?
This is how I'm writting the code:

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

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)

execute other class before executing the main windows form application class

I've been given a windows form application written in VB. For some reasons I will need to execute the second class before the form application in the first class. The form class has to be the first class in the file. I can't simply inherit the second class and call the functions, because it has already used up the only allowable inheritance. I did some research and found there is something called main procedure that determines which codes executes first? It is automatically generated for any windows form application, but I simply can't find that file. Any thoughts on that? or any other ways that I do this?
Follow Start VB.NET GUI app using Sub Main or form startup object? for better alternatives.
But if you really need to start with Main(), follow these steps.
Open application settings.
Uncheck "Enable application framework"
Set startup object to "Sub Main"
Then add a new source file (.vb) and include Main() in it
Module MainModule
Sub Main()
'Your code here
End Sub
End Module

Public properties from another class in vb.net

I've tried searching... a lot for the answer, but as I'm not too sure what exactly I'm trying to do I can't seem to find anything.
I'm trying to write a dll in order to handle errors thrown from a vb.net app. In the dll I need several forms (I'm not totally sure if they can have forms - I'm a bit of a newbie when it comes to dll's) for which the user can type in their message about the error and submit it.
I need to be able to pass strings to the form and for the form to be able to access public subs in the class file.
For instance I have a public sub called Emailer, which I want, when the submit button is clicked from the form to be run.
Or, lets say I have a public string:
Public strName as string = nothing
why cant I, from the class file just do this:
frmFormName.strName = "abc"
Not sure if I've explained that very well, but like I said I'm a bit of a newbie with this stuff.
1) You can have froms in a DLL it just does not have a entry point you will need a exe for this this. Which it sounds like you have
2) You will need to add a project reference to a dll from your exe
ok not sure how this will work but try to download this Example project let me know if you have more questions

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