I'm using custom buttons to create a virtual keyboard. These buttons use the text of the button as output. However there are keys like Backspace, Space, Tab, etc... That while I want to display a text on them I need to send the corresponding codes like {BACKSPACE}, " ", {TAB} etc...
Someone (Jimi) suggested the use of Dictionary to map the text to the corresponding codes, and I have found examples for comboboxes but I don't really find anything to do it with buttons in vb.net.
Right now my form only has TextBoxes and DataGridViews for user input which gets picked up from button click with:
If TypeOf ActiveControl Is TextBoxBase Then
SendKeys.Send(selectedButton.Text)
ElseIf TypeOf ActiveControl Is DataGridView Then
Dim ctrl = DirectCast(ActiveControl, DataGridView)
If TypeOf ctrl.CurrentCell IsNot DataGridViewTextBoxCell Then Return
SendKeys.Send("{F2}")
SendKeys.Send(selectedButton.Text)here
So I guess my question is how can I change that text with a dictionary when the text of a button gets picked up. I tried:
Dim ButtonSurrogateKeys As New Dictionary(Of String, String)()
ButtonSurrogateKeys.Add("Borrar", "{BACKSPACE}")
Where "Borrar" is the button text and {BACKSPACE} what I want it to change to when clicked.
Thanks in advance.
Related
Probably it's very easy, but I have searched for a post or somehone who have the same problem without fortune. I have a Listbox in Vb Net, which contains a list of names.
in vb6, while typing in the listbox the selected item changed automatically based on the letters typed until completion, but I can't find a method that repeats the same thing in VS, as the only thing it lets me do is identify only the first one letter typed in the listbox. So, if in the list there are two similar names like Autocarro or Automobile, if after the 'A' I type the 'U' the cursor moves to the 'U' of 'Urban'.
Could anyone help me find a solution whithout using a textbox?
Thanks in advance
You can use a ComboBox with the DropDownStyle set to Simple. This displays a TextBox combined with a ListBox (hence the name Combo Box).
To make it select entries automatically you can add a TextChanged event handler with the following code:
private void ComboBox1_TextChanged(object sender, EventArgs e)
{
int inputLength = comboBox1.Text.Length;
if (inputLength > 0) {
int index = comboBox1.FindString(comboBox1.Text);
if (index >= 0) {
comboBox1.SelectedIndex = index;
comboBox1.Focus();
comboBox1.SelectionStart = inputLength;
comboBox1.SelectionLength =
((string)comboBox1.Items[index]).Length - inputLength;
}
}
}
Note, however, that editing the text box part becomes a bit difficult as the selection mechanism kicks in every time you edit the text. E.g. deleting text with Dellete or Backspace does not work well, as the automatic selection restores the text immediately. You can delete the whole text by typing Ctrl-A, Delete.
ComboBox after having typed "cl":
Try this (Works both for text box and combobox, not sure if it works directly in list box, you can try in this lime), what you can do is add textbox on top of list box and run second code to add data to textbox as suggestion
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
Or this one
Dim iddata As New AutoCompleteStringCollection()
Dim idtable As New DataTable
idtable = (your datatable source)
For Each r As DataRow In idtable.Rows
iddata.Add(r("id").ToString)
Next
With Textbox1
.AutoCompleteCustomSource = iddata
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteSource = AutoCompleteSource.CustomSource
End With
My code creates a 5x5 grid of buttons. I am wanting to give each of these buttons different names "BtnColour1", "BtnColour2", etc. How do I give them all different names and how do I refer to each button later in the program?
Dim bytCounter As Byte
For bytCounter = 1 To 25
Dim btnColour As New Button
Me.Controls.Add(btnColour)
btnColour.Height = 50
btnColour.Width = 50
btnColour.Name = "btnColour" & bytCounter
btnColour.Enabled = False
btnColour.Left = ((bytCounter - 1) Mod 5) * 51
btnColour.Top = ((bytCounter - 1) \ 5) * 51
AddHandler btnColour.Click, AddressOf BtnClick
Your code (I guess you forgot the ending Next) does create 25 Buttons, with names btnColour1... btnColour25.
In the BtnClick event, to get the name of the clicked button, you should write something like:
Private Sub BtnClick(sender As Object, e As EventArgs)
Dim buttonName as string=CType(sender, Button).Name
'buttonName now has the clicked button name
End Sub
Of course, since you set the enabled property to False, your button click event will not fire.
In a general sense (and in addition to Spyros' answer, which is a good way to do it in an event handler - the sender is always the thing that raised the event), when you give a control a name and add it to a control's Controls collection, you can then retrieve it by that name later:
'Here you added the button to the form controls:
Me.Controls.Add(btnColour)
'later in the code you can ask for it back by name, for example:
Dim controls = Me.Controls.Find("btnColour1")
What you get back is an array of Controls. You get an array because Find can search all children (panels inside panels inside groupboxes inside forms etc) and it is thus conceivable that multiple controls in different panels will both have the same name. In your case if you know you only have one control called "btnColour1" it's safe to get it by array index:
Dim control = controls(0) 'controls variable is from the above Find
Lastly, remember that it comes back as a Control, the parent class for all controls. Because you know it's a button, it's safe to cast without check:
Dim button = DirectCast(control, Button)
Remember that if your property is available on the base Control class you don't even need a cast:
'here's a 1 line way to get the text of the button named btnColour1
'Find all controls named btnColour1, take the first, get the text
Dim t = Me.Controls.Find("btnColour1")(0).Text
If you want to refer to the buttons later in the program to change a setting without clicking the button, you can add each button to an array and call each of them with an index number:
Dim buttons(24) As Button
Then as the buttons are created, you can add each button to the array:
Dim bytCounter As Byte
For bytCounter = 1 To 25
Dim btnColour As New Button
Me.Controls.Add(btnColour)
buttons(bytCounter) = btnColour
you can then reference each button and their properties using the index number of the button in the array. You may also want to add a specific tag to each button to make each button more unique using:
btnColour.Tag = bytCounter
I am making small restaurant system project. I have 16 buttons as tables in the restaurant. I would like to change their colors or disable any of them when some events trigger (the form is loaded).
I save my button names in format TableX_ButtonY
I used a for-loop to change theirs border colors like this:
CType(Me.Controls.Find(String.Format("Table{0}Button{1}", i, x), True)(0), Button).FlatAppearance.BorderColor = Color.Blue
It will be great if I can save these buttons as a list, so I can manage it more easily.
I name their tags from 1 to 16 but I don't know how to use them correctly. Because the trigger not based on button click but rather based on the Load Form event.
Buttons are already in a collection and is a bit redundant to add them to a generic collection. In this example there are 2 buttons in a group controls collection, which could very well be any applicable container.
Dim ReservedTables() As Integer = {5, 10, 15, 20}
For Each Btn As Button In GroupBox1.Controls.OfType(Of Button)
If ReservedTables.Contains(CType(Btn.Tag, Integer)) Then
Btn.Enabled = False
End If
Next
Dim TableList As New List(Of Button)
TableList.Add(TableX_ButtonY)
For Each Table As Button in TableList
'do stuff
next
if you generated the buttons by using the designer, you can use the method you described in your question to add them all to the list
I need to read a datagrid numbers of items, and programmatically add tabs to one tabControl. No problem on reading the datagrid, no problem in creating the model in the tabcontrol. So, I read the number of items, create the tabs accordingly, with all textboxes already with the correct values and so on.
At this point, the user will update some information on the tabs created, and need to click a Update button. At this point, I need to read all tabs, one by one, accessing all textboxes created, and send this to my database.
The only thing I got no result till now is “How to access these programmatically created Textboxes?
This is how I create the textboxes inside the TabControl
Dim TXT As New TextBox
TXT = New TextBox
TXT.Location = New System.Drawing.Point(213, 25)
TXT.Width = 303
TXT.TextAlign = HorizontalAlignment.Center
TXT.Name = "TXT_02_" & tab_counter
TXT.Text = MAT_DTCP(1) 'ABERTURA
TXT.BackColor = ColorTranslator.FromOle(RGB(128, 255, 255))
FORM_01.TBC_DTCP.SelectedTab.Controls.Add(TXT)
You could use LINQ:
Dim allTextBoxes = From tab In FORM_01.TBC_DTCP.TabPages.Cast(Of TabPage)()
From txt In tab.Controls.OfType(Of TextBox)()
Where txt.Name.StartsWith("TXT_02_")
Select txt
For Each txt As TextBox In allTextBoxes
' ... '
Next
I am trying to make the reversi game in VB.Net. I have some difficulties translating the game`s logic into vb.net
If a button is black and the button next to it is white,than the button next to the white one will be black wen pressed.
newButton.tag = colum of button + (row of button * amount of columns)
-> I made 64 buttons via a function loop and added a tag
Dim knop As Button = sender
Dim value As String = knop.Tag
If value = "...(?)" Then
knop.BackColor = Color.Black
If ....(?)
End If
End If
I already made a scheme with the label of the buttons, but I find it hard to implement the logic. Can someone help me out with thid one?
EDIT: http://i.stack.imgur.com/3gdrJ.png
If you use Dim ButtonList As List(Of List(Of Button)) and add the buttons to the form in runtime you can add each the button for each row to a list then add that list to ButtonList. Now you can access each button by the indexes in the 2 dimensional list.
Since you're changing the backcolor just use that instead of using the tag.