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
Related
I have a group box with multiple Checkboxes(food item) and each one has a corresponding NumericUpDown control(quantity). For context, it is for a project based on a restaurant menu. I want to hide a button called btnSave whenever either a checkbox is unchecked or the quantity (NumericUpDown) is changed. I currently have btnSave.Hide under the CheckBox1_CheckedChanged and NumericUpDown1_CheckedChanged SubProcedures but I want to know if there's a way to do this when anything within this group box is changed instead of putting the code under each SubProcedure. Thanks
I think you meant .ValueChanged for the NumericUpDown control. (There is no .CheckedChanged) Although this doesn't matter much in this case, this is a good pattern for future reference. Instead of calling an event call a Sub from your events.
When you have several controls responding to a single Event handler, you can find out which control triggered the event by checking the sender parameter. Since, as you can see, sender is an Object you will have to cast it to the appropriate type to get the properties of a CheckBox.
Private Sub HideSaveButton()
btnSave.Hide
End Sub
Private Sub CheckBoxInGroupBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
HideSaveButton()
Dim WhichCheckBox As CheckBox = DirectCast(sender, CheckBox)
Select Case WhichCheckBox.Name
Case "CheckBox1"
MessageBox.Show("CheckBox1 has changed")
Case "CheckBox2"
MessageBox.Show("CheckBox2 has changed")
End Select
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
HideSaveButton()
End Sub
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 developed an application which is like a digital comprehension sheet activity.
The order of which this works is:
A user double clicks an underscored text area on a text box
A form opens which asks them to place an answer
The answer is validated, if true it goes back to the original form
The issue here is that for some odd reason my labels don't update on step 3, as- well as the text. I have a label named lbAnswerStatus which notifies the user if they have a correct answer, the text does not update, and the 'SelectedText' on Form 1 should be replaced with the answer (if correct)
Here is my code:
Form 1 (when textbox is clicked):
Form2.Answer(answers(counter))
answers(counter) represents the correct answer being passed on, to compare with users answer in form 2
Form 2:
If tbAnswer.Text = theanswer Then
Form1.answerStatus(True, theanswer)
Form 1:
Public Sub answerStatus(status, answer)
If status = true Then
Form2.Close()
rtb.SelectedText = answer
lbAnswerStatus.ForeColor = Color.Green
End If
End Sub
My first assumption was that the Rich text box's SelectedText wasn't changing because it didn't have focus on it however, the lbAnswerStatus color didn't change either, so I figured that there was issues with making modifications to the form.
I used a message box to test whether lbAnswerStatus.Text would pop up and it did, so it can read but not write.
I also attempted to change the text of the label and selectedtext of the textbox in step 1 and it worked fine.
Any ideas what may be causing this issue?
Thanks.
I guess that you want your Form2 (AnswerForm) presented as a modal dialog. Doing that, you can return a result. You didn't say what you want to happen to the answer status label or the answer form if the supplied answer is incorrect.
Just as an example of how it can be done, create a new Windows Forms project. On Form1, place a button (Button1) and a label ("lblAnswerStatus"). Add a new form named "AnswerForm" to the project, and add a TextBox ("TextBox1") and a button ("bnDone").
As code for AnswerForm:
Public Class AnswerForm
Private statusLabel As Label
Private answerText As String
Private Sub bnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnDone.Click
If TextBox1.Text.Equals(answerText, StringComparison.CurrentCultureIgnoreCase) Then
' Alternative 1: set the color here if you want to
statusLabel.ForeColor = Color.Green
' return a value indicating success
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
Else
' indicate error
statusLabel.ForeColor = Color.Red
End If
End Sub
Public Sub New(ByVal statusLabel As Label, ByVal answerText As String)
InitializeComponent()
Me.statusLabel = statusLabel
Me.answerText = answerText
End Sub
End Class
and as code for Form1:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lblAnswerStatus.ForeColor = Color.Blue
Using answerFrm As New AnswerForm(lblAnswerStatus, "X")
Dim dlgResult = answerFrm.ShowDialog()
' Alternative 2: use this if you want to change the color in this handler
If dlgResult = DialogResult.Yes Then
lblAnswerStatus.ForeColor = Color.Purple
End If
End Using
End Sub
End Class
If you click Button1 on Form1, a dialog box appears. Type into its TextBox1 and click bnDone. Because the instance of AnswerForm has a reference to lblAnswerStatus (supplied in its constructor), it is easy to update the latter, e.g. it turns red if you enter the wrong answer.
This question already has answers here:
how to set a code so that mouse is clicked in the textbox to enter info? [closed]
(2 answers)
Closed 9 years ago.
In Visual Basic 2010. I have two textbox and an onscreen number keyboard. Every time I click a number, the number is shown in both the textboxes. How do I make it so I need to click into the textbox first then the number is only entered into that textbox?? This is what I have in my code and it is not working. The numbers I tried to enter to the mtbNum textbox and txtQuantity1 is only shown mtbNum textbox no matter which textbox I clicked on. My professor said to change the click event of the textboxes to textbox_click. However I don't know where to go from there. Can someone please help me? How do I change it so that the textbox has to be clicked for the number to be entered? Please and thank you.
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
If mtbNum.Focus = True Then
mtbNum.Text += "5"
ElseIf txtQuantity1.Focus = True Then
txtQuantity1.Text += "5"
Else
End If
End Sub
Private Sub mtbNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mtbNum.Click
End Sub
Private Sub txtQuantity1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQuantity1.Click
End Sub
not clear sure what you want, but I think you are looking for this. I'll delete this if this is not what you are looking for.
solution 1:
if txt1.setFocus=true then
'do the button code
else if txt2.setFocus=true then
'do button code
end if
solution 2:
*better yet, have a checkBox so that the user could clearly choose which textbox he/she will enter, say :
if chk1.checked = true then
txt1.setFocus
'do button code
else if chk2.checked = true then
txt2.setFocus
'do button code
end if
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