Saving a Dynamically Created form - vb.net

I have created a sample form as
Dim obj_CommonForm As New Form
Dim btn_Create As New Button
btn_Create.Location = New System.Drawing.Point(30, 200)
btn_Create.Size = New System.Drawing.Size(60, 15)
btn_Create.Text = "MyCommand"
obj_CommonForm.Controls.Add(btn_Create)
obj_CommonForm.Show()
Now I would like to save this form as .vb file and include it in my project.
Similar I have created in PHP where output html and saving as .php file is easy. This I have used as a initialize function as i start doing some project whose database has been created already.

Your question is not clear to me... so I try to guess.
If you want to have a .vb file then you just have to take the .vb created by Visual Studio or by your application and include it in the project at compile time.
Instead if you are talking about a generic form created by your program and load it at runtime, you have to learn something about Serialization.
In latter case, I had your same problem some time ago and following article was a good starting point:
http://community.visual-basic.it/lucianob/archive/2007/10/14/20711.aspx
It is in Italian, but I think you can translate via some tool (except for code examples, obviously) :-)

Related

Including source files at compile time with Visual Studio

As usual, I do my research in the various forums. My personal discipline is that if I have struggled for more than a day, I need to reach out for help. Because after a day, I can no longer see the wood because of the trees.
My web site has some code written in VB.NET which I use on many pages of the web site. Where possible, I try to write code only once. For obvious reasons. I include the code in a class. However, when I use a class, I cannot access the page controls. For example, the HTTP context is available on the VB.NET code-behind but not in the class declared in the code-behind. Is there some way of achieving this result?
Another option is to have VB.NET code in a separate source file. And to include this source file into the main source code file at compile time. If I need to make a change, I can do it in the source code “segment” once only. Right now, if I want to make a global change, I have to cut and paste through many web pages. I have read that this type of “include” was possible in VB prior to VB.NET. I can find no reference to this for VB.NET in Visual Studio. Any suggestions?
For example if this code is to be run on every web page, how would you set it up so that you only have one source? That is called on 150 pages?
Protected Sub getCookieVariable()
'
Dim myCookieName As String
Dim myCookieValue As String
'
myCookieName = "hfl3"
Dim myCookie As HttpCookie = HttpContext.Current.Request.Cookies(myCookieName)
If Request.Cookies(myCookieName) IsNot Nothing Then
myCookieValue = myCookie.Value
myCookie.Expires = DateTime.Now.AddDays(30)
Response.Cookies.Add(myCookie)
session("myCookieValue") = myCookieValue
End If
'
End Sub

Including source files at compile time with VB.NET and Visual Studio

As usual, I do my research in the various forums. My personal discipline is that if I have struggled for more than a day, I need to reach out for help. Because after a day, I can no longer see the wood because of the trees.
My web site has some code written in VB.NET which I use on many pages of the web site. Where possible, I try to write code only once. For obvious reasons. I include the code in a class.
However, when I use a class, I cannot access the page controls. For example, the HTTP context is available on the VB.NET code-behind but not in the class declared in the code-behind. Is there some way of achieving this result?
Another option is to have VB.NET code in a separate source file. And to include this source file into the main source code file at compile time. If I need to make a change, I can do it in the source code
“segment” once only. Right now, if I want to make a global change, I have to cut and paste through many web pages. I have read that this type of “include” was possible in VB prior to VB.NET. I can find no reference to this for VB.NET in Visual Studio. Any suggestions?
For example if this code is to be run on every web page, how would you set it up so that you only have one source? That is called on 150 pages?
Protected Sub getCookieVariable()
'
Dim myCookieName As String
Dim myCookieValue As String
'
myCookieName = "hfl3"
Dim myCookie As HttpCookie = HttpContext.Current.Request.Cookies(myCookieName)
If Request.Cookies(myCookieName) IsNot Nothing Then
myCookieValue = myCookie.Value
myCookie.Expires = DateTime.Now.AddDays(30)
Response.Cookies.Add(myCookie)
session("myCookieValue") = myCookieValue
End If
'
End Sub

Creating Custom Form Template

I'm creating a vb.net project with over 20-30 forms. I want to apply same options for every form like "no border,backcolor,picturebox at left bottom etc.". and it takes much time to do these for ever form. Please tell me a shortcut :)
(I've thinked about creating one form and duplicating it.)
Creating Template Forms using Visual Inheritance
https://www.codeproject.com/Articles/7038/Creating-Template-Forms-using-Visual-Inheritance
[RESOLVED] Creating a Windows Form template/library?
http://www.vbforums.com/showthread.php?641019-RESOLVED-Creating-a-Windows-Form-template-library&s=1fb7d88423241485b8fff8e50ef97d04
Create your Form with the properties set then
File (Menu) | Export Template
Note: Links to other posts aren’t advised but there is a lot of content and didn’t want to just copy/paste, these could become dead links in the future mind.
You can create Project Templates and Item Template which can have code files, assets etc already there, it’s how File | New works.

How can I recreate a designer.vb file?

I have a windows forms application in which one of the designer.vb files is missing and has apparently been missing for years (checked all my backups). When I right-click on the vb file in Solution Explorer and select "View Designer", nothing happens. The Windows form designer generated code is still in the vb file, and the form shows up fine when the application runs. Is there any way to recreate the designer file so I can edit the form?
Create a new Form and copy the code from the InitializeComponent method of the old form into the same method in the ".designer.vb" file of the new Form (and replace the existing code in this method.). Also copy the designer generated variable declarations.
Since InitializeComponent creates all the controls and sets all the form properties, this copies the whole design of the old form to the new form.
You will have to copy your own code (containing Load and Click methods and so on) from the old ".vb" file into the new ".vb" file as well.

VB.NET ~ Sub Routines - can they be saved to a text file and then called?

I have about 100 sub routines that I need to use..I am going to be calling them into a web browser component do get some elements after each web page has completed.
Is it possible to create one sub routine and then has say a streamreader loop through a folder and read each text file in the folder to put the sub into a string?
I would then simply call that one sub into the webbrowser component but I didn't know if this was possible?
There would be about 100 different text files in the folder.
The thinking behind this would be that if I wanted to add more website instructions to the sub or take away from the sub I could just delete a text file.
How would one begin this crazy journey?
Thanks
That isn't really something you would want to do. Its also not possible since vb.net is a compiled language it can't just read the text of the code on the fly and implement it like that.
You are better off investigating another pattern that will meet your needs.
Actually you can programmatically create a vb.net program on the fly. I created a web program which rewrites itself as I see fit. Part of that code adds new subroutines to the same vb.net program. Basically writing more sub routines to itself and then it runs itself again. You can easily store your other subs into txt files and then recall the data later if you wanted. The trick here, however, is that you have to first add the sub routines into an entirely new file, then, when all the writing is done, you can then preform the following:
File.Delete(Server.MapPath("your old file name"))
File.Copy(Server.MapPath("your new file name"), Server.MapPath("your old file name"))
It should be noted that I make web applications, so it doesn't quite work the same. Using asp.net pages, the way I get the new program to run with the new sub routines is to run on the client side code, in which I include a little timed refresh, which fires once the rewriting task is finished. The page then refreshes with the new vb.net back end code in place.
In order to do this for a desktop application or whatever might require something else which I am not aware of.