VB.net use custom control - vb.net

I am trying to use the custom GenericDatagridview
(which I got here)
I have added the dll to my toolbox. It is also referenced in my project properties. However when I run my application. The form with the new control gives the following error : The error is: Object reference not set to an instance of an object
The code for the form load is :
Imports GenericDataGridView
Public Class GenericDGVTRASHFRM
Private Sub GenericDGVTRASHFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SkeduleringsDatabasis6DataSet.Blokkeklaar' table. You can move, or remove it, as needed.
Me.BlokkeklaarTableAdapter.Fill(Me.SkeduleringsDatabasis6DataSet.Blokkeklaar)
End Sub
End Class
Regards.

Related

WinForm ".Show()" method doesn't show up in Visual studio

I am pretty a beginner in using visual studio so I may missed a lot of things. I want to create a custom group in MS Project with a button which I want when I click on it, it would open up a WinForm with some buttons on it. So I created a VSTO project for MS project then added a Ribbon(Visual designer) and a form.
My custom group in Task tab created with a button in it. I double clicked on this button to jump right into it's click handler code, then tried to write a simple code to show my form but it seems it lacks something because intellisense doesn't show up .Show() method and it can not be built. The error I got when I try to build is this:
error BC30469: Reference to a non-shared member requires an object reference.
My form label is Form1 and the simplest code which I wrote in my button click event handler is as follow:
Imports Microsoft.Office.Tools.Ribbon
Imports System.Windows.Forms '(I added this line after the hints)
Public Class Ribbon1
Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e
As RibbonUIEventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As
RibbonControlEventArgs) Handles Button1.Click
Form1.Show() '(The Line which error occurs)
End Sub
End Class
First of all, you need to check references of your add-in project. By default VSTO add-in projects don't include the System.Windows.Forms assembly reference. If the reference is not there, you need to add it manually. Then in the code you need to use the fully qualified namespace or include it in the beginning of the code file (by using the Imports keyword).
To be able to call the Show method you need to create the Form1 instance in the code first. The method is not static, so you can't invoke it on the class declaration. You must have an object reference.
The code may look like that:
Private Sub Button1_Click(sender As Object, e As
RibbonControlEventArgs) Handles Button1.Click
Dim f as Form1 = new Form1()
f.Show()
End Sub

VB.Net Code in class and WithEvents issue

I've created a VB.Net application which consists of a form and a series of classes. One class (GUI) contains all of the routines that handle the logic associated with the controls on my form. These controls are placed at design time.
I've having a problem with a couple of the timers on the form. They are declared in the GUI class thus:
Private WithEvents timerScreenUpdate As Timer
Private WithEvents timerDebug As Timer
but I'm getting an error in this line:
Private Sub timerScreenUpdate_Tick(sender As System.Object, e As System.EventArgs) Handles timerScreenUpdate.Tick
The error is "Event 'Tick' cannot be found". I'm getting the same error for the other timer.
It's probably related but I'm also getting a similar error in this line:
Private Sub Monitor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
which is "Event 'FormClosing' cannot be found".
How can I make these events visible to my class ?
Make sure that the reference and the Namespace-Import of System.Windows.Forms is available in your project respectively class file.
Without it, VB will use a Timer from the namespace System.Timers which you don't want to use in your case.

Embed database in vb.net app

I'm building a small VB.net app. I want to add a little database (about 5 columns, 20 records). I want to keep everything in a single exe. I think it's a bit overkill to add a 'full' database, so I'm looking for an alternative.
I could create a CSV file, and add it as a resource. Is this a good idea, or are there any other better alternatives?
Another option for such a small amount of data is to store it in ApplicationSettings. Your question implies you are using WinForms, so you can make use of the built-in features with just a small amount of work to store your own custom class.
Create a class to represent your data.
Wrap that class in a property of another class that inherits from ApplicationSettingsBase as a List(Of )
Manipulate this custom setting as needed and call Save() as needed.
Here is an example that binds to a DataGrid:
The class that represents your data:
Public Class Fruit
Public Property FruitName As String
Public Property FruitColor As String
Public Property FruitGrowsOn As String
End Class
The class that turns Fruit into a collection stored in application settings. Notice it inherits ApplicationSettingsBase. Also notice the attributes on the Fruits property that identify this as a user setting as opposed to an application setting (which cannot be modified by the user). The DefaultSettingAttribute makes sure the collection is instantiated so you don't get null reference exception until after the first time you add an item:
Imports System.Configuration
Public NotInheritable Class FruitCollection
Inherits ApplicationSettingsBase
<UserScopedSettingAttribute()>
<DefaultSettingValue("")>
Public Property Fruits() As List(Of Fruit)
Get
Fruits = Me("Fruits")
End Get
Set(ByVal value As List(Of Fruit))
Me("Fruits") = value
End Set
End Property
End Class
The Form definition. Retrieves the instance of your custom setting (FruitUserSettings), creates a binding source for a DataGridView, and provides a Save button to persist the changes made in the grid to Settings. Next time the user opens the form the changes will still be there provided they clicked the Save button:
Public Class Form1
Dim FruitUserSettings As FruitCollection
Dim GridBindingSrc As BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FruitUserSettings = New FruitCollection()
GridBindingSrc = New BindingSource(FruitUserSettings, "Fruits")
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = GridBindingSrc
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FruitUserSettings.Save()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
GridBindingSrc.Dispose()
End Sub
End Class
Note, you don't need a binding source or grid, these were just for demonstration. You can manipulate FruitUserSettings.Fruits like any other list in any way you want. As long as Save() is called on the settings you will retain the data.
You can download/clone the working sample here: https://github.com/crowcoder/CustomSetting
i would use XML file as little database, you can query it easily with linq (Language-Integrated Query). also there are built in library's that can help you handle you records and query's. of course that you can use access, excel (you can query excel with SQL) csv or txt file . also you can create a local data base file in visual studio

Create And Implement a Custom Form in VB.NET

I am trying to create a customized form class (CustomBorderlessForm) in VB.NET.
My Progress So Far
I created a new Class and named it CustomBorderlessForm.vb
I then proceeded to write the following code:
CustomBorderlessForm.vb
Public Class CustomBorderlessForm
Inherits Form
Dim _form As Form = Nothing
Public Sub New(form As Form)
_form = form
MsgBox("Testing: New()")
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
MsgBox("Testing OnMouseMove()")
End Sub
End Class
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
End Sub
End Class
Results of progress
A message box displays "Testing: New()" on load
Nothing shows on mouse move
As you can see, my problem lies with the events
Questions
Is it possible to create a form object and use that instead of the pre-populating form?
If so, can I give this form custom properties, such as, a border and some boolean values (shadow...etc), just like any other custom object/class?
What am I doing wrong in my current approach?
Why isn't the OnMouseMove being overridden?
Am I initialising the class wrong?
Can it even be done this way?
After creating a form you also need to show it. Change your logic to:
Dim form As New CustomBorderlessForm(Me)
form.Show()
Before you do that, I'd recommend changing from MsgBox to Console.WriteLine(), otherwise you can run into a fun/frustrating little cat and mouse game.
EDIT
Based on the comments, if, from VS you did a "Add New, Windows Form" you can just right-click the project, select property and on the Application tab change the Startup object to your new form. VS only allows you to do this with forms it creates for you (by default, more on this later).
If you wrote that file by hand (which is absolutely fine) you can perform the Show() like I did above and call Me.Hide() to hide the "parent" form. Unfortunately the Load event is fired before the Show event so if you place this in Form1_Load() it won't work. Instead you can use the Shown event like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
form.Show()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Hide()
End Sub
Another option has to do with "Application framework". You can read about it here however it basically handles application events that other languages have to manually implement. If you go into your project properties you can uncheck the "Enable application framework" checkbox. This will give you more option in the "Startup object" dropdown. If you add the following code to your project one of the items in the Startup object dropdown menu should now be "Loader"
Public Module Loader
<STAThread()>
Public Sub Main()
Dim form As New CustomBorderlessForm(Nothing)
form.ShowDialog()
End Sub
End Module
You'll notice that the above bypasses Form1 completely. Also, instead of Show() I'm using ShowDialog() because otherwise the form shows and then the program ends.

How to use the Show Event in VB.net

I try tu use this msdn snipped to execute some code right after my form loads:
Private Sub Form1_Shown(sender as Object, e as EventArgs) _
Handles Form1.Shown
Some Code
End Sub
But it seems that I am missing something. I get an errormessage that translated sounds like this:
The Handle requiere an WithEvents-Variable, which is defined in the contained type or its basis class... My Form is named Form1 so that should be ok. the error is marked in the second line of the code. Any ideas?
Instead of:
Handles Form1.Shown
do this:
Handles Me.Shown
Usually that is the sort of error you would get if you create the form in code and not in the designer. The designer will automatically declare the generated form as WithEvents. If you create the Form in code instead you have to declare it as WithEvents.
For example:
Public Form1 as frmMain
Would generate that error unless you add the handler yourself.
AddHandler Form1.Shown, AddressOf Form1_Shown
If you do this instead:
Public WithEvents Form1 as frmMain
wouldn't generate the error.
WithEvents is necessary on any object created if you want to use the handles clause in that way.