Visual Basic 2008 - vb.net

My previous question is not clear. So I'm going to repeat it.
The label on the right part are the transmuted grade and the textbox is the raw score percentage.
How do I do a short way for this codes (where I will not repeat it again in other textboxes)
Dim grade as Integer
This codes will be in the button so when pressed, the raw percentage will be transformed to transmuted grade.
If MathTextbox.Text = "100" then MathLabel.Text= "1"
Codes like this, my prob is how do I avoid repeating it in each texboxes.

If the problem is that you want one button to perform the same action to multiple controls then you can do something like this:
For Each ctrl1 As Control In Me.Controls
If TypeOf (ctrl1) Is TextBox Then
For Each ctrl2 As Control In Me.Controls
If TypeOf (ctrl2) Is Label AndAlso _
Microsoft.VisualBasic.Strings.Left(ctrl1.Name, 4) _
= Microsoft.VisualBasic.Strings.Left(ctrl2.Name, 4) Then
ctrl2.Text = ctrl1.Text
Exit For
End If
Next
End If
Next
Note that there are much better ways for identifying which label pairs with which textbox (I prefer the Tag property myself) and that if you're going to perform a numerical operation on a string (such as a textbox's Text property) you should validate it first, e.g. with IsNumeric.

Related

How can I get the name of the Textbox in which a function is being run in MS Access form?

I am trying to get create a function (or some expression) that can be run from any textbox on a Form, which will return the Name of the textbox as the Value of that textbox, i.e. each box will display it's name in the box itself.
I'm looking for something like Me.ActiveControl.Name, but that only returns the same value for each textbox. Is it possible to be self-referential like that? I haven't been able to find anything that does that.
Cell? self-referential?
The answer is Me.ActiveControl.Name which does return the name of the active control (textbox).
That name doesn't change from record to record. If you wish to identify individual records, use the ID of the record or the CurrentRecord property of the form.
You can make a control the ActiveControl of the form with:
Me!TheControlName.SetFocus
or:
Me.Controls("TheControlName").SetFocus
or loop the Controls collection of the form to list the textboxes:
Dim Control As Control
For Each Control in Me.Controls
If Control.ControlType = acTextBox Then
Debug.Print Control.Name
End If
Next

using a loop to input arraylist value into checkbox, gives wrong value

I have a form I would like to fill out with the following code. The purpose is to fill out the CheckBoxes, which are placed inside panels, and then placed in some TabPages. This code worked well to grab the value of the CheckBoxes, but for some reason it reads my ArrayList wrong. For example, if the ArrayList is filled with "1, 1, 0, 0, 0..." it will read every row as "1" and set the CheckBoxes accordingly.
I also tried placing an integer to see if it repeated the process multiple times (the ArrayList contains 16 rows) and the integer turned out to be several times the 16 rows. I did try to restrain the loop with an if sentence, and even though it stopped after a given number, it still produces the wrong answers.
I've come to a stop, and can't figure out why this code won't do the trick. Help would be greatly appreciated.
I should probably mention that 'tabell' is the ArrayList which I try to pull the data out of.
(Also, if this has been asked before, I am sincerely sorry for repeating the question..)
For Each rad In tabell
For Each tb In TabControl1.Controls.OfType(Of TabPage)()
For Each pnl In tb.Controls.OfType(Of Panel)().OrderBy(Function(c) c.TabIndex)
For Each cb In pnl.Controls.OfType(Of CheckBox)()
If rad = 1 Then
cb.Checked = True
End If
Next
Next
Next
Next
You are looping through all of the checkboxes for each value in tabel1, and since you never uncheck the boxes, the first 1 value will check all the boxes and that is how they will stay.
I am guessing that you want to use the corresponding value from tabel1 based on the order that the checkboxes are found (which I think does not necessarily have to match the order that they appear on the screen, so you may have to sort the checkboxes too):
Dim idx = 0
For Each tb In TabControl1.Controls.OfType(Of TabPage)()
For Each pnl In tb.Controls.OfType(Of Panel)().OrderBy(Function(c) c.TabIndex)
For Each cb In pnl.Controls.OfType(Of CheckBox)()
cb.Checked = tabel1(idx) = 1
idx += 1
Next
Next
Next

How to change the order controls are read in .Net?

I have a very textbox rich heavy panel (approx ~ 25 textboxes) for which I want to retrieve all my text from.
I've done so using this loop.
Dim AllItemsArray As New ArrayList
For Each txt As Control In Panel2.Controls
If txt.GetType Is GetType(TextBox) Then
AllItemsArray.Add(txt.Text)
End If
Next
However, for some odd reason it reads the textboxes in a completly random order which makes using the information extremely difficult.
I thought that the textboxes were read in the order they are made, but so far it hasn't done so.
Does anyone have any suggestions as to how I can alter it so that it reads the textboxes in order?
IE. textbox1.text, textbox2.text ...etc
and not
textbox5.text, textbox2.text, textbox 20 ....etc
Thanks
This gets the controls in Tab order sequence. It also gets controls that are in containers, i.e. GroupBox.
Dim ctrl As Control = Me.GetNextControl(ctrl, True)
Do While ctrl IsNot Nothing
Debug.WriteLine(ctrl.Name)
ctrl = Me.GetNextControl(ctrl, True)
Loop

Populating a textbox, if another textbox is populated

i've got two textboxes in my form.
According to title, when I write something in one textbox (a random one), i need that at the same time, in the other textbox a text (given in the code) appears.
1 letter for 1 letter
1)Example with random text:
1 textbox ) How are you?
2 textbox) Let's just c
2)Example with random text:
1 textbox ) What is the aim of the project?
2 textbox) Let's just chill out for a mome
thanks so much
You will need to determine which responses you would like to which questions. However, the tackle to letter for letter problem. I would use the substring method and get the length of the current text.
Dim Response as String = "Hello World!"
Private Sub Text(ByVal..) Handles Textbox1.changed
Textbox2.text = Response.Substring(0, Textbox1.text.length)
End Sub
As the text changes in the textbox you are typing in, this will output the response corresponding to the length of the text input. Since the event is textbox.changed, each character entered will update the second textbox
You need to use an event based code here. I would use a loop per stroke of the key and make the event "Key Press." Write the code onto the textbox you are inserting data into.

how to clear all tools in the vb.net from

i have used many of the controls in vb.net forms, including a textbox and combobox, but I want to clear all my text boxes at once, while not typing textbox1.clear() for each one.
Is there any other way to clear all my textboxes?
If I understand you question right, you should be able to loop through all the controls on your form and check to see what Control type they are. Based on their type, either set the textbox Text property to String.Empty, or your ComboBox to the index of a blank ListItem (presumably item zero).
Something like:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
Else
' do something similar for your ComboBox
End If
Next
You can parse through each control on your form and test what type of control that is and handle each type separately, but it is easier to simply set each control manually.
Consider writing a ClearForm routine that does all of this, that way all you have to do is call the method.