im just wondering on how can i pass value of a variable to another form from a different form
I have a form called frmSearch then I have another form called frmMain from frmSearch i have a varible A which is publicly declared and i wanted the value of A to frmMain i've tried the following
frmMain
dim B = frmSearch.A
but everytime i checked the value of B it always returns a blank string
also when i checked the value of frmSearch.A it also returns nothing even though it returns a value when i checked it in frmSearch
please help im really stuck
thanks in advance
Accessing a field of a form shouldn't be a problem. Just make sure that at the time you are reading the field it is already assigned.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim form = New Form2
Debug.Assert(form.A Is Nothing)
form.ShowDialog()
Debug.WriteLine(form.A(0))
End Sub
End Class
Public Class Form2
Public A As String()
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
A = {"abc", "aaa"}
End Sub
End Class
Related
I wrote a code to demonstrate the issue:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
doSomething()
End Sub
Dim controlArr() As Object = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4, CheckBox1, CheckBox2, CheckBox3, CheckBox4}
Private Sub doSomething()
Dim testStr As String = ""
For Each control In controlArr
Select Case control.GetType
Case GetType(NumericUpDown)
control.value = 1
Case GetType(CheckBox)
control.checked = True
End Select
Next
End Sub
End Class
When I run the code I receive Null Referenece Exception "Object reference not set to an instance of an object", the error disapears when I declare the controlArr array inside DoSomething subroutine. Anyway I would prefer having it declared outside since I am using it in many functions. I would like to understand it better so if you provided me with a topic I could read up on I would be very grateful. Thank you very much for your help.
The issue is that declarations are processed before the constructor. That means that this line:
Dim controlArr() As Object = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4, CheckBox1, CheckBox2, CheckBox3, CheckBox4}
is executed before the code that creates all the controls on the form and assigns them to those fields. As such, all the fields are Nothing at the time that code is executed and so your array contains a whole lotta nothin'. There's no issue creating objects to initialise fields like that and that code does successfully create an array. It's just that you are implicitly setting every element of that array to Nothing so that's what you get to use later on.
If you want to reference any control then you have to wait until after the form's controls are created. That means, at the earliest, after the call to InitializeComponent in the constructor. More generally, you should do it in the Load event handler, e.g.
Dim controlArr As Object()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Form1.Load
controlArr = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4, CheckBox1, CheckBox2, CheckBox3, CheckBox4}
End Sub
I'm writing a program to be used by my library as a step by step checklist when adding new materials (books) to the collection.
Option Explicit On
Option Strict On
Public Class frmCircCounter
Public ReadOnly Property Items As CheckedListBox.ObjectCollection
'confirms all boxes have been checked, and clears them
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'If
' End If
End Sub
Private Sub CheckedListBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox4.SelectedIndexChanged
'I'm not sure what this is for the internet told me to add this?
InitializeComponent()
'establishes the arrary displayed in the checklistbox
Dim strProperPackage() As String = {"Call Number and Authors Last name?", "Sub-Category Sticker?", "Plastic Wrapping on the Cover if needed?"}
'displays it... or it should!!!?
clbProperPackage.Items.AddRange(strProperPackage)
End Sub
End Class
I expect to have the array displayed in the CLB upon execution
Try looping through your array and add each item into it.
For Each item As String In strProperPackage
clbProperPackage.Items.Add(item)
Next
Your script will not execute the correct way because your using selected index changed but this will only run when a item has been selected in the list.
The best way to test this is by creating a new button and assigning this code to that button. With the button, the script will execute on click.
Public sub btnAccept (sender as object, e and eventargs) handles btnAccept.click
Dim strProperPackage() As String = {"Call Number and Authors Last name?", "Sub-Category Sticker?", "Plastic Wrapping on the Cover if needed?"}
'displays it... or it should!!!? clbProperPackage.Items.AddRange(strProperPackage)
End Sub
I have attached code for reference. I am trying to create database for my project details. I have currently 2 forms:
main form
project detail form
Main form used to switch various forms
Project detail forms are meant for add / edit / load project details
My main form in visual basic look like this.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Bt_Project_Details_Click(sender As Object, e As EventArgs) Handles Bt_Project_Details.Click
Me.Hide()
Project_Details_Form.Show()
End Sub
End Class
My project detail form look like this.
Public Class Project_Details_Form
Private Sub Project_Details_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Project_Data_Set.Project_Data_Table' table. You can move, or remove it, as needed.
Me.Project_Data_TableTableAdapter.Fill(Me.Project_Data_Set.Project_Data_Table)
End Sub
Private Sub Bt_Load_Project_Click(sender As Object, e As EventArgs) Handles Bt_Load_Project.Click
End Sub
Private Sub Bt_Cancel_Click(sender As Object, e As EventArgs) Handles Bt_Cancel.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Project_Data_TableBindingSource.AddNew()
End Sub
Private Sub Project_Data_TableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Project_Data_TableBindingNavigatorSaveItem.Click
Me.Validate()
Me.Project_Data_TableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Project_Data_Set)
End Sub
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
Project_Data_TableBindingSource.RemoveCurrent()
End Sub
Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs)
Try
Me.Project_Data_TableTableAdapter.FillBy(Me.Project_Data_Set.Project_Data_Table)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Now I am facing a few problems:
Whenever I run the program, try to enter first entry data being saved but with empty value. The data enter next time being stored properly. How can make my first entry visible?
In below code:
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Project_Data_TableBindingSource.AddNew()
End Sub
I am using AddNew(). This working fine, but problem is even I don't enter data like date, name kept empty, data recorded in their position is store empty only. I would like to say if any of project data set is empty give error message (since it mandatory for user to enter those data)
Is there any other method where i can assign value individually .
Like my Project database contain project name, customer name etc. if i say new button pressed clear all field. Once form filled individual value being record once save button pressed
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Project_Data_TableBindingSource.AddNew()
End Sub
This is my first post on stackoverflow so please hang with me. I am attempting to update an access database using VB.NET. Using the following series of videos (starting with this one), we have been able to properly use our form to save new data into the access database.
https://www.youtube.com/results?search_query=vb.net+ms+access+database+tutorial+1+%23+add+new+remove+save+data+in+database+using+vb.net
However, when we start up the form again after opening up access to make sure the updates have occurred (which they always do), none of our changes are there. And when we open up access again the changes we made that showed up in the access database are gone, and the access database goes back to its original state (the state it was when we linked it to visual studio in the first place).
Below is the code we have so far, we appreciate any help you can give us!
Thanks.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Sheet1BindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Sheet1BindingNavigatorSaveItem.Click
Me.Validate()
Me.Sheet1BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(MIS275_Small_BusinessDataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MIS275_Small_BusinessDataSet.Sheet1' table. You can move, or remove it, as needed.
Timer1.Start()
Me.Sheet1TableAdapter.Fill(MIS275_Small_BusinessDataSet.Sheet1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Sheet1BindingSource.AddNew()
End Sub
Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
Try
Sheet1BindingSource.EndEdit()
Sheet1TableAdapter.Update(MIS275_Small_BusinessDataSet.Sheet1)
MessageBox.Show("Data Saved")
Catch ex As Exception
MessageBox.Show("Error")
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim count As Integer
count = Sheet1BindingSource.Count
End Sub
End Class
Am still learning VB and database but I have a little problem. My codes works but when I
try to look into the database I don't find all what are been saved, how do I rectify it? and also the edit and delete. thanks
Here is my code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
'TODO: This line of code loads data into the 'ItemsDataSet.Items' table. You can move, or remove it, as needed.
Me.ItemsTableAdapter.Fill(Me.ItemsDataSet.Items)
End Sub
Private Sub ItemsDataSetBindingSource_CurrentChanged(sender As Object, e As EventArgs)
End Sub
Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click
ItemsBindingSource.EndEdit()
ItemsTableAdapter.Update(ItemsDataSet.Items)
MsgBox("Saved")
End Sub
Private Sub btadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
ItemsBindingSource.AddNew()
MsgBox("Added Successfully")
End Sub
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
ItemsBindingSource.RemoveCurrent()
MsgBox("Item Deleted")
End Sub
Private Sub btnexit_Click(sender As Object, e As EventArgs) Handles btnexit.Click
Close()
End Sub
Private Sub btnprevious_Click(sender As Object, e As EventArgs) Handles btnprevious.Click
ItemsBindingSource.MovePrevious()
End Sub
Private Sub btnnext_Click(sender As Object, e As EventArgs) Handles btnnext.Click
ItemsBindingSource.MoveNext()
End Sub
End Class
You probably have your MDB (ACCDB) file included in your project files.
If you check the properties associated with this project file you will find one named Copy to Output Directory. Set this to Copy Always.
Also your connection string contains the shortcut Data Source = "|DataDirectory|\yourdb.mdb"
If this scenario is correct, then you start your application and the MDB / ACCDB file is copied from the original location to the BIN\DEBUG directory.
You insert your data there without error. When you stop the debug session to fix errors or other problems you restart your application and the fresh copy (but empty) of your db is copied again in the output directory.
To fix this, set the property to Copy Never or change your connection string to point to a fixed location.