Get Own control values in visual basic 10 - vb.net

I'm looking for a way to get the data of control (in this case a Trackbar) inside the trackbar itself.
I got this:
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Label1.Text = TrackBar1.Value
End Sub
But I want to replace that "trackBar1." with something like
Label1.Text = THIS_CONTROL.Value
Is this possible in Vb10 ?
Thanks

Use the sender argument:
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Dim THIS_CONTROL As TrackBar = DirectCast(sender, TrackBar)
Label1.Text = THIS_CONTROL.Value
End Sub

Related

Use a textbox to store a numeric counter in VB.Net

I want to count with a TextBox.
Here is my code:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
OrainsProgressBar1.Increment(1)
If OrainsProgressBar1.Value = 100 Then
Timer3.Start()
Timer1.Stop()
End If
End Sub
Private Sub OrainsTheme1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsTheme1.Click
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Start()
Timer2.Start()
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
OrainsProgressBar1.Increment(-1)
If OrainsProgressBar1.Value = 0 Then
Timer1.Start()
Timer3.Stop()
End If
End Sub
Private Sub OrainsButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsButton1.Click
OrainsTextBox1.Text += 100
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsTextBox1.Text += 1
End Sub
End Class
But I have an error with OrainsTextBox1.Text += 1. VB says:
'Conversion from string "" to type 'Double' is not valid.'
What is the problem?
In the .Net world, the data types of things matter a lot. Strings (like the .Text property) are NOT numbers. You need to convert. Even if someone only enters the digits 0-9 into the textbox, that's still a string of numeric characters, rather than a number. And what should happen if someone enters random text into that textbox that won't convert to a number type at all?
For this code, I suggest building a property, like this:
Private _orainsValue As Double
Public Property OrainsValue As Double
Get
Return _orainsValues
End Get
Set
_orainsValue = Value
OrainsTextBox1.Text = _orainsValue.ToString()
End Set
End Property
That will let you write code like this and have the expected result shown to the user:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsVale += 1
End Sub
Note that it does mean you will want to mark the TextBox disabled,though, because this doesn't account for user data entry.
Instead of doing like this OrainsTextBox1.Text += 1
do like this OrainsTextBox1.Text = Val(OrainsTextBox1.Text) + 1
Because .Text is string. Which will append the 1s as "11111111"

Replace string if followed by any character visual basic

I am new to Visual Basic. I want to use the like operator in a textbox to change a character if it is followed by any other character. But it should be on the key-up event.
Anyone please help me: how I can make the following code work?
Public Class Form1
Dim myString As String
Dim sMatch As Boolean = myString Like "x?"
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If sMatch = True Then
TextBox1.Text = TextBox1.Text.Replace(myString, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myString = "x"
End Sub
End Class
Try this and ask. Your variable myString was never getting it's value from the TextBox. You just set it to x in the load event.
Public Class Form1
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If TextBox1.Text Like "x?" Then
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "x"
End Sub
End Class

How to make a picturebox hide when dragged over another picturebox?

So I nailed the ability to drag a picturebox around the Windows form. I need it to hide itself when it's dragged and dropped over another picture. I've tried a few methods but none seem to work and I am now back at square one, the only code I have is so that I can move the picturebox around the form.
I Solved Your Problem
Here Is How The Form Should Look Like
And Here Is The Code I Made:
Private Sub CheckTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
CheckTimer.Stop()
CheckTimer.Interval = 1
If PictureBox2.Location = New System.Drawing.Point(TextBox1.Text, TextBox2.Text) Then
PictureBox2.Visible = False
End If
CheckTimer.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = PictureBox1.Location.X
TextBox2.Text = PictureBox1.Location.Y
CheckTimer.Start()
End Sub
Hope This Code Was Helpful To You.
I made an better code than the old one, try it.
Here Is How The Form Should Look Like:
and here is the code :
Public Class Form1
Dim Point As New Point
Dim X, Y As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Timer1.Interval = 1
Point = Cursor.Position
PictureBox2.Location = New System.Drawing.Point(Point.X - X, Point.Y - Y)
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
X = Cursor.Position.X - PictureBox2.Location.X
Y = Cursor.Position.Y - PictureBox2.Location.Y
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
Timer1.Stop()
If PictureBox2.Location.X < PictureBox1.Size.Width Then
PictureBox2.Visible = False
End If
End Sub
End Class
I hope this code was useful to you.

IF statement not working as I expected

Hi so im playing with microsoft visual studio 2010 edition and I need to make an if statement work on it.
What im doing is putting a number into a textbox, it then goes into my listbox (however many times I put numbers in like 3-4 numbers). Then it is meant to add them all up and put it into the label after.
Heres the program so far
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Result As Integer
For Each Item As Integer In ListBox1.Items
Result = Result + Item
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Value As String
Value = TextBox1.Text
ListBox1.Items.Add(Value)
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Label1.Text = "You have a Balance of " + DialogResult.ToString
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If (String.a
End Sub
End Class
In Visual Basic .NET (which is what your code looks to be in), If statements are formatted like this:
If condition Then
DoSomeCodeHere()
End If
C# code is formatted as
if (condition)
{
DoSomeCodeHere();
}
Do you mean you wan the sum all the amount in the list box and show it in a label after clicking button 2?
If yes add the below should do.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Result As Integer
For Each Item As Integer In ListBox1.Items
Result = Result + Item
Next
Label1.Text = "You have a Balance of " + Result.ToString
End Sub

how disable text box from two give text box?

I have 2 text box in one vb form. if txtMaterial was fill i want to disable the txtPackage and vice versa. I use the code below, but it didn't work. could someone fixed it.
Really appreciate it. tq.
Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
txtMaterial.Enabled = True
txtPackage.Enabled = False
End Sub
Private Sub txtPackage_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPackage.TextChanged
txtPackage.Enabled = True
txtMaterial.Enabled = False
End Sub
Make both TextBoxes fire the same handler, then simply set the Enabled() state of each one based on whether the other TextBox has something in it:
Private Sub txtChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged, txtPackage.TextChanged
txtMaterial.Enabled = (txtPackage.TextLength = 0)
txtPackage.Enabled = (txtMaterial.TextLength = 0)
End Sub
*Note the ending of the first line has both controls listed using Handles txtMaterial.TextChanged, txtPackage.TextChanged at the end.
Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
If txtMaterial.Text <> "" Then
txtPackage.Enabled =False
End If
End Sub
based on the comment that you added
Blockquote i want to make it disable as the second one. but in my case if i fill the txtMaterial the txtPackage also can be fill with text at the same time.
the solution to your problem must be this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtPackage.Enabled = False
End Sub
Private Sub txtMaterial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
If Not String.IsNullOrEmpty(txtMaterial.Text) Then
txtPackage.Enabled = True
Else
txtPackage.Enabled = False
End If
End Sub