Jump to content

Need help with VB

JT8101997

I'm making a game for my final project and i need some help. I need to figure out how to access data across forms. like say on form1 i want to set a labels text equal to whatever another label equals on form2. How would i do this? My reason for asking is that i need to make a score board and i also wanted to set some options for light and dark themes and stuff like that, that would have to be applied to all levels from the main menu. Any help is appreciated. Thanks guys :)

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm making a game for my final project and i need some help. I need to figure out how to access data across forms. like say on form1 i want to set a labels text equal to whatever another label equals on form2. How would i do this? My reason for asking is that i need to make a score board and i also wanted to set some options for light and dark themes and stuff like that, that would have to be applied to all levels from the main menu. Any help is appreciated. Thanks guys :)

 

If it's VB.NET I'm pretty sure you can simply type in the form1 code:

 

Form2.Label1.Text = "Text here"

 

(I'm sorry if the above syntax is wrong, I haven't programmed in VB for a while...)

Please tell me if its wrong and I'll check for myself in VB.

 

 

EDIT: Worth noting though, is that for the above code to work, the form you want to edit (in this case label1 in form 2 from code in form1) Form2 has to be open when the code is executed. Otherwise you have to implement in a different way.

 

There are plenty of different ways, I would recommend a simple solution like storing the data you want the form to show on launch in a setting. Access settings via "Solution Explorer" then double click on My Project and go to Settings. Tell me if you need help with it!

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

With VB.NET you can use My.Forms to access all your forms.

My.Forms.Form1My.Forms.Form2//etc// Here's an example where you read text box data from Form1Dim data As String = My.Forms.Form1.TextBox1.Text

That's probably the simplest option but when you end up learning another language, it likely wont have something equivalent to My.Forms. Even C# doesn't have an equivalent. So it's good to learn alternative ways of accomplishing the task.

 

One simple option is to pass forms around in constructors. Here's an example.

Public Class Form1    Private ReadOnly _form2 As Form2    Public Sub New()        InitializeComponent()        _form2 = New Form2(Me)        _form2.Show()    End Sub    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        _form2.TextBox1.Text = Me.TextBox1.Text    End SubEnd ClassPublic Class Form2    Private ReadOnly _form1 As Form1    Public Sub New(form As Form1)        InitializeComponent()        _form1 = form    End Sub    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        _form1.TextBox1.Text = Me.TextBox1.Text    End SubEnd Class

There are better options as you get to more advanced object oriented topics, but this should be good enough for your use.

 

You can also store your data in things like plain text files, xml files, and databases. This way any form has access to the data without also having access to other forms.

Link to comment
Share on other sites

Link to post
Share on other sites

With VB.NET you can use My.Forms to access all your forms.

My.Forms.Form1My.Forms.Form2//etc// Here's an example where you read text box data from Form1Dim data As String = My.Forms.Form1.TextBox1.Text

~snip~

Ok. Well i have tried that and it works but only for the form. So like i can get a property of the form itself, but i can't seem to get it to work for anything on that form. Anyways though, I've got it working kind of. I'm using a module to store the variables i need, but i was wondering if you knew a way to write to it. Kinda getting back to the same problem :/

I can declare a variable in the module and access it in any of the forms, but i can't figure out how to change the value of it from a form, if that's even possible. It's for storing the scores of each level of my game so i can display them on a score board later. I just need to figure out how to save them from the form to the module so i can access them on the other form.

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ok. Well i have tried that and it works but only for the form. So like i can get a property of the form itself, but i can't seem to get it to work for anything on that form. Anyways though, I've got it working kind of. I'm using a module to store the variables i need, but i was wondering if you knew a way to write to it. Kinda getting back to the same problem :/

I can declare a variable in the module and access it in any of the forms, but i can't figure out how to change the value of it from a form, if that's even possible. It's for storing the scores of each level of my game so i can display them on a score board later. I just need to figure out how to save them from the form to the module so i can access them on the other form.

 

Sorry, I don't follow what you're saying. Show me the code that isn't working, tell me what it should do, and I should be able to help you fix it.

Link to comment
Share on other sites

Link to post
Share on other sites

If the labels are equal, presumable the forms are the same, so don't make two forms. That is a red flag for bad design.

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry, I don't follow what you're saying. Show me the code that isn't working, tell me what it should do, and I should be able to help you fix it.

Ok well i've created a separate module in my project. I can declare a variable on the module and access it from form1 for example. But then if in the code on form1 it changes the value of the variable, it only changes the value on form1 and stays the same on the module. So my question is if there's any way to save the new value to the module or is that not possible? Does this make any more sense? I could write some code but it would only be an example of what i'm trying to do.

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to comment
Share on other sites

Link to post
Share on other sites

If the labels are equal, presumable the forms are the same, so don't make two forms. That is a red flag for bad design.

Well I'm making a game where each for each level it opens a new form and then closes that form when the level is finished and goes back to the main menu that is a separate form from the levels (sorry if that's confusing). Is there another way that i should be doing this? How would i put them all onto one form?

Just a normal guy with a constant desire to modify everything he owns. 

Check out my current build here:

https://linustechtips.com/main/topic/1006447-the-cake-is-a-lie-water-cooled-portal-pc/

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well I'm making a game where each for each level it opens a new form and then closes that form when the level is finished and goes back to the main menu that is a separate form from the levels (sorry if that's confusing). Is there another way that i should be doing this? How would i put them all onto one form?

 

If the forms are the same (identical) I don't see why you need more than one. Can't you inject the data that changes, like a template?

Link to comment
Share on other sites

Link to post
Share on other sites

Ok well i've created a separate module in my project. I can declare a variable on the module and access it from form1 for example. But then if in the code on form1 it changes the value of the variable, it only changes the value on form1 and stays the same on the module. So my question is if there's any way to save the new value to the module or is that not possible? Does this make any more sense? I could write some code but it would only be an example of what i'm trying to do.

 

Something needs to tell that form to update itself with the new data. Events are one way to do this. Timers are another. Form1 could run a method on Form2. There are multiple ways to accomplish it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×