how to put the data with combobox to another form? - vb.net

I am doing a payment system and what I really want to happen is that when I select the payment type(full or installment) the data that I inputted will show on another form.
For example, after I filled up my information and selected the payment method, and after I saved it, if my payment method is "full", the information will appear on the "full_pay" form or if I selected "installment_pay" the information will appear on the "installment" form.
if paytype.text="Full" Then
'the info will go to the full_pay form

you can write propertys in your new form and pass them from your old form
'Code on Main form to open form and Set parameters
Dim frmFull_pay As New full_pay
frmFull_pay.paytype = paytype.text
frmFull_pay.show()
'Code on full_pay
Private _paytype As String
Public Property paytype As String
Get
Return _paytype
End Get
Set(ByVal Value As String)
_paytype = Value
End Set
End Property
Now you can access all the data you set on Porpertys

Related

ToString not updating on object when altering ListBox in VB

I have a form that alters the content of a class within a list box. The information is updated correctly, but my ToString override on my object doesn't refresh - meaning the old ToString doesn't change. How would I fix this?
Here's my object:
Public Class Destination
Public strDestinationName As String
Public strAddress As String
Public intQuality As Integer
Public intPrice As Integer
Public Overrides Function ToString() As String
Return strDestinationName
End Function
End Class
Here's the code where it should be updated
Dim selectedDestination As Destination
selectedDestination = CType(ListForm.lbNames.SelectedItem, Destination)
selectedDestination.strDestinationName = tbName.Text
selectedDestination.strAddress = tbAddress.Text
selectedDestination.intPrice = cbPrice.SelectedIndex
selectedDestination.intQuality = cbQuality.SelectedIndex
Me.Close()
Regardless of how you add items to a ListBox, it is the ListBox that actually displays the data. In your case, it appears that you are adding Destination objects to the ListBox somehow, given that the SelectedItem is a Destination object. Given that you have written that ToString method, you are presumably relying on that to produce the text that the ListBox displays for each item. You are now expecting to be able to change the value of the strDestinationName field of one of the items and have the ListBox reflect that change. How exactly do you think that is going to happen?
The ToString method has to be called in order to get the new value and who do you think is going to call it? It would be the ListBox that calls it because it is the ListBox that displays the result. When you change that field, you are expecting the ListBox to call your ToString method but why would it do that? What reason has the ListBox got to call that method? It has no knowledge of the change you made so why would it think that it has to get new data?
The solution to your problem is to change your code in some way to notify the ListBox that data has changed so that it knows that it needs to get that new data and display it. There are multiple ways that you could do that.
The simplest option would be to bind your data to the ListBox via a BindingSource and then, when you modify an item, call the ResetItem method or similar of the BindingSource. That will raise an event that is handled by the ListBox and the ListBox then knows that it needs to refresh the data for that item. That is what will prompt the ListBox to call your ToString method and get the new data to display. You would add the BindingSource to the form in the designer and then do the binding where you are currently adding the items, e.g.
Dim destinations As New List(Of Destination)
For i = 1 To 10
Dim d As New Destination
d.strDestinationName = "Destination " & i
destinations.Add(d)
Next
destinationBindingSource.DataSource = destinations
destinationListBox.DataSource = destinationBindingSource
The modification would look something like this:
Dim selectedDestination = DirectCast(destinationBindingSource.Current, Destination)
selectedDestination.strDestinationName = "New Destination"
destinationBindingSource.ResetCurrentItem()
The Current property returns the item currently selected in the bound UI and the ResetCurrentItem method notifies the bound UI to refresh the display of that item.
This is really not the best way to go about it though, given that you have control over the item type. What you ought to do is implement the type using properties rather than fields, get rid of the ToString method that only returns the value of one property and then add a change event to that property:
Public Class Destination
Private _destinationName As String
Public Property DestinationName As String
Get
Return _destinationName
End Get
Set(value As String)
If _destinationName <> value Then
_destinationName = value
OnDestinationNameChanged(EventArgs.Empty)
End If
End Set
End Property
Public Property Address As String
Public Property Quality As Integer
Public Property Price As Integer
Public Event DestinationNameChanged As EventHandler
Protected Overridable Sub OnDestinationNameChanged(e As EventArgs)
RaiseEvent DestinationNameChanged(Me, e)
End Sub
End Class
You can now bind a list of Destination objects directly and specify any of those properties as the DisplayMember to have that property value displayed:
Dim destinations As New List(Of Destination)
For i = 1 To 10
Dim d As New Destination
d.strDestinationName = "Destination " & i
destinations.Add(d)
Next
destinationListBox.DisplayMember = "DestinationName"
destinationListBox.DataSource = destinations
You don't need the ToString method because the DisplayMember specifies that the value of the property with that name should be displayed. When you modify the value of the DestinationName property of an item, it will raise the DestinationNameChanged event and that will notify the ListBox that it needs to refresh the display for that item, so you don't need any additional code to make the ListBox update.
That's fine if you only plan to modify existing items. There's still a problem if you want to add and/or remove items after binding though. The List(Of T) class that is used to bind the items to the control in this example does not have any events to notify the control of changes to the list like that. In that case, you can use a BindingSource again if you want. If you add and remove items via the BindingSource then it will raise that appropriate events and the ListBox will update. If you wanted to add and remove via the underlying list then you'd have to call an appropriate method of the BindingSource when you made a change.
An alternative would be to use a BindingList(Of Destination) instead of a List(Of Destination). As the name suggests, the BindingList(Of T) class is made for binding, so it will automatically raise the appropriate events when the list changes to enable the UI to update without extra code from you. Using the combination of property change events in your item class and a BindingList(Of T), you can add, edit and remove items in the bound list and the UI will reflect those changes automatically.

How to set a variable to a field value retrieved in a query

Updated 3/14/19 to include code for setting variable and Run-time error is '2427' 'You entered an expression that has no value.' on setting outCome1 variable
Private Sub Report Open(Cancel As Integer)
Dim outCome1 As String
' Set public variable to true to indicate that the report
' is in the Open event
bInReportOpenEvent = True
MsgBox (bInReportOpenEvent)
' Open Appt Query Dialog
DoCmd.OpenForm "craid CMM Client Report Dialog", , , , , acDialog
Cancel Report if User Clicked the Cancel Button
If IsLoaded("craid CMM Client Report Dialog") = False Then Cancel = True
outCome1 = (Reports("CMM Client Status Report").Controls("googleoutcome").Value)
MsgBox (outCome1)
' Set public variable to false to indicate that the
' Open event is completed
' bInReportOpenEvent = False
End Sub
Also, I notice when I run that bInReportOpenEvent does not stay True when in Dialog Form and so it doesn't appear to be public even though I have run Sub Report Open with Public and it still doesn't stay True when in Dialog Form.
I am new, but have been struggling quite a bit with trying to set a variable to a field value retrieved in a query, so thought I would see if I can get some relief.
I am writing an Access Report that uses a Dialog Form to prompt user for which record in database to display on Report.
I use a query as the Record Source for the report. One of the fields I retrieve with the query is googleoutcome. I want to set a variable to the value of googleoutcome, so I can then do a test on it and display the result on my report.
I have tried:
Dim outCome1 As String
outCome1=Me!googleoutcome
Dim outCome1 As String
outCome1=[googleoutcome]
Dim outCome1 As String
outCome1=Me.googleoutcome
I am receiving the error:
error '2427; You entered an expression that has no value.
Any help is appreciated.
Laura
In VBA, the Me keyword refers to an instance of the class in which the current code is executing. For example, in MS Access, if you use Me within a Form Module, it will return an instance of the active Form; if you use it within a Report Module, it will return an instance of the active Report.
Therefore, your use of Me in the On Open event handler for the Report is returning an instance of the Report which is being opened, not the Form you have opened.
Instead, you could reference the value of the form control by traversing the object model in the following way:
Forms("YourFormName").Controls("YourControlName").Value

Passing Array to Another Form (vb.net)

I got a form which declared an array in public,
Public requestedqueue() As Integer
I would like to pass the array to form3 and perform other calculations there, but how?
I tried doing(at a new form):
public newrequest() As Integer
newrequest = form2.requestedqueue
I tried to show it at a new form by doing:
TextBox1.Text = = String.Join(",",form2.newrequest)
But whenever I run into form3 it would say the newrequest is null.
But it shows as an array in form2, Im so confused.
I'm not sure what you mean by
But it shows as an array in form2
but newrequest will be Nothing because you have set it equal to the value of requestedqueue which is Nothing until you populate the array with some values.
If you had Public requestedqueue() As Integer = {1, 2, 3} then you would not encounter the error.
One way to pass data to a form is to add a property to the second form.
Lets say you have a form called Form3 and this is your form's code. In the code you will need to declare an array of integer to hold the passed data, and also declare a public property so you have a way of passing the array
Public Class Form3
Dim requestedqueue() As Integer
Public Property ArrayParameter As Integer()
Get
Return arrayData
End Get
Set(value() As Integer)
arrayData = value
End Set
End Property
End Class
Then, to pass the data from Form1, in form1, you would simply use
Form3.ArrayParameter=requestedqueue()
to set the parameter.
and if you wish, you can show the form as normal, or if the form is already visible, you can process the code using button clicks etc.
If you want to process the data in an already open form immediately without any user interaction, you can write a Procedure that does the processing and include that in the Set portion of your property.
For example. If you want to add all the elements of the array to a ListBox called ListBox1 in Form3, you could write a procedure like this..
Private Sub AddDataToListbox()
ListBox1.Items.Clear()
For Each item As Integer In requestedqueue
ListBox1.Items.Add(item)
Next
End Sub
and change your Form3.ArrayParameter code to this
Public Property ArrayParameter As Integer()
Get
Return requestedqueue
End Get
Set(value() As Integer)
requestedqueue = value
AddDataToListbox()
End Set
End Property

VB.NET Adding list of property in Designer to choose from

First excuse my my english, I'm french.
I have made a control (a class in fact) inherited from textbox.
The purpose of this class is simply to keep the textbox aligned above a column of a datagridview.
All is working correctly. But I have a question :
I made a datagridview property, and in design time, I can simply select it in a combobox listing the datagridview of my form.
But I have also a columnName property as a string. I would like that, once the datagridview is selected, the designer propose me the list of the datagridviewcolumn name in this property.
I have absolutly no idea how to achieve that. I'm a very beginner in code, and have made no studies on it.
I tried making the string property into a datagridviewcolumn property, but not only did the designer do not propose me the ones available, but also it crashes and I have to remove all about this textbox in the file.Designer.vb.
Thank you for your help.
EDIT : Here is the code :
Public Class TextBoxFilter
Inherits TextBox
Private WithEvents _DGV As DataGridView = Nothing
Public Property DGV As DataGridView
Get
Return _DGV
End Get
Set(value As DataGridView)
_DGV = value
End Set
End Property
Private _ColumnName As String = String.Empty
Public Property ColumnName As String
Get
Return _ColumnName
End Get
Set(value As String)
_ColumnName = value
End Set
End Property
The rest of the code is irrelevant. Just know that it features event (when datagridviewcolumns are move, not displayed, not visible) to adjust the textbox.
To answer Plutonix, I would like to be able to give the designer a list of column, even if there is several datagridview in the form, and even if it can't find them automatically. I suppose I could do that in the set of the datagridview ? But I don't know how.

Invalid cast exception

I have a simple application to store address details and edit them. I have been away from VB for a few years now and need to refreash my knowledge while working to a tight deadline. I have a general Sub responsible for displaying a form where user can add contact details (by pressing button add) and edit them (by pressing button edit). This sub is stored in a class Contact. The way it is supposed to work is that there is a list with all the contacts and when new contact is added a new entry is displayed. If user wants to edit given entry he or she selects it and presses edit button
Public Sub Display()
Dim C As New Contact
C.Cont = InputBox("Enter a title for this contact.")
C.Fname = frmAddCont.txtFName.Text
C.Surname = frmAddCont.txtSName.Text
C.Address = frmAddCont.txtAddress.Text
frmStart.lstContact.Items.Add(C.Cont.ToString)
End Sub
I call it from the form responsible for adding new contacts by
Dim C As New Contact
C.Display()
and it works just fine. However when I try to do something similar using the edit button I get errors - "Unable to cast object of type 'System.String' to type 'AddressBook.Contact'."
Dim C As Contact
If lstContact.SelectedItem IsNot Nothing Then
C = lstContact.SelectedItem()
C.Display()
End If
I think it may be something simple however I wasn't able to fix it and given short time I have I decided to ask for help here.
I have updated my class with advice from other members and here is the final version (there are some problems however). When I click on the edit button it displays only the input box for the title of the contact and actually adds another entry in the list with previous data for first name, second name etc.
Public Class Contact
Public Contact As String
Public Fname As String
Public Surname As String
Public Address As String
Private myCont As String
Public Property Cont()
Get
Return myCont
End Get
Set(ByVal value)
myCont = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Cont
End Function
Sub NewContact()
FName = frmAddCont.txtFName.ToString
frmStart.lstContact.Items.Add(FName)
frmAddCont.Hide()
End Sub
Public Sub Display()
Dim C As New Contact
C.Cont = InputBox("Enter a title for this contact.")
C.Fname = frmAddCont.txtFName.Text
C.Surname = frmAddCont.txtSName.Text
C.Address = frmAddCont.txtAddress.Text
'frmStart.lstContact.Items.Add(C.Cont.ToString)
frmStart.lstContact.Items.Add(C)
End Sub
End Class
You're doing
frmStart.lstContact.Items.Add(C.Cont.ToString)
That is, you are adding strings to lstContact. Of course lstContact.SelectedItem() is a string!
Since you didn't post all of your code, I'll give you my best guess which is:
lstContact seems like it is bound to an IEnumerable of strings instead of IEnumerable of Contacts.
Therefore, when you get lstContact.SelectedItem() it is returning a string instead of a Contact (and throwing an error when trying to cast string as Contact).
It'd be more helpful if you could post the code bit that binds lstContact to a data source.