vb.net textbox not displaying text or colors - vb.net

I have many applications that reliably display data and colors in text boxes. I copied one working program to make a new application. There are about 10 text boxes that I use for data displays. Some are working - and some are not. Here are 2 samples - the Switch code works in many programs - but here it will put the word "ON" in the box but no color, and then when the switch is off - no change to the textbox. The code seems not to care between the .AppendText or the .text = variable control. I have tried changing the properties of the text box to public but no luck. I have tried adding the .show object, and verified the .visible attribute in the properties view. A similar problem is with the DistanceUnitsOut and the NextImageDistanceOut - I can step through the code and see the values, but the displayed data does not change! I have tried changing the definition of the display to public - no joy!
SwitchStatusBox.clear
SwitchStatusBox.AppendText(SwitchStatusIn)
If SwitchStatusIn = "OFF" Then
SwitchStatusBox.BackColor.Equals(Color.Red)
SwitchStatusBox.Show()
Switch_Is_On = False
Else
SwitchStatusBox.BackColor = Color.Green
SwitchStatusBox.Show()
Switch_Is_On = True
End If
If dDistance > 1.0 Then
'show distance in Miles
DistanceUnitsOut.Clear()
DistanceUnitsOut.Text = "MI"
NextImageDistanceOut.Clear()
NextImageDistanceOut.Text = CType(dDistance, String)
Else
NextImageDistanceOut.Clear()
NextImageDistanceOut.Text = CType(dDistanceFT, String)
DistanceUnitsOut.Clear()
DistanceUnitsOut.Text = "FT"
End If
Private Sub NextImageDistanceOut_TextChanged(sender As Object, e As EventArgs) Handles
NextImageDistanceOut.TextChanged
End Sub
Private Sub DistanceUnitsOut_TextChanged(sender As Object, e As EventArgs) Handles
DistanceUnitsOut.TextChanged
End Sub

To see how the .Equals function works...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a = TextBox1.BackColor.Equals(Color.Red)
Debug.Print(a.ToString)
End Sub
In my test app this prints False in the Immediate window.

The answer is that some time ago I began using <variablename_OUT> followed by the TEXT = as in :
NextImageDistanceOut.Text = CType(dDistanceFT, String)
what worked was:
NextImageDistanceOut.Appendtext(CStr(dDistanceFT))
I still don't understand why the first did not work - Both require a previous
NextImageDistanceOut.Clear

Related

How to Remove a control from another Sub than the one where the control was initially created in VB .NET

I have written some code to create a PictureBox every time the code runs, which works fine.
Public Sub BtnHit_Click(sender As Object, e As EventArgs) Handles BtnHit.Click
Dim PicBoxNewCard As New PictureBox
PicBoxNewCard.Width = 114
PicBoxNewCard.Height = 166
PicBoxNewCard.SizeMode = PictureBoxSizeMode.Zoom
DrawCard(PicBoxNewCard)
Me.Controls.Add(PicBoxNewCard)
PicBoxNewCard.Location = New Point((257 + (57 * DrawnCardCounter)), 349)
But I want to be able to delete these created PictureBoxes by pressing a button, which would be in a different sub to the one that creates the Boxes.
I have googled around and have found references to creating Classes, Panels etc and have not had any success. I have found the exact code that I need to make it work, (Me.Controls.Remove(PicBoxNewCard)) but it only seems to work when executed in the same Sub.
Set the Name or Tag property and use that later when searching for the PictureBox to remove.
PicBoxNewCard.Name = $"Card{DrawnCardCounter}"
Private Sub RemoveButton_Click(sender As Object, e As EventArgs) Handles RemoveButton.Click
RemoveCardPicBox(10)
End Sub
Private Sub RemoveCardPicBox(CardNumber As Integer)
Dim delpicBox As PictureBox = Me.Controls.OfType(Of PictureBox).Where(Function(x) x.Name = $"Card{CardNumber}").FirstOrDefault
If Not delpicBox Is Nothing Then
Me.Controls.Remove(delpicBox)
End If
End Sub

Textbox only converter

I am making a temperature converter in vb.net for my assignment. I know the conversion method and so on.. but the problem is, I need to only use two textboxes. One for Celsius and one for farenheit. Whenever I update the textbox for celsius, the changes on textbox should also happen, and when I change the value for farenheit, the celsius textbox should also change depending on the value for farenheit. What method should I do?
This is the current one im working on..
Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If TextBox1.Focus() Then
TextBox2.Clear()
TextBox2.Text = (TextBox1.Text - 32) / 1.8
ElseIf TextBox2.Focus() Then
TextBox1.Clear()
TextBox1.Text = (TextBox2.Text * 1.8) + 32
End If
End Sub
End Class
Thanks for posting some code. This is the way I'd do it (after wiring up TextChanged event handlers for both text boxes):
Private DegreesCChanging As Boolean = False
Private DegreesFChanging As Boolean = False
Private Sub DegreesF_TextChanged(sender As Object, e As EventArgs) Handles DegreesF.TextChanged
If Not DegreesFChanging Then
Dim Temperature As Double
DegreesCChanging = True
If Double.TryParse(DegreesF.Text, Temperature ) Then
DegreesC.Text = ((Temperature - 32.0) / 9.0 * 5.0).ToString("0.##")
Else
DegreesC.Text = String.Empty
End If
DegreesCChanging = False
End If
End Sub
Private Sub DegreesC_TextChanged(sender As Object, e As EventArgs) Handles DegreesC.TextChanged
If Not DegreesCChanging Then
Dim Temperature As Double
DegreesFChanging = True
If Double.TryParse(DegreesC.Text, Temperature ) Then
DegreesF.Text = (Temperature / 5.0 * 9.0 + 32.0).ToString("0.##")
Else
DegreesF.Text = String.Empty
End If
DegreesFChanging = False
End If
End Sub
There are a few things to note.
I'm using the TextChanged event - as soon as the user types
something into either text box, the world starts changing
I use double.TryParse to convert the number to a string. If I can't figure out what's going on (i.e., the TryParse call returns False), I stick an empty string in the other text box. It works quite well.
When the user types something into a text box, it causes a TextChanged event that forces new text into the other text box - which will result in a TextChanged event for that control. I use two Boolean flags to prevent this.
I use a custom numeric format string on my ToString calls to limit
the precision to two decimal places.

Validating textbox when enter button is pushed

I'm creating a AddIn for Autodesk Inventor (3D drawing software), and for the moment I am playing with positional constraints.
I created a custom user menu for quick editing certain values, in this case the elevation and orientation value.
First I used the textbox.textchanged event to change my constraint value. But this was working not 100%. Example when pressing elevation 1000 would change the elevation 4 times ( on per digit ).
Now I went to using the validated event. This works better, BUT I would like to have the textbox to initiate validation when the Enter button is pressed. For this I whipped up this, but it's not correct i'm sure of it. How should I write this correctly?
The code below works but, I would like to have a proper way to achieve the result.
Private Sub tbElevationValue_TextChanged(sender As Object, e As EventArgs) _
Handles tbElevation.Validated
' If the elevation parameter and textbox value are the same
' The sub must be aborted
If CDbl(tbElevation.Text) = oElevationParameter.Value * 10 Then Exit Sub
' Check the entered value
Dim oValue As Double
If tbElevation.Text = "" Then
oValue = 0
tbElevation.Text = 0
Else
oValue = tbElevation.Text
End If
' Set the parameter value
oElevationParameter.Value = oValue / 10
' Update the document
EM_AddIn.StandardAddInServer.m_inventorApplication.ActiveDocument.Update()
End Sub
Private Sub tbOrientation_TextChanged(sender As Object, e As EventArgs) _
Handles tbOrientation.Validated
' If the orientation parameter and textbox value are the same
' The sub must be aborted
If CDbl(tbOrientation.Text) = cRandiansToDegrees(oOrientationParameter.Value) Then Exit Sub
' Check the entered value
Dim oValue As Double
If tbOrientation.Text = "" Then
oValue = 0
tbOrientation.Text = 0
Else
oValue = tbOrientation.Text
End If
' Set the parameter value
oOrientationParameter.Value = cDegreesToRandians(oValue)
' Update the document
EM_AddIn.StandardAddInServer.m_inventorApplication.ActiveDocument.Update()
End Sub
Private Sub OrientationElevationEnterKey_Pressed(sender As Object, e As Windows.Forms.KeyEventArgs) Handles tbElevation.KeyUp, tbOrientation.KeyUp
If e.KeyCode = Windows.Forms.Keys.Enter Then
CType(sender, Windows.Forms.TextBox).Parent.Focus()
CType(sender, Windows.Forms.TextBox).Focus()
End If
End Sub
I think you're on the right way. It can be a pain to register a key_down event for each TextBox object, but your idea is good.
You can set a key_down event listener to your form instead.
Try something like this:
Private Sub Main_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.KeyCode.Enter Then
MyBase.Focus() ' this will cause the currently focused control to validate
End If
End Sub

How to check if a folder exists automatically without ".click" function

I have a problem. I have a program written in Visual Basic using Visual Studio 2013 and it works good. The problem is that I want the program to check if a folder exists on program start-up and return a text value in a label without having to manually "click" the label. I have searched and cannot find anything, maybe I'm not searching for the right thing?
Here is a sample of the check:
''//ASDG_JR check
Private Sub lbl_asdg_jr_pres_Click(sender As Object, e As EventArgs) Handles lbl_asdg_jr_pres.Click
If My.Computer.FileSystem.DirectoryExists(user_txt_dir.Text & "\#ASDG_JR_v0.14") Then
lbl_asdg_jr_pres.Text = "All good!"
btn_asdg_di.Text = ".zip file downloaded already"
btn_asdg_unzip.Text = "unzipped and installed already"
btn_asdg_di.Enabled = False
btn_asdg_unzip.Enabled = False
Else
lbl_asdg_jr_pres.Text = "Use buttons ----->"
btn_asdg_di.Enabled = True
btn_asdg_unzip.Enabled = True
End If
End Sub
Place your code inside the form's load event.
Private Sub myForm_Load(sender As System.Object, e As System.EventArgs) Handles myForm.Load
'If directoryExists Then update label
End Sub

Checkbox returned in Event Handler isn't one on form

I'm creating an array of checkboxes on a form dynamically; the code that creates the array looks like this:-
checkbox_array(count_of_checkboxes) = New CheckBox
if (count_of_checkboxes = 0) Then
checkbox_array(count_of_checkboxes).Top = specimen_checkbox.Top
checkbox_array(count_of_checkboxes).Left = specimen_checkbox.Left
else
checkbox_array(count_of_checkboxes).Top = checkbox_array(count_of_checkboxes - 1).Top + vertical_offset
checkbox_array(count_of_checkboxes).Left = checkbox_array(count_of_checkboxes - 1).Left + horizontal_offset
End If
my_panel.Controls.Add(checkbox_array(count_of_checkboxes))
AddHandler checkbox_array(count_of_checkboxes).MouseClick, cbxSpecimen_CheckedChanged
checkbox_array(count_of_checkboxes).Name = someValue
checkbox_array(count_of_checkboxes).Text = someValue
checkbox_array(count_of_checkboxes).Enabled = true
checkbox_array(count_of_checkboxes).Visible = true
checkbox_array(count_of_checkboxes).Show()
This works fine and dandy on one form. However, I am using the same code on a form which is derived from a base form, and running into a problem, in that the object returned in the sender parameter, although clearly a checkbox with a recognisable name, isn't any of the checkboxes in the array.
I verified this with:-
Private Sub cbxSpecimen_CheckedChanged( sender As System.Object, e As System.EventArgs) Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To checkbox_array.GetUpperBound(0) - 1
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
End Sub
Can anyone shed any light on why this should work on a normal form, but not a derived-class form?
I verified this with:-
Private Sub cbxSpecimen_CheckedChanged( sender As System.Object, e As System.EventArgs)
Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To checkbox_array.GetUpperBound(0) - 1
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
End Sub
Why checkbox_array.GetUpperBound(0) - 1? This will skip the last element in the array. Try:
For i As Integer = 0 To checkbox_array.GetUpperBound(0)
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
Or
For i As Integer = 0 To checkbox_array.Length - 1
...
I have managed to get this to work by refilling the array of checkboxes inside the click event:-
Private Sub cbxSpecimen_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To check_boxes.GetUpperBound(0)
If check_box_array(i).Name = CType(sender, CheckBox).Name And
Not check_box_array(i).Equals(sender) Then
check_box_array(i) = CType(sender, CheckBox)
End If
Next i
' do useful work
End Sub
After the check box on the form has been stuffed back into the array, it remains there (so the second invokation for the same checkbox doesn't insert into the array a second time).
This seems like a ghastly hack to me, but I'll go with it for the time being.