-
Nov 25th, 2014, 04:49 PM
#1
Thread Starter
New Member
Cannot display textbox content on a Tabpage
Hi all!
I have a module in which I have procedures calculating integers. I want to have a textbox on a Tabpage display the figure (e.g., txtScore.text = Total on Tabpage2) when a button is clicked. Sounds easy enough, right? Nope.
Error message; ""txtScore" is not declared. It may be inaccessible due to its protection level."
Does anyone know what I need to do? Please... keep it simple... I hope to be a novice VB programmer someday.
Thanks!
-
Nov 25th, 2014, 05:00 PM
#2
Re: Cannot display textbox content on a Tabpage
txtScore is the name of your control. If your TabPage is located on a separate form than the module that you're trying to access the control from, then you need to type in the form's name before the control. For example, if your TabPage is located on a Form called Form1 then you'd do this:
Code:
Form1.txtScore.Text = Total
Also, just a piece of advise... use a NumericUpDown control rather than a TextBox to handle numerical data.
Edit - As I pondered on this question for a while, it may be more beneficial for you to pass a ByRef parameter in your calculate procedure. Take this for example:
Code:
Friend Sub Calculate(ByVal x As Integer, ByVal y As Integer, ByRef txtbox As TextBox)
txtbox.Text = (x + y).ToString
End Sub
Or to make the sub a function that away you could set the text property of the textbox to the string version of the function:
Code:
'This code in your module
Friend Function Calculate(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
'This code in your form
txtScore = MyModule.Calculate(1, 2).ToString
Last edited by dday9; Nov 25th, 2014 at 06:14 PM.
-
Nov 26th, 2014, 11:03 AM
#3
Thread Starter
New Member
Re: Cannot display textbox content on a Tabpage
Your suggestion of using a function instead seems to work just fine! Thanks!
I had originally intended to use a sub procedure because I thought it might be easier to calculate a number of related variables, and then just have them appear in their appropriate text boxes. For example;
-------------------------------------------------------
Code:
Private Sub ProduceNumbers()
(dim all the variables)
Num1 + Num2 + Num3 + Num4 = Sumnum
txtSumnum.text = Sumnum.tostring
Num1 + Num4 = PartNum
txtPartnum.text = PartNum.to string
And so on...
End sub
-------------------------------------------------------------
In reality, I'll be comparing two arrays to generate a number of variables like number of missing items, scores of factors within the arrays. I know that a function can be made to produce more than one variable, but it's beyond my ability.
Given my rudimentary VB skills, I guess I'll just write a bunch of functions and then populate the texboxes with another event (e.g. btnScoretheTest). It'll do the trick!
Thanks again... you're the best!
Nohandles
Last edited by dday9; Nov 26th, 2014 at 05:23 PM.
-
Nov 26th, 2014, 03:45 PM
#4
Hyperactive Member
Re: Cannot display textbox content on a Tabpage
Originally Posted by Nohandles
I know that a function can be made to produce more than one variable, but it's beyond my ability.
Given my rudimentary VB skills, I guess I'll just write a bunch of functions and then populate the texboxes with another event (e.g. btnScoretheTest). It'll do the trick!
dday9's suggestion of passing a ByRef parameter to your method is the way that you can have a function / sub return more than one value. When you pass a value type of parameter ByRef it is actually passing that exact variable to your method, allowing you to change the contents of the variable and having that change remain in the calling method (meaning that the change will remain even after the program exits that method). ByVal in contrast will make a copy of the data into a new variable. This means that it won't change the underlying variable in the calling method.
Note that in VB.Net, passing a reference type (eg an instance of a class) ByVal means that the pointer to that Object is passed ByVal (eg the pointer value is copied to a different variable) but the Object itself that is being pointed to remains the same. This means that you can still make changes to the Object itself that will persist throughout the rest of the program. The only difference is that if you had the variable to point to a new object, that pointer won't be reflected back to the calling method; you'd have to pass that parameter ByRef in order to have the rest of the program point to a new object.
-
Nov 26th, 2014, 03:59 PM
#5
Hyperactive Member
Re: Cannot display textbox content on a Tabpage
I just realized that my previous post was a little misleading in that I made it sound like passing parameters ByRef was the only way to have a function return multiple values. Although correct in that it is one possible way of going about this, some programmers would argue that using this practice is a little dangerous in that its possible to make inadvertent changes that affect the rest of the program, and is also more confusing in that when calling that method it isn't clear which parameters are acting as a secondary output field. A better way would be to create a structure or a different object in which all of the output values are put into only one returned item.
-
Nov 26th, 2014, 04:17 PM
#6
Thread Starter
New Member
Re: Cannot display textbox content on a Tabpage
This is slowly making more sense to me... and I sure appreciate the help you guys are giving me... I'll definately study your suggestions to better get a grip on the topic!
I bet there's something simple that I'm stumbling over, but I still don't get why this code doesn't work (please excuse what must seem like an insanely simplistic and unsophisticated understanding of VB.Net);
-------------------------------------------------------------------------
Code:
Public Sumnum, PartNum as integer
[ - in the frmLoad evernt - to give them scope so that they can be used on a different form]
Code:
Private Sub ProduceNumbers()
(dim all the Private variables)
Num1 + Num2 + Num3 + Num4 = Sumnum
txtSumnum.text = Sumnum.tostring
Num1 + Num4 = PartNum
txtPartnum.text = PartNum.to string
And so on...
End sub
------------------------------------------------------------------------
Ian
Last edited by dday9; Nov 26th, 2014 at 05:23 PM.
Reason: Added Code Tags
-
Nov 26th, 2014, 05:21 PM
#7
Re: Cannot display textbox content on a Tabpage
You cannot declare the scope of the variable with the exception of the Static keyword inside a method or event. If you want the two variables to have a global scope then place them at the form level:
Code:
Public Class Form1
Public Sumnum, Partnum As Integer
End Class
Also, you have the order backwards. You want to set Sumnum and Partnum not check for equality:
Code:
Sumnum = Num1 + Num2 + Num3 + Num4
Partnum = Num1 + Num4
Edit: Also, please in the future wrap your code in code tags. They look like this:
[CODE]'My code here[/CODE]
It properly formats your code in the code box. You can either type the tags manually or hit the pound sign in the quick/advanced reply and it will automatically generate them for you.
-
Nov 26th, 2014, 05:29 PM
#8
Thread Starter
New Member
Re: Cannot display textbox content on a Tabpage
As for the scope issue... DOH! As as for the other part.... DOH!
I'm glad you guys are so patient!!!
Ian
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
|