i am making vb.net application in which there are 10 textbox in which i am changing background color when it got focus and lost focus. and adding validation number or character only. is there any way i can set or add custom code that every textbox added in form change color on got focus and lost focus and can assign textbox validation number only, alphanumeric. i don't want to add code on every event on keypress , gotfocus and lostfoucs. i just want to set it as default property of text box
here is my code
Private Sub txtProductDescc_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductDescc.GotFocus
txtProductDescc.BackColor = interfaceDesign.gotFocusTxtColor
End Sub
Private Sub txtProductDescc_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductDescc.LostFocus
txtProductDescc.BackColor = interfaceDesign.lostFocusTxtColor
End Sub
One way, which I am using myself often, is to make an array of textboxes. This way they all share the same "code" for every event and you can select them by index if you need to address a specific item.
Updated
You can also capture key events on the main form:
C# capture main form keyboard events
How do I capture Keys.F1 regardless of the focused control on a form?
Related
VB.net .. Currently I have two controls, one richtextbox and a textbox. The RTB is readonly, and also HideSelection is set to false.
The textbox is generic, it allows for input (to send data).
I want to be able to select things in the richtextbox without losing focus in the textbox. There is a client called 'mushclient' that does this, and it works pretty well. The text is still selected, but it doesn't lose the focus on the chatbar to type in.
I don't exactly know however how to prevent 'focus' though. At the moment it breaks flow when you are in game but want to copy something, you'll have to click the textbox again to start typing again. I understand I could setfocus after clicking the RTB, but this feels overall a bit odd. I was wondering if there is a more elegant solution.
Thanks!
This seems to work well for me. The TextBox does loose focus, but as soon as the Mouse_UP event fires, the selected text is copied to the clipboard and focus is sent back to the text box.
Public Class Form1
Dim LostFocusControl As Control
Private Sub RichTextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseUp
If RichTextBox1.SelectedText.Length > 0 Then
Clipboard.SetText(RichTextBox1.SelectedText)
End If
If Not IsNothing(LostFocusControl) Then
LostFocusControl.Focus()
End If
End Sub
Private Sub ControlLostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
LostFocusControl = Sender
End Sub
End Class
The code is a bit longer than it could be, but this makes it easier if later on you want to change the control that focus is returned to. To change the control that you want to return focus to, just change the name of the control that the handler is subscribed to e.g
Change
Private Sub ControlLostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
To
Private Sub ControlLostFocus(sender As Object, e As EventArgs) Handles Listbox1.LostFocus
or whatever the name of the control is that you want to return focus to.
is there a way to disable a textbox in code only when an even is triggered?
for example, a textbox wants user to enter an initial amount of money in a textbox. After the calculate button is clicked, make the textbox not editable.
im a beginner and my textbook doesnt mention it.
To disable textbox on button click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Enabled = False
End Sub
To make it Read Only
TextBox1.ReadOnly = True
The difference between Enabled and ReadOnly is :
Readonly allows the user to set focus to and select and copy the text but not modify it.
A disabled TextBox does not allow any interaction whatsoever.
Use ReadOnly when you have data that you want the user to see and copy, but not modify. Use a disabled textbox, when the data you are displaying is not applicable in for the current state of a dialog or window.
This is a VB.NET winforms project, using EF. The ComboBox is bound to a seperate datasource which contains the colums tName and tNumber. TName is a brief description of that value and tNumber is the actual number that is saved in the Financial Table under column named transaction_type. Everything works flawlessly on the display of existing data that is in the Financial Table.
The comboboxes for each item in the grid all show the correct description for the transaction_type. But when ever I change the value of any of the combo boxes and click the save button it does not save any value to the transaction_type.
Any ideas why this might be? Any missing code I will add if required..
The form_load event looks like this:
Private Sub paymentHistoryView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FinancialDataGridView.DataSource = db.financials.Where(Function(f) f.TenantId = tenentId).OrderBy(Function(f) f.transaction_date)
TranstypeBindingSource.DataSource = db.transtypes.OrderBy(Function(f) f.tNumber)
BindingNavigatorDeleteItem.Enabled = False
End Sub
And the savebutton click event is as follows:
Private Sub FinancialBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FinancialBindingNavigatorSaveItem.Click
db.SaveChanges()
End Sub
The properties for the ComboBox are shown Below:
It should be noted that all other changes to the datagrid are saved correctly when the save button is clicked... After further testing the value will actually save if the ComboBox is no longer selected. I guess a work around would be to focus on something else after the value of a comboBox is changed. If this seems like the best way how would I hook on SelectedIndexChanged event for comboBoxs in the datagridview???
I think that if you check, the same thing will actually happen with the other columns in the datagridview, if you do not move out of the cell before clicking the save button. The good news is that you only need to add one line of code.
FinancialDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
Now your SaveItem_Click event handler should look like this:
Private Sub FinancialBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FinancialBindingNavigatorSaveItem.Click
FinancialDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
db.SaveChanges()
End Sub
This will commit the dirty cell before the save.
See https://stackoverflow.com/a/6469559/269123
I have a gridview that is populated and a button outside the gridview that I want to enable editing on the selected row when clicked. I have this in the code behind. What goes in the btn_click event to invoke the grid view editing?
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEventArgs)
GridView1.EditIndex = e.NewEditIndex
FillGrid()
End Sub
Protected Sub btnEdit_Click(ByVal sender as Object, ByVal e As System.EventArgs) Handles btnEdit.Click
What goes here??
End Sub
There is a problem with this approach.
"GridView1_RowEditing" is expecting a row index, so it can turn on "EditItemTemplate" accordingly, correct?
But If you want to click on button outside of Gridview and make entire Gridview editable, you shouldn't trigger GridView1_RowEditing, since you don't know what editindex to pass.
You need to implement editable control(textbox) as part of "ItemTemplate", not in "EditItemTemplate".
And visibility of this control would be controlled by the outside button you have created, which will flag the visibility on / off.
Please review following link, this demonstrates how it should be implemented.
http://highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx
I'm making a desktop application in vb.net. When I click the back button (placed by me) on any form it hides current form and shows previous form but when i go again to the form from which i had hit back button then the focus cue still remains on the back button but I want the focus to be on the first textbox on that form or any specific button on which I want.....How can i achieve this...
I have already used some code to shift focus from one textbox to another when i press enter key...but it doesn't work in the above mentioned case....I'm using vb.net in visual studio 2005...
This is the code i'm using for shifting focus among textboxes
Private Sub party_code_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles party_code.KeyDown
If e.KeyData = Keys.Return Then
party_name.Focus()
End If
End Sub
Can anyone help me on this????
You need to add a handler for either the form's Activated event:
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
party_name.Focus()
End Sub