|
-
Jan 26th, 2004, 09:08 AM
#1
Thread Starter
Hyperactive Member
Refreshing a datagrid on another form
I am trying to refresh a datagrid on one form from another. I found a code sample on this forum where I could click a button from a second form like this (Public Shared):
Form1
VB Code:
' This is form1
Public Shared Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Hello World")
End Sub
Private Sub Button2_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim xForm As New Form2
xForm.Show()
End Sub
Form2
VB Code:
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim zForm As New Form1
zForm.Button1_Click(Me, Nothing)
End Sub
Pushing the button on the second form causes the button on the first form to show the Hello World message.
Now when I try to do the same thing with a button that refresshes a datagrid I get this error: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
How do I overcome that?
-
Jan 26th, 2004, 09:24 AM
#2
PowerPoster
Hi,
Sounds like you are trying to refresh a DataGrid in a form you have not instanced. Whilst you can use the listed code to activate shared procedures/methods in a form which is not instanced, I don't see how you can refresh an object contained in an uninstanced form - i.e. while that object does not presently exist.
You could try including code in either the calling procedure or the called procedure to create an instance of the form there and then.
Last edited by taxes; Jan 26th, 2004 at 09:32 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 09:37 AM
#3
Thread Starter
Hyperactive Member
Actualy I never get to the second form to instance it. As soon as I put the RefreshDatagrid() in the button sub of the first form the blue squigly lines appear under the RefillDataGrid() with the error I posted above.
VB Code:
Public Shared Sub btnRefreshDrids_Click _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles btnRefreshDrids.Click
RefillDataGrid()
End Sub
-
Jan 26th, 2004, 10:00 AM
#4
PowerPoster
Hi,
Assuming RefillDatagrid() is a sub, why not make it a shared procedure and call it from form2?
Alternatively, just try making it a shared sub.
Last edited by taxes; Jan 26th, 2004 at 10:29 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 10:15 AM
#5
Addicted Member
Can you post a little bit of your code, to help me better understand what exactly is going on?
-
Jan 26th, 2004, 10:20 AM
#6
Addicted Member
You need to call that shared sub from the other form like:
VB Code:
Form2.Button1_Click(me,nothing)
I use this all the time.
me and nothing can be replaced by any object or eventarg.
Also the shared sub needs to be on the form with the datagrid and/or button.
If you want to just make the refreshdatagrid shared then do this:
VB Code:
' on the form with the datagrid
Public Shared Sub RefreshDataGrid()
' Do actions
End Sub
'on the form that you want to call the sub replacing Form1 with your form name
Form1.RefreshDataGrid()
You should see intellisense menu show the shared sub for Form1 when you type "Form1."
Last edited by jwmoore2001; Jan 26th, 2004 at 10:25 AM.
-
Jan 26th, 2004, 10:35 AM
#7
PowerPoster
Hi Guys,
I think that what bukhix is saying is that;
RefillDatagrid is a procedure in form1, which he tries to call from a shared sub in the same form. It is when he enters the call that he gets the intellisense message. He never gets to enter any code in form2.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 10:41 AM
#8
Addicted Member
Ok the only thing you cannot do that I am aware of is if you have two shared subs and you call one of the shared subs from another, you cannot use the keyword "me." That keyword only works for instance methods.
-
Jan 26th, 2004, 10:47 AM
#9
PowerPoster
Hi Jason,
Thanks!
May be I should be paying you a retainer!!
Is that OK now bukhix?
Regards,
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 10:48 AM
#10
Thread Starter
Hyperactive Member
From my second form I have this code
VB Code:
Private Sub btnSendRefresh_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSendRefresh.Click
Dim xform As New frmMain
xform.btnRefreshGrid_Click(Me, Nothing)
End Sub
There is no problem there. On the first forum I have this.
VB Code:
Public Shared Sub btnRefreshGrid_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRefreshGrid.Click
RefillDataGrid()
End Sub
The RefillDatagrid button click works fine on the 1st form but not the second.
I originaly tried to make the RefillDataGrid public so that I could reference the sub directly from my other form but that caused the same error.
For instance if I do this
VB Code:
Public Shared Sub RefillDataGrid()
Dim cnn As New SqlConnection(strConn)
Dim cmd As New SqlCommand
Dim strEmp As String = txtEmp.Text
cmd = cnn.CreateCommand
cmd.Connection = cnn
Dim da As New SqlDataAdapter
Dim DsPayroll1 As New DataSet
Try
All my references to text boxes and the local datagrid (in this sub) get the squigly blue line with the same error I posted above.
Last edited by BukHix; Jan 26th, 2004 at 10:53 AM.
-
Jan 26th, 2004, 10:54 AM
#11
PowerPoster
Hi bukhix,
If you are creating an instance of form1 in the form2 procedure, that you do not need the Shared declaration. Simply leave it as Public.
Jason will probably know the answer, but I think that you cannot call procedures which are not shared from a shared procedure, as they would not be available from an uninstanced form.
Yes, I just tried it. You have to either;
Make the RefillDatagrid a Public Shared procedure and do not bother to instance form1 in form2
or
remove the Shared declaration in the called procedure, and create an instance of form1 through form2.
Last edited by taxes; Jan 26th, 2004 at 11:04 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 11:01 AM
#12
Addicted Member
Actually it hased to be Public Shared. Either way, the way i explained above works.
There must be something else you are doing wrong. Please post the entire first form down to the click event and the refresh sub.
I use this all the time.
Any event or procedure you declare as Public Shared can be used by any object within your assembly(program). Any instances declared as Friend Shared can be used by other programs outside that assembly.
-
Jan 26th, 2004, 11:03 AM
#13
PowerPoster
Hi Jason,
Sorry, I don't quite agree. See my previous post.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 11:07 AM
#14
Addicted Member
heres a sample:
VB Code:
Class Form1
Public Shared Sub GetThis()
MsgBox("gEt this")
End Sub
Public Shared Sub GetThis2()
'me.GetThis() this wont work because the keyword me cant be used in a shared instance
'but
form1.GetThis() 'will work or can remove form1 here
End Sub
End Class
Class Form2
Private Sub CallForm1GetThis()
form1.GetThis()
End Sub
End Class
-
Jan 26th, 2004, 11:16 AM
#15
PowerPoster
Hi bukhix,
" Public Shared Sub RefillDataGrid()
Dim cnn As New SqlConnection(strConn)
Dim cmd As New SqlCommand
Dim strEmp As String = txtEmp.Text
cmd = cnn.CreateCommand
cmd.Connection = cnn
Dim da As New SqlDataAdapter
Dim DsPayroll1 As New DataSet"
To clarify mysuggestions, if, in a shared procedure, you refer to other objects,, those objects must have been declared as Public Shared, so you will have to declare them in the general section of form1.
Don't give up!
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 11:18 AM
#16
Addicted Member
Ok wait a minute. There are a couple of things we are talking about here. Let me see if I can lay this out.
If for instance you are trying to have both forms up, Form2 will have a button that refreshes the data grid on Form1 then you cannot use an instance of form1 on form2, you must use the shared sub to refresh the data grid being displaye.
Now if you just want to say use a function or sub that you may want to re-use that is form independant and doesnt access controls on the form then just create a seperate class or module that has re-usable objects.
Furthermore, you could declare the datagrid as shared and then instantiate it as shared and that data grid could be accessed on any form.
This is a huge topic, many different things can be done here. We just need to understand what it is he is doing.
-
Jan 26th, 2004, 11:23 AM
#17
PowerPoster
Hi Jason,
You're right. I was just trying to patch up bukhix's existing code, so that he can understand the principles here rather than abandon them without knowing why they did not work.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 11:25 AM
#18
Thread Starter
Hyperactive Member
Ok the code is very long so I don't think I should (can) post it all.
taxes and jwmoore2001 thank you for your help so far. I think I am starting to understand the problem.
Per taxes last post the reason the Public Shared works for a message box and not my text boxes is probably because the text boxes have not been declared as Public Shared. If that is the case and I understand you correctly I can go to the top of the page and declare them.
I did try and make the sub public without the shared and called it from the second form but that didn't seem to do anything at all.
I'll post back once I have declared the text boxes as public shared.
-
Jan 26th, 2004, 11:28 AM
#19
Addicted Member
Sorry, just want to clarify instantiation or creating a new instance. When you do that it creates a whole seperate memory allocation slot. So if you have form1 running and you instantiate another form1 on form2, you cannot access the running form1 from the instance object of form1 on form2.
When you instantiate you create a new object name.
dim objForm1 as Form1
now objForm1 and and the object name that .NET compiler uses for the visible Form of form1 are running in seperate threads.
so objForm1's members and methods are not going to change the running Form1. Its going to change properties in its own instance.
-
Jan 26th, 2004, 11:32 AM
#20
Thread Starter
Hyperactive Member
Is it possible to make the text boxes a publis share another way?
Public Shared txtEmp As TextBox gives me this error 'txtEmp' is already declared as 'Friend Dim WithEvents txtEmp As System.Windows.Forms.TextBox' in this class.
-
Jan 26th, 2004, 11:32 AM
#21
PowerPoster
Hi BukHix,
"Dim cnn As New SqlConnection(strConn)
Dim cmd As New SqlCommand
Dim strEmp As String = txtEmp.Text
Dim da As New SqlDataAdapter
Dim DsPayroll1 As New DataSet"
Ref your last post. You need to delare all the above (including strConn) as Public Shared in the general section of form1 ( or a module)
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 11:47 AM
#22
Addicted Member
Yes, but you need to go to the original declaration and redefine it
Public Shared WithEvents TextBox1 as System.Windows.Forms.TextBox
Here is some sample code that should help for access shared methods.
VB Code:
Public Class Form1
Friend WithEvents dgrdDb1 As System.Windows.Forms.DataGrid
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dgrdDb1.DataSource = dsMyDB
End sub
Public Shared function RefreshDataGrid(ByVal Sender as Object, ByVal e as System.EventArgs) as boolean
dgrdDb1.refresh()
End Function
End Class
' Second form
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(Form1.RefreshDataGrid(me,nothing))
End sub
End Class
-
Jan 26th, 2004, 12:13 PM
#23
PowerPoster
Hi Bukhix,
In which form is the textbox txtEmp?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 12:28 PM
#24
Thread Starter
Hyperactive Member
txtEmp is on the the first form. I tried to change its properties to public shared and got squigly lines all through out my application.
I have not been able to figure this out so I think I am going to have to abandon this for now and come back to it at a later time.
Actually I may just start a new application with nothing but a public sub and two forms, print out this thread and start some experimentation on a project that I don't have to be so careful with.
Thank you both for all the help and time you have put into this thread.
-
Jan 26th, 2004, 12:52 PM
#25
PowerPoster
Hi Bukhix,
Don't give up! Whenever Jason is contributing I always learn a lot!!
If the textbox is on form one, and presumably contains the information with which you wish to update the datagrid, why on earth are you trying to update it from form2????
I had assumed that you were trying to update the form1 datagrid when you were already in form2 and did not wish to access form1 for some reason, but if you are simply updating some information just entered in the textbox, you must already be in form1.
To summarise what I have learned from this thread:
1. If a form is not open, you can only access it's subs and methods if they are declared as Public Shared.
2. If a Public Shared sub contains code referring to other objects, including variables, contained in the same form, those objects must be declared as Public Shared.
3. If a form is already instanced and you declare another instance of it using the same name, that other instance is a completely separate form from the one first instanced.
Many thank Jason.
Regards,
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 26th, 2004, 01:00 PM
#26
Addicted Member
If you declare the object this way then you need to declare the subs that handles that objects events as shared. This is really complicated to do.
VB Code:
Friend Shared WithEvents txtEmp as System.Windows.Forms.TextBox
Friend WithEvents dgrdDB1 as System.Windows.Forms.DataGrid
Private Shared Sub txtEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDisplay.Click
dgrdDB1.refresh() 'this will not work because dgrdDB1 also needs to be declared as shared
End Sub
Simply put, you cannot reference a method, property or object from within a shared object unless they are also declared as shared.
I dont know what you are doing but, you can easily just declare the textboxes event sub as shared and call it from another form. You do not need to declare the object as shared.
VB Code:
' not declared as shared
WithEvents Button1 as system.windows.forms.Button
'declared as shared
Private Shared Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDisplay.Click
'From another form
Form1.Button1_Click(Me,Nothing) ' again dont use me, if this call is inside of a shared procedure
-
Jan 26th, 2004, 01:03 PM
#27
Addicted Member
Hey, no problem at all. I am studying for my MCSD. So by answering questions it helps burn it into my brain (and i like helping people, which is why i am a software developer, software helps people).
-
Jan 26th, 2004, 01:42 PM
#28
Thread Starter
Hyperactive Member
Ok I am going to keep trying this. Let me try and explain the entire process of what is supposed to happen between the two forms.
form1 has a datagrid on it where the user can double click the grid to edit the data in the particular row he/she clicked on.
The second form, form2 is a form with text boxes to hold the information from the row that was clicked on in form1. In form2 the user can update or delete the record (only one record at a time) they are working with.
All I need to do is to refresh the datagrid in the first form so that if, for instance, the user wants to change the [TotalHours] field from 40 to 45 the changes will be reflected in the grid without forcing the user to click the Refresh Button on form1.
You may be wondering why I don't just allow editing in the data grid directly on form1. If you are the answer is that I need to do look ups and new calculations when ever the data the fields are changed.
Thanks for the patiance and encouragement.
-
Jan 26th, 2004, 03:19 PM
#29
Addicted Member
Ok in order to bypass sharing. You need to instantiate the form that has the datagrid in the edit form and show it from the edit form. You need to hide the edit form when it loads if you dont want that form to be displayed until a user clicks a button.
VB Code:
Class Form2
dim f1 as new form1()
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
f1.Show()
Me.Hide()
Me.Visible = False
End Sub
now all the methods and properties are available in form1 that is being displayed.
-
Jan 26th, 2004, 05:38 PM
#30
PowerPoster
Hi Jason,
"Class Form2
dim f1 as new form1()
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
f1.Show()
Me.Hide()
Me.Visible = False
End Sub"
My only comment is that BukHix does not need to instantiate f1 as above. It should already be instanced when form2 is activated. If he instances both forms in a module that should suffice.
Hi BukHix,
Sorry I have been out playing tennis for the past 4 hours.
You have two easy options here. Instance both forms in a module and leave them open, switching between them using Jason's code for form2_Activated as above. You then should not use the Shared declaration at all, only Public.
Alternatively you could do away with form2 and place all the objects on form1, hiding them and making them visible as required - again, no Shared (or even Public) declarations required.
After all this, it seems that there is no need to use any shared declarations at all, as both forms will have been instanced at the same time.
best of luck.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|