Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

Yesterday I asked a question about Modules with Visual basic and WPF but still haven't found an answer to my question. I am currently working on a computing project in which data must be entered in from a "Form" to a database. My project is in Visual Basic as a WPF Application. The reason I have used WPF is so that I can create different styled forms and Login screens. I attempted to set up 2 WPF Windows with Textboxes on the first one (Window1) and Labels on the second (Window2). I kept getting a number of errors every time, so as an experiment I created a fresh new project under Windows Forms rather than a WPF Application. In [Form 1] I added a textbox called [Textbox1] and a button called [btnSend]. Here is my code for [Form 1]:
VB
Public Class Form1
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Form2.Show()
    End Sub
End Class


Very simple, All it does is open up the Second Form on the click event of the button.

On [Form 2] I have a label called [label1]. (If your wondering why I left the names as there default, its to keep everything simple.) So heres the code for [Form 2]:

VB
Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = Form1.Textbox1.Text
    End Sub
End Class


So what [Form 2] does, is simply when loaded make [label1] equal to [textbox1] on [Form 1]. I compiled the application and it worked fine. Typed random things into the textbox and once I clicked the button the second form loaded up and displayed whatever I had typed into the textbox on the first form.

So I thought to myself, "Ok that works, now I will create a fresh new WPF Application, create a WPF Window and a Form and copy the code in, tweak any names so that everything is linked up correctly....". No. I now get the error message:

Error 1 Reference to a non-shared member requires an object reference.

Which corresponds to the the line
VB
Form2.Show() 
on [Form 1]. I also get the same error message for
VB
Label1.Text = Form1.Textbox1.Text
in [Form 2].

Just to make sure it wasn't something to do with the change from a WPF Window to a standard form. I create a fresh new project and create 2 standard Forms and tried it again. Same error messages. Does anyone know why I'm getting error messages in a WPF Application but not in a Windows Standard Form? Can anyone help me with some examples using Visual Basic in a WPF Application as I have been searching for days and have found nothing.

Thank you
Posted

1 solution

Without a look at your XAML it is hard to judge what is wrong.
I would suggest you take look at MVVM pattern (Google is your friend). It solves very same problem as yours: allow views bind to and share same data (ViewModels).
It also explains how to bind your UI controls to data so it could be displayed and updated in your views.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900