Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Go Back  Xtreme Visual Basic Talk > > > Passing Values of an Array from One Form to Another


Reply
 
Thread Tools Display Modes
  #1  
Old 06-23-2004, 02:54 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default Passing Values of an Array from One Form to Another


Is there a way to pass the values of an array formed in Form1.vb, say, by reading an external text file, into another form, say Form3.vb, so that these values be plotted by Form3.vb on the screen?
Reply With Quote
  #2  
Old 06-23-2004, 03:03 PM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default Vb6

This question relates to all VBs. The easiest way is to declare the array Public and I believe you have to declare it in a class or a module to be able to access it in multiple forms. Also, the easiest way to write to the 'screen' is to write on an image using special functions (I can't remember their names, but they will draw pixles, squares, circles, and bitmaps). If you need more help, let me know, but try and look some of the stuff up if you can.


Quote:
Originally Posted by Omnibus
Above question is regarding Visual Basic .NET.
Reply With Quote
  #3  
Old 06-23-2004, 03:25 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

How do you declare the array Public? The most I could do is place the dim:

Dim xdata(1000) As String

near the top, just below the Inherits. Didn't do any good, though.
Reply With Quote
  #4  
Old 06-23-2004, 05:26 PM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default public

Try somthing like this

in module:
Code:
Public myVar() As Integer

in onload of main form. This initializes. Once initialized, you should be able to use it in any form.
Code:
'im not sure if you have to initialize Integers, but if it was a custom class you would: Dim x As Integer For x = 1 To 3000 Set myVar(x) = New Integer Next
Reply With Quote
  #5  
Old 06-23-2004, 09:53 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Thank you for the code. It didn't work, though. I tried it with Public and with Public Shared and so on in one form, in another form and nothing helped. Also the Set keyword disappears when you type it in. Could it be that there is no simple way to declare the arrays with their values to be available everywhere in the program once you create them, like in the old days?
Reply With Quote
  #6  
Old 06-24-2004, 09:33 AM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default module

Did you put it in the Module. I believe it has to be declared in a module, thats what I found works best. Also, set is understood in .NET so you do not have to type it, but it should work find without that word.
Reply With Quote
  #7  
Old 06-24-2004, 10:04 AM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Maybe this is something I don’t understand and am doing wrong. What do you mean by “the Module”? What I see in the Solution Explorer is:

<NameOfProject>
References
AssemblyInfo.vb
Form1.vb
Form2.vb
Form3.vb

Also, something called app.config keeps popping up after I delete it. It only causes problems. I tried to add Module1.vb and wrote the code in it but it did no good. Probably I’m missing something.
Reply With Quote
  #8  
Old 06-24-2004, 10:47 AM
bear24rw's Avatar
bear24rw bear24rw is offline
Contributor
 
Join Date: May 2004
Location: NJ
Posts: 477
Default

Project > Add New Item > Module

That will give you a module
Reply With Quote
  #9  
Old 06-24-2004, 10:52 AM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default module

Your decleration should be in the module. See post #5. With ints I do not think you have to initialize them, so that should be about the only line of code you need.

Also, if you are still having problems, I think you can have the Integer array in one of the forms. Make it public. And access it through something like form1.myVariable=10
Reply With Quote
  #10  
Old 06-25-2004, 12:37 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Thank you very much. Worked very well, just as you suggested. All the best.
Reply With Quote
  #11  
Old 06-25-2004, 01:36 PM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default

Awesome. Im glad I could help. Just let me know if you have any other questions.
Reply With Quote
  #12  
Old 06-26-2004, 02:49 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Thank you rex64. Here’s another question. I’m using the following timer but I can’t get to stop it properly:

#Region " Timer Code "

'This starts the timer
Private Sub EnableTimer(ByRef iInterval As Integer)

'Create the timer
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer

AddHandler aTimer.Elapsed, AddressOf OnTimer

aTimer.Interval = 100
aTimer.Enabled = True

End Sub

Public Sub OnTimer(ByVal source As Object, ByVal e As ElapsedEventArgs)
'Timer fires ...
Me.Refresh()
End Sub

'This will get rid of the timer
Private Sub DisableTimer()
'Dispose of the timer and set the object to nothing
ThreadedTimer.Dispose()
ThreadedTimer = Nothing
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Enable the timer
EnableTimer(100)
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'Disable the timer
DisableTimer()
End Sub

#End Region



I’m creating a button:

Public Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer

aTimer.Enabled = False

ThreadedTimer.Dispose()
ThreadedTimer = Nothing

End Sub



but the system crashes and the following error message pops up when I click the button and try to stop the timer:

An unhandled exception of type 'System.NullReferenceException' occurred in Graphing Project.exe

Additional information: Object reference not set to an instance of an object.
Reply With Quote
  #13  
Old 06-26-2004, 03:00 PM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default

That may be a little beyond what I have done. One thing I noticed, should ThreadedTimer actually be aTimer? Also, at the very top of your code you should have this statement to make sure everything is declared. If you dont know the type for something, declare it as a Variant.

Also put the following around your code in posts: [ vb] code [ /vb]
Code:
option explicit
Reply With Quote
  #14  
Old 06-26-2004, 03:20 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Quote:
Originally Posted by rex64
That may be a little beyond what I have done. One thing I noticed, should ThreadedTimer actually be aTimer? Also, at the very top of your code you should have this statement to make sure everything is declared. If you dont know the type for something, declare it as a Variant.

Also put the following around your code in posts: [ vb] code [ /vb]
Code:
option explicit
Indeed, I do have the following at the top:

Dim ThreadedTimer As System.Threading.Timer

I also added:

Option Explicit

as you suggested, but it gave no effect. Tried also to change aTimer to Timer. Same thing – program crashes and same error message appears.
Reply With Quote
  #15  
Old 06-26-2004, 03:37 PM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default

Im thinking you need to initialze one of your variables. Make sure all classes (timers...) you do the 'new' command for each on that you call.
Reply With Quote
  #16  
Old 06-26-2004, 08:27 PM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

I solved this problem again in a very simple way. I abandoned the code I posted earlier and resorted to the drag-n-drop timer tool available in VB.NET. And then, of course, I placed Timer1.Enabled = False in the Stop button code.

Next problem I'm working on is how to switch from form to form ... Would like to discuss it with you ...
Reply With Quote
  #17  
Old 06-26-2004, 11:02 PM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default form switching

To switch from form to form use stuff like this:
Code:
unload myForm load myForm2
Reply With Quote
  #18  
Old 06-27-2004, 12:01 AM
Omnibus Omnibus is offline
Regular
 
Join Date: Jun 2004
Posts: 85
Default

Your suggestion is very elegant, indeed. Unfortunately, I can’t make it work. So far I’ve been able to switch from form to form in this way:

In Module1:

Module Module1
Public OldForma1 As New Form1
Public OldForma2 As New Form2
‘add other forms, if necessary …

Sub main()

OldForma1.ShowDialog()
‘this is the startup form

End Sub
End Module






In Form1, make a button with the following content:

Public Class Form1
Inherits System.Windows.Forms.Form

[Windows Form Designer generated code]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim OldForma1 As New Form1
OldForma1.Hide()
‘this is supposed to hide the active Form1 (in reality it doesn’t hide it for some ‘reason?)

Dim OldForma2 As New Form2
OldForma2.ShowDialog()
‘this makes Form2 active

End Sub
End Class







In Form2, make a button with the following content:

Public Class Form1
Inherits System.Windows.Forms.Form

[Windows Form Designer generated code]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim OldForma2 As New Form2
OldForma2.Hide()
‘this is supposed to hide the active Form2 (in reality it doesn’t hide it for some ‘reason?)

Dim OldForma1 As New Form1
OldForma1.ShowDialog()
‘this makes initial Form1 active

End Sub
End Class


Unfortunately, when the new form appears the old form doesn’t disappear. What could be the reason?

Last edited by Omnibus; 06-27-2004 at 12:10 PM.
Reply With Quote
  #19  
Old 06-27-2004, 06:19 AM
Iceplug's Avatar
IceplugPassing Values of an Array from One Form to Another Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

That's because OldForma2 is a new form, which is not the old form.
In the Form2 class, you need to add a reference to form 1.
Public F1 As Form1 (in Form class 2)

Public F2 As Form2 (in Form class 1)

Then, in the module, assign these references to their respective forms.
OldForma1.F2 = OldForma2
OldForma2.F1 = OldForma1

Then, you can access the other forms via these two references.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #20  
Old 06-27-2004, 10:41 AM
rex64 rex64 is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,171
Default

Try these:
Code:
frmViewAll.Show unload myForm
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
 
Passing Values of an Array from One Form to Another
Passing Values of an Array from One Form to Another
 
-->