Visual Basic Dealing with Many Variables on a Form?
-
I'm new to visual basic and finding that working with many variables on the same form can be complicated and time consuming. In my program, the user has the option to press a button to use default values of the variables, which are all completely different. Then they can change the value of any of these variables by modifying the textbox corresponding to each variable. Basically, the user is free to enter a numerical value and they can use the predetermined value if they are unsure, but their value always takes priority. I think there might be a way to fix each of the variables to a textbox and maybe use If statements to solve this problem? Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click H_Temp = 93 D_Temp = 69 C_Temp = -9 W_Temp = 54 TextBox2.Text = H_Temp TextBox3.Text = D_Temp TextBox4.Text = C_Temp TextBox5.Text = W_Temp End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged H_Temp = Val(TextBox2.Text) End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged D_Temp = Val(TextBox3.Text) End Sub Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged C_Temp = Val(TextBox4.Text) End Sub Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged W_Temp = Val(TextBox5.Text) End Sub When dealing with 20 variables or more on a form this can become very time consuming. Any suggestions?
-
Answer:
One of the powerful things VB has to offer is the ability to write self documenting code. What this means is that you can use meaningful names to identifiy variables and controls so that anyone reading your code can easily figure it out. Textbox2 is a default name so what does it mean and which of your temporary values is supposed to go with it? TextBox2.Text = H_Temp TextBox3.Text = D_Temp If instead you go into the controls properties anc change its name using Hungarian Notation you would have the following code instead. Hungarian notation uses a 2 or 3 letter prefix to ID the type of control (txt for a TextBox, chk for a Checkbox) . The prefix is followed by a meaningful name which held describe what it is for. txtH.Text = H_Temp txtD.Text = D_Temp =======================================… There is a neat little property called a TAG which can be used by you to store a string within the textbox control. A TAG can be used for many thinks but in this case you can store your default (temporary) value. Look in the control's property window and you will see the Tag property. You can either set the Tag at design time via the property window or at run time when your form loads txtH.Tag = "93" txtD.Tag = "69" Using the tag property will save you from having to use module level variables. Another option is to go into your project properties and create a bunch of settings. Once you have established these setting you can accessthem from the MY.Settings object The setting can be any datatype you need . Me.TextBox2.Text = My.Settings.H or Me.txtH.Text = My.Settings.H You can change the value of a setting from a text box entry. my.setting.H = Cint(Val(txtH.text)) the change will remain ineffect while the program runs, unless you save the change which will preserve it for the next time the program runs. =======================================… You can make an event handler fire for multiple controls. You can avoid duplication of similar event handlers by using a multi control event handler. The trade off is that you need a way to identify a specific textbox. The tag property can be used to store an index number of an array or you can parse the control name to extract a unique ID that can be used as a key in a collection Private Sub Multi_txt_TextChanged(sender As System.Object, _ e As System.EventArgs) _ Handles txt_H.TextChanged, _ txt_D.TextAlignChanged, _ txt_W.TextChanged ' Dim myTB As TextBox = sender Dim strName, strMsg As String Dim ay(), myKey As String 'array used for split command 'all textboxed have been named with a prefix txt_ i.e. txt_H 'a unique patterne in the name can be used to extract a name or you 'can use the tag property to hold a letter code or name Dim mySettingsCol As New Collection ' 'loading this collection would be done at form load but is shown here for clarity With mySettingsCol .Add(93, "H") .Add(69, "D") .Add(54, "W") End With strName = myTB.Name ay = Split(strName, "txt_") myKey = ay(1) strMsg = "The setting for " & myTB.Name & " is " & mySettingsCol(myKey) MsgBox(strMsg) End Sub
h2cpetr0... at Yahoo! Answers Visit the source
Other answers
put all your variables into a class. There you can set the default when you declare the variable. In your example, the defaults should be up at the top of the form where they are declared Dim H_Temp as Integer = 93 And remember, no one has ever said programming is was easy or quick.
AJ
The whole point of having controls in VB is that you can refer to the controls themselves. You don't need to store them in variables. So whenever you're using W_Temp in your code (for example), you would instead use TextBox5.Value to get the same value. Saving everything to variables is unnecessary and puts undue strain on your code. Not to mention that you're just creating hundreds of potential issues for yourself by variables not saving correctly, code being omitted, etc. Does that make sense?
Jack
Related Q & A:
- What are the object-oriented features of Visual Basic.NET?Best solution by msdn.microsoft.com
- Can you program Graphics with Visual Basic?Best solution by Yahoo! Answers
- Codes for a simple visual basic program.Best solution by vbtutor.net
- Is java or visual basic a machine level, low level, high level or binary level programming language?Best solution by Quora
- How do I output from one form to another in Visual Basic?Best solution by support.microsoft.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.