Question Communicating with two forms

donnex777

New member
Joined
Dec 20, 2013
Messages
3
Location
london united kingdom
Programming Experience
Beginner
Good day

I am a newbie in vb.net and I am trying to create a database program. Moving on to my inquiry, I am having trouble getting the value of a combobox1 from formA and passing it on two form b. All help will be very much appreciated.

my code looks like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim w As New Wholesale
            'w.WholesaleTA.Insert(w.ComboBox1.Text,w.DateTimePicker1.Text,w.TextBox3.Text)
            Dim name As String
            name = w.ComboBox1.Text
            Dim day As Date
            day = w.DateTimePicker1.Text


every time I run this code variable name and day always appears empty even though there is something written in combobox1 and datetimepicker1 in wholesale tables

In addition I would also like to pass to the dgv of wholesale form the additional information. Which also to my demise is not happening every time button 1 is triggered. the continuation of the code is as follow:


Dim subtotal As Double
            subtotal = TextBox1.Text * TextBox3.Text
            TextBox4.Text = FormatNumber(subtotal, 2, , , TriState.True)
            Dim pt = ProductTA.GetDataByproductname(ComboBox1.Text)
            Dim p As COLONELSACCESS.CODS.productdatabaseRow = pt.Rows(0)
            Dim lt = LotnoTA.GetDataByproductid(p.productid)
            Dim l As COLONELSACCESS.CODS.lotnoRow = lt.Rows(0)
            w.Dgv.Rows.Add(p.productid, l.lotnumber, p.productname, TextBox1.Text, TextBox3.Text, TextBox4.Text, l.deldate, l.expdate, l.supplier, l.discount)


            Dim i As Integer
            For i = 0 To w.Dgv.Rows.Count - 1
                Dim dgvsubtotal As Double
                dgvsubtotal += w.Dgv.Rows(i).Cells(5).Value
                w.TextBox3.Text = FormatNumber(dgvsubtotal, 2, , , TriState.True)
            Next


            
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
        End Try
    End Sub


Please help. my code use to work in vb 2008 and i dont know why it is not working in vb2010. id really appreciate any help
 
First, I would recommend you use "Option Strict On". It will save you headaches in the long run.

I will give you some generic ways to share data between forms. Each has its advantages and disadvantages:

- Use properties and/or methods. Forms are classes. You can use them just like any other class. If you want to pass data to a class, you usually do it through a public property or method.
- Make the controls on the 2nd form public. Then just access the data in the controls directly. This is probably the simplest way to do it but it exposes your data.
- Use an intermediate class. If you are creating a multi-tiered application, then this is what is usually done. The forms are in the presentation layer. The business algorhyms are in classes in the business layer and the data is kept in a database or class the data layer.

Personally, I prefer to use an intermediate class. That way the data class can manipulate the data internally and only exposes the bare minimums externally. If anything goes wrong, it makes debugging easier.
 
So how do i basically do that...? Im really just new in programming and just trying to learn everything through the web...if you could help me i would really appreciate it...if you could tell me step by step how to do it that would be great
 
The problem is that you're creating a new Wholesale instead of using the existing one. If you wrote something on a piece of paper and then you got a new piece of paper, would you expect to be able to read what you wrote on the other one? Of course not, and this is the same. If you make a selection in a ComboBox on one Wholesale form and then you create another Wholesale form, why you expect to be able to get the ComboBox selection from the new one? I can assure you that that code did not work in VB 2008. Whatever you had in VB 2008 that worked was different to that.

If you want to learn how to manage data amongst multiple forms then I suggest that you follow the link in my signature below to that effect.
 
I believe you would be best served by learning some of the fundamental programming techniques rather given a quick answer. You will understand how things work better and will develop good programming habits.

4 major principles of Object-Oriented Programming | Raymond Lewallen

I recommend you use Encapsulation and Abstraction mentioned in the article above.

If you check out the link I suggested and read my three-part blog on the subject, I think you'll agree that it's not just "a quick answer". Your point is well taken though.
 
Back
Top