Let me start off with saying I have looked around and I guess my question is too specific to get any straight forward results. Also this is my first VB project.
I am trying to make a simple point of sale GUI. I have two textboxes (Price, Cash tendered) and 11 buttons making a number keypad. What I want to do is be able to click on the number keypad buttons (for example, buttons "1" and "0" to enter 10 dollars) and it go to the textbox that the cursor last clicked on.
So here is an example: I click on the textbox "Price" then click the button "1" and the button "0". This will then type 10 to the "Price" textbox. After that, I want to be able to click on the "Cash Tendered" textbox and be able to use the same number keypad buttons as before for the same operation.
A picture of the GUI is available.
You could use a boolean to keep track of which textbox was last selected
When you click in the Price textbox set the boolean as true. When you click in the Cash Tendered textbox set the boolean as false. When you click one of the number buttons in the keypad, test the boolean value to see which textbox was last selected, then enter the numbers into that textbox.
Example:
Public Class Form1
Dim lastClickedTextBox1 As Boolean
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
lastClickedTextBox1 = True
End Sub
Private Sub TextBox2_Click(sender As Object, e As EventArgs) Handles TextBox2.Click
lastClickedTextBox1 = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If lastClickedTextBox1 = True Then
TextBox1.Text = "Box 1 was it"
Else
TextBox2.Text = "Box 2 was it"
End If
End Sub
End Class
Related
I'm having an issue with my ComboBoxes whereby if I type into it to get a value & then tab out the Text changes to the first item in the list with the first letter typed.
I have:
AutoCompleteMode set to SuggestAppend
AutoCompleteSource set to ListItems
DropDownStyle set to DropDownList
I add the items for the ComboBox in the Load event of the Form the ComboBox is on.
e.g. the below is code from a Load event where I populate a ComboBox that I have set up as below.
`Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")`
After running the code, if I select the ComboBox1 & type in common - common is selected in ComboBox1 but if I leave ComboBox1 the Text reverts to combo.
It gets a bit stranger as if I user the below code in the ComboBox1_Leave event procedure it throws common:
MsgBox(ComboBox1.Text)
I've also tried assigning the value of Text to a string in the ComboBox1_KeyUp event procedure & then assign that to ComboBox1.Text in the ComboBox1_Leave event procedure but that doesn't do anything.
If I put a the above MsgBox code before assigning the strings value to ComboBox1.Text then the Text value does revert to Common but this is isn't a practical solution.
I've also noticed that if I hit Enter before hitting tab it retains the correct value but again I'm don't think this is a particularly practical solution.
Does anyone have any idea what's going on here & how I can fix it?
It is absolutely necessary to have the DropDownStyle set to DropDownList?
Because if you set DropDownStyle to DropDown the selected value will be retained when you press tab or lose the focus.
If it's absolutely necessary to have it that way, you could try this.
Public Class Form2
Dim selectedTextForCombo As String = ""
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")
End Sub
Private Sub ComboBox1_LostFocus(sender As Object, e As System.EventArgs) Handles ComboBox1.LostFocus
ComboBox1.SelectedItem = selectedTextForCombo
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
selectedTextForCombo = ComboBox1.Text
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
End Class
Warning:
This example works with the tab action.
If the users writes something that doesn't exist like "commun" the
selected value will end up being the visually selected value, in this
case: "common"
to keep it short and simple, I am attempting to display a number selected through a combo box on a second form through a label.
Here are the bits that are relevant to the issue I am having:
for i = 1 To 31
cmb_days.Items.Add(i)
next
' Populating combo box
days = cmb_days.text
frm_result.lbl_renting = "Renting for " & days & " Days"
I have tried using other variants such as cmb_days.selecteditem to no avail
I am also kinda having issues with like telling my code to do things with whatever number is actually selected in the combo box, idk I am veryyyyy new
That looks more or less correct.. But the question is where your call to update the label occurs in your code...
This is a minimal working example, with just a ComboBox on a form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 31
ComboBox1.Items.Add(i)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Text = ComboBox1.Text
End Sub
End Class
The update of the label (in this case the text of the form, but it works exactly the same with a label on a different form) occurs in the SelectedIndexChanged event of ComboBox1.
If you copied your code straight from the project, you haven't had time to select anything yet, so the Text property will be empty.
I have a calculator with two textboxes. The user enters a number in each one, then presses a button to add, subtract, multiply, or divide the two numbers, with the results in a label. The user should click a save button, where the textbox.text is saved as an arraylist. Then the user should click the display button to display the text, but this has to happen up to ten times (ten saves). I can save it once, but can't figure out how to add more than one to the arraylist. Here is my code:
Public Sub btnStore_Click(sender As Object, e As EventArgs) Handles
btnStore_Click
Session("myArray") = Input
i = Convert.ToInt32(Session("increment"))
Input.Insert(i, txtResults.Text)
Session("myArray") = Input
Session("increment") = Convert.ToInt32(Session("increment")) + 1
End Sub
Public Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles
btnDisplay.Click
output1.Text = Convert.ToString(Session("myArray"))
End Sub
Good day everyone.
I need your help in this project I am into (a Visual Basic program with no database.) It just contains a Datagridview, a Textbox, and three buttons (an "Add" Button, a "Edit" and an "Update" Button).
1 . Is there any way (like using "for loop") to automatically assign DataGridView1.Item("item location") to the one edited and be updated?
2 . Or is it possible to just click an item in the Datagridview then it will be edited at that without passing it to a Textbox, and to be updated at that.
The DataGridViewCellEventArgs variable (e in the method stub the designer will generate for you) of the double click event of the cell has RowIndex and ColumnIndex properties which refer to the position of the cell you clicked.
Save those (in a class variable possibly or a local one if that's all you need) and then refer to them when you update the cell in your DataGridView, possibly like this MyDataGridView.Item(e.ColumnIndex, e.RowIndex) or MyDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex) where e is the variable from the double click event handler.
For you cell double click event you could have something like this:
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Using myEditor As New frmCellEditor(Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value)
If myEditor.ShowDialog() = DialogResult.OK Then
Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = myEditor.NewCellValue
End If
End Using
End Sub
This will call a new instance of your editor and get a value from you. For the purpose of this demo I have made a form like this:
Public Class frmCellEditor
Public NewCellValue As Integer
Public Sub New(ByVal CurrentCellValue As Object)
InitializeComponent()
Me.TextBox1.Text = CStr(CurrentCellValue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.NewCellValue = CInt(Me.TextBox1.Text)
Me.DialogResult = DialogResult.OK
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Call Me.Close()
End Sub
End Class
Which just has two buttons (Button1 = OK, Button2 = Cancel). When you click OK, it just returns the value 1 which then gets set as the value of the cell.
This is a VERY simplistic example, but it should provide you the basics of what you are trying to do.
UPDATE:
I updated the code for the editor interface so it will include handling for passing the value back and forth from the form with your datagridview.
In your project, make a new form called frmCellEditor. This forms has to have two buttons and a textbox (Make sure that the programmatic names match!). Replace the code with the code listed above. You will have to add Imports System.Windows.Forms above the class as well.
Amend the event handler for the cell double click event of your datagrid to pass the cell value when frmCellEditor is constructed (the line going ... New frmCellEditor(...).
How many columns does your DataGridView has?
Based on how you populate your DataGridView, I'll assume only 1.
Declare this on top of your form
Dim i as Integer
On your btnUpdate_Click Event (Just combine your Edit and Update button into One)
SELECT CASE btnUpdate.Text
Case "Update"
With DataGridView1
'Check if there is a selected row
If .SelectedRows.Count = 0 Then
Msgbox "No Row Selected for Update"
Exit Sub
End If
i = .CurrentRow.Index 'Remember the Row Position
Textbox1.Text = .item(0 ,i).value 'Pass the Value to the textbox
.Enabled = False 'Disable DataGridView to prevent users from clicking other row while updating.
btnUpdate.Text = "Save"
End With
Case Else 'Save
DatagridView1.Item(0,i).Value = Textbox1.Text
btnUpdate.Text = "Update"
END SELECT
Thanks for those who contributed to finding answers for this thread. I have not used your solutions for now (maybe some other time). After some research, I've found an answer for problem 2 (more user friendly at that):
2 . Or is it possible to just click an item in the Datagridview then
it will be edited at that without passing it to a Textbox, and to be
updated at that.
Here's what i did:
in Private Sub Form1_Load, just add:
yourDataGridView.EditMode = DataGridViewEditMode.EditOnEnter
in Private Sub yourDataGridView_(whatever event here: DoubleCellClick, CellContentClick, etc.) add:
DataGridView1(e.ColumnIndex, e.RowIndex).[ReadOnly] = False
DataGridView1.BeginEdit(False)
I have a form with + 50 controls and with key numeric screen to write in only 5 textbox of this form.
I am using this code to write in these textbox using key numeric screen:
Private Sub bt0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt0.Click, bt1.Click, bt2.Click, bt3.Click, bt4.Click, bt5.Click, bt6.Click, bt7.Click, bt8.Click, bt9.Click, btDec.Click
If TypeOf sender Is DevExpress.XtraEditors.SimpleButton Then
txtRefe.Focus()
SendKeys.Send(CType(sender, DevExpress.XtraEditors.SimpleButton).Text)
End If
End Sub
The problem: I need to know which of these 5 textbox had the focus before touching the numerical button
I have seen this code from this post Find out the control with last focus to find last focus:
Private _focusedControl As Control
Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As EventArgs)
_focusedControl = DirectCast(sender, Control)
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If _focusedControl IsNot Nothing Then
'Change the color of the previously-focused textbox
_focusedControl.BackColor = Color.Red
End If
End Sub
But how I do it in a form with + 50 controls (Controls of many types: buttons, checkbox, combos, textbox, etc.) ?
You can recursively loop through the form's Controls collection and add event handlers.
The simplest and most trivial solution that comes to anyone's mind is to make a flag holding a datatype of your choice. Let's say int, then what you have to do is to update that flag with a different value for each focus event of each textBox. For the sake of simplicity,
When textBox1 gets focused -> set the flag value to 1
when textBox2 gets focused -> set the flag value to 2
when textBox3 gets focused -> set the flag value to 3
when textBox4 gets focused -> set the flag value to 4
when textBox5 gets focused -> set the flag value to 5
Now you are keeping record for the last focused textBox. You can do a switch statement to handle each of the 5 cases we have
Select Case flag
case 1: 'code for textBox1
case 2: 'code for textBox2
case 3: 'code for textBox3
case 4: 'code for textBox4
case 5: 'code for textBox5
End Select