I have been battling with this for three days and asked a LOT of people the question, but maybe I'm asking it wrong. The project that I'm working , is to create a digital safe lock. I need to achieve the following:
I created a keypad with six buttons in a group box. When one click on the buttons, then the digit being clicked must be displayed in the listbox. As you press the rest (up to six digits max) the all have to be displayed in the listbox, next to each other in sequence like the way one will press the buttons on a calculator and then the listbox must display them in the same way. Here is whet I have figured out up to date:
Public Class Form1
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If pinlst.Items.Count = 0 Then
pinlst.Items.Add(btn1.Text)
Else
pinlst.Items(0).SubItems.Add(btn1.Text)
End If
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If pinlst.Items.Count = 1 Then
pinlst.Items.Add(btn2.Text)
Else
pinlst.Items(1).SubItems.Add(btn2.Text)
End If
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
If pinlst.Items.Count = 2 Then
pinlst.Items.Add(btn3.Text)
Else
pinlst.Items(2).SubItems.Add(btn3.Text)
End If
End Sub
Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
pinlst.Text = btn4.Text
End Sub
Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
pinlst.Text = btn5.Text
End Sub
Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
pinlst.Text = btn6.Text
End Sub
Private Sub btngp_Enter(sender As Object, e As EventArgs) Handles btngp.Enter
pinlst.Text = pinlst.Text & btn1.Text
pinlst.Text = pinlst.Text & btn2.Text
pinlst.Text = pinlst.Text & btn3.Text
pinlst.Text = pinlst.Text & btn4.Text
pinlst.Text = pinlst.Text & btn5.Text
pinlst.Text = pinlst.Text & btn6.Text
End Sub
End Class
It's best to assign every button to a character being pressed or a function being performed, in design time. For this to work, you need to have every button as a custom control, based off a regular button of course, exposing a property "CharPressed" or something like that. Then you can have a single static handler for all buttons, which you will wire dynamically in Sub New, after InitializeComponent. If this does not make sense, please feel free to ask a question in comments.
Related
I created a mouse position program that can be used to save your mouse position {X, Y}
I realised that this is not going to be effective unless I implement a method where for example pressing "5" will save that position
The only way i can save the position is by pressing the button, although that does work, there is no way to save the position without clicking the btn.
Can anyone help? I would be very grateful
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub XYbtn_Click(sender As Object, e As EventArgs) Handles XYbtn.Click
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
TimeCo.Start()
End Sub
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
LabelX.Text = "X"
LabelY.Text = "Y"
X2.Text = "X2"
Y2.Text = "Y2"
End Sub
Private Sub TimeCo_Tick(sender As Object, e As EventArgs) Handles TimeCo.Tick
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
End Sub
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
LabelX.Text = Cursor.Position.X
LabelY.Text = Cursor.Position.Y
End Sub
Private Sub save2_Click(sender As Object, e As EventArgs) Handles save2.Click
X2.Text = Cursor.Position.X
Y2.Text = Cursor.Position.Y
End Sub
Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
End Sub
End Class
If your form will have focus, you can set the AcceptButton property of the FORM to saveBtn. This will make it so that when you press ENTER on the keyboard while your form has focus then that button will be pressed.
If you'd rather use the key approach then set the KeyPreview property of the Form to True and handle the KeyPress event:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = "5" Then
Console.WriteLine("5")
End If
End Sub
I am currently enrolled in a visual basic class through my university and I am having trouble with one of the exercises assigned to me. I need to display the cost of an item for a company and I cannot get anything to display in a label.
This is the code
Option Explicit On
Option Strict On
Option Infer Off
Public Class MainForm
Private Sub BtnCalc_Click(sender As Object, e As EventArgs) Handles BtnCalc.Click
Dim intCost As Double
If RadTwin.Checked Then
intCost = 39.99
ElseIf RadFull.Checked Then
intCost = 49.99
ElseIf RadQueen.Checked Then
intCost = 49.99
ElseIf RadKing.Checked Then
intCost = 69.99
End If
lblCost.Text = intCost.ToString("n")
End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private Sub RadTwin_CheckedChanged(sender As Object, e As EventArgs) Handles RadTwin.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadFull_CheckedChanged(sender As Object, e As EventArgs) Handles RadFull.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadQueen_CheckedChanged(sender As Object, e As EventArgs) Handles RadQueen.CheckedChanged
lblCost.Text = String.Empty
End Sub
Private Sub RadKing_CheckedChanged(sender As Object, e As EventArgs) Handles RadKing.CheckedChanged
lblCost.Text = String.Empty
End Sub
End Class
I need to display the cost of each item in the lblCost when the radio button is chosen. I have worked at getting this to work for hours but I am not knowledgeable enough to know what to do. If anyone has any ideas on how to make this code work I would greatly appreciate it. It is likely something stupid simple that I am missing but I want to learn how to solve this problem if it happens again in the future.
I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class
Many developer can write real time tooltip for him self.
So, if you don't understanding my question, here is example
I want the tooltip following my cursor with smooth, can anybody tell me the code?
This will do what you asked for. As I said, you simply have to show the ToolTip yourself and not rely on the system to do it for you.
Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
Dim text = e.Location.ToString()
If text <> Me.ToolTip1.GetToolTip(Me.Button1) Then
Me.ToolTip1.Show(e.Location.ToString(), Me.Button1, New Point(e.X + 20, e.Y + 20))
End If
End Sub
Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
Me.ToolTip1.Hide(Me.Button1)
End Sub
If you want the tooltip to follow the mouse, maybe use a timer and something like this
Public Class Form1
Private toolTipMsg As String = "Busy, Please Wait..." & vbCrLf & "Line2 Blah bla bla."
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ToolTip1.Active = True
ToolTip1.IsBalloon = False
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
' get cursor position
Dim cp = Cursor.Position
' offset Y so tooltip is under mouse
cp.Y += CInt(Cursor.Size.Height * 1.5)
' show tooltip
ToolTip1.Show(toolTipMsg, Me, PointToClient(cp))
End Sub
So I'm doing this calculator program and need the numbers, the operator used and the "=" sign to show up in the listbox so "1 + 1 = 2" should show up. I have the calculator working and can move items to the listbox, but can't figure out how to remember the button that was clicked to get the outcome and move it over also.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & NumTwo.Text & Result.Text)
End Sub
End Class
As far as you just need to store one character, you can rely on the Tag property of ListBox1 (basically, a black box where you can store anything you want). Sample code:
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
ListBox1.Tag = "+"
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
ListBox1.Tag = "-"
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
ListBox1.Tag = "x"
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
ListBox1.Tag = "/"
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & "=" & Result.Text)
End Sub