How to make a Search Bar that looks for buttons - vb.net

I want to make a Flow Layout Panel and fill it with a bunch of buttons. I want to have a Text box on the top of the form to search for a specific button. No clue on how. When the user searches, all other buttons will not be visible, only the button that its .text property matches will be visible.

Assuming search box is looking for the button's .Text property, that the textbox is named txtSearch, and the panel is named pnlButtons:
Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
pnlButtons.SuspendLayout()
For Each c As Control In pnlButtons.Controls
c.Visible = False
Next
Dim matchingButtons = pnlButtons.Controls.OfType(Of Button).Where(Function(b) b.Text.StartsWith(txtSearch.Text))
For Each b As Button In matchingButtons
b.Visible = True
Next
pnlButtons.ResumeLayout()
End Sub

Related

How do you set a dynamically created panel to false in VB.net?

I have on my screen design Panel1(left half), and panel2 through 10(right half), the panels on the right half are named based on data from a database.
I need to be able to click on a button in panel1 and when I do so, I need to set visibility to false for the current panel on the right half and set visibility to true that is referenced from the button click. I know that I can do the following but I think this is just way too much overhead and there has to be a better solution than this:
For Each control In Me.Controls.OfType(Of Panel)
If control.visible = true Then
control.visible = false
exit
Next
Panel that the visibility that needs to be set to false was dynamically created so it can not just be accessed by just name, otherwise that would solve my issue easily.
I seen this bit of code elsewhere and it would make my life easier, however not sure how to implement it dynamically when the panels are created, since the name of the panels are unknown at creation.
This bit of code to be able to reference the panels directly.
Dim machinePanel As Panel = DirectCast(Me.Controls.Item("pnl" & strMTB & strMachineID), Panel)
I'm not sure what you mean but "is referenced from the button click", so I'll assume that the text of the button refers to the panel name.
Create a method that handles the visibility
Private Sub SetPanelVisibility(button As Button)
'panel that the button is in
Dim leftPanel = CType(button.Parent, Panel)
'get right panels and ignore left
Dim rightpanels = Me.Controls.OfType(Of Panel).Where(Function(x) x.Name <> leftPanel.Name)
'set visibility of each panel.
For Each panel In rightpanels
panel.Visible = panel.Name = button.Text
Next
End Sub
To call the method just pass the button in on the click event. e.g.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SetPanelVisibility(sender)
End Sub

Setting tab stop between two panels

I have two panels, each has several textboxes. What I want is something that seems very simple: user enter a textbox in this panel, then press Tab to jump to its 'linked' textbox in the other panel.
But, vb refuses to jump to other panel unless it finishes through all of the textboxes inside one panel, no matter what TapStop it is.
I tried to catch the tab key from a textbox, and send focus to the linked one without success: pressing Tab doesn't even fire the KeyDown nor KeyPress event.
I tried to set TabStop to the panels first, but that also fail.
So, the problem remain.. how to set tabstop.. or any similar means, to the textboxes between two panel so that when user press Tab in one panel, it will switch to the other one ?
I need the Tab key, not anyother key.
You'll have to set the TabStop property of each control on the form to False and then handle the tabbing yourself, which you can do like this:
Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox6.PreviewKeyDown,
TextBox5.PreviewKeyDown,
TextBox4.PreviewKeyDown,
TextBox3.PreviewKeyDown,
TextBox2.PreviewKeyDown,
TextBox1.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
Dim controls As Control() = {TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2}
Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)
Dim nextControl = controls(currentControlIndex + 1)
nextControl.Select()
End If
End Sub
Every control you want to be able to Tab from must be in the Handles clause and every control that you want to be able to Tab to or from must be in the array and in the order you want to Tab to them in. You should also repeat the first control at the end of the array again, in order to wrap back to the beginning from the end.
Also note that no control will be selected by default if none of them a Tab stops, in which case you must manually Select the control you want to have focus by default in the form's Shown event handler.
EDIT: Here is a more complete example:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'Manually focus the first control after the form is displayed.
Button1.Select()
End Sub
'Include all the controls that you want to behave as Tab stops in the Handles clause.
'The order is unimportant but ordering them you will Tab to them is not a bad idea.
Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown,
TextBox1.PreviewKeyDown,
TextBox4.PreviewKeyDown,
TextBox2.PreviewKeyDown,
TextBox5.PreviewKeyDown,
TextBox3.PreviewKeyDown,
TextBox6.PreviewKeyDown,
Button2.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
'This array must contain all controls to behave as Tab stops in order and the first must be repeated at the end.
Dim controls As Control() = {Button1, TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2, Button1}
'Find the currently active control in the array.
Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)
'Get the next control in the manual tab order.
Dim nextControl = controls(currentControlIndex + 1)
'Focus that next control.
nextControl.Select()
End If
End Sub
That code works for the following form, where TextBox1, TextBox2 and TextBox3 are in Panel1 and TextBox4, TextBox5 and TextBox6 are in Panel2:

Remove the form close button

I need a form without buttons, but with Text and icon;
So far, I managed to get it except the close "X" button;
Is there a way to remove it?
My code is below,
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Text = "Form with Icon and Text, but NO Buttons"
Me.Icon = New Icon("c:\test.ico")
Me.MaximizeBox = False
Me.MinimizeBox = False
End Sub
You can hide it using Me.ControlBox=false; in code or in properties change the
ControlBox option to false
You can workaround this by one of the following (two involves custom drawing on the title bar):
Disable the close button and custom draw on the title bar to hide it
Remove the close button and draw the icon on the title bar
check this link for custom title bar drawing : http://msdn.microsoft.com/en-us/library/windows/desktop/bb688195(v=vs.85).aspx
Best of luck

Open Tabs Control

I'm using MDI container to run my bussiness application I created for my client.
Since using MDI means that when I open several forms they will still run in background all the time untill I close them manualy.
What I need is to make User Control or anything else that could preview all opened forms in Tab Form so my client can easily close all or some of opened forms without closing a form he is curently viewing.
For now I have used this code, so for now only first clicked item from menu appears as button, but not others clicked menu items.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim Button As New Button
Me.Panel5.Controls.Add(Button)
Button.Text = e.ClickedItem.Name
Button.Width = 50
Button.Height = 25
End Sub
Now I need to write a code to add more buttons bellow, also should add a code for adding buttons only when I click on SubMenu item (The one when is clicked new Form appear).
And also, I should now add a little Close button into previewed User-Button-Control.
From your comments, I understand that your ideas regarding adding buttons at runtime are not too clear and thus I am including a small code which hopefully will help you on this front. Start a new project and put a Panel (Panel5) and a Button (AddButtons) on it, and write this code:
Dim lastButtonIndex, lastLeft, lastTop As Integer
Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
Dim curButton As Button = DirectCast(sender, Button)
If (curButton.Name = "Button1") Then
'do Button1 stuff
End If
'etc.
End Sub
Private Sub addNewButton()
lastButtonIndex = lastButtonIndex + 1
lastLeft = lastLeft + 5
lastTop = lastTop + 5
Dim Button As New Button
With Button
.Name = "Button" + lastButtonIndex.ToString()
.Text = "Button" + lastButtonIndex.ToString()
.Width = 50
.Height = 25
.Left = lastLeft
.Top = lastTop
AddHandler .Click, AddressOf Button_Click
End With
Me.Panel5.Controls.Add(Button)
End Sub
Private Sub ButtonAddButtons_Click(sender As System.Object, e As System.EventArgs) Handles AddButtons.Click
addNewButton()
End Sub
This code will add a new button to the panel every time you click on AddButtons. All the buttons will have an associated Click Event (the same one for all of them): Button_Click. The way to know which button is the current one inside this method is via sender, as shown in the code (you can put as many conditions as buttons. The names are given sequentially starting from 1; but you can take any other property as reference, curButton is the given Button Control).
Bear in mind that one of the problems you have to take care of is the location of the buttons. The code above has a very simplistic X/Y values (Left/Top properties) auto-increase which, logically, will not deliver what you want.

Clicking on a tab of tab control, I want to do an action in VB.Net

When I click on a tab, so it shows the content of that tab page but I want to do an action when the tab is selected, for example when I click on "finance tab" there would be a combobox in the Finance Tab, that combobox should fill with the data from sql server, is it possible ?
You can use the TabControl.SelectedIndexChanged Event
Private Sub TabControl1_SelectedIndexChanged(sender as Object, e as EventArgs) _
Handles TabControl1.SelectedIndexChanged
'suppose your *finance tab* instance is TabPageFinance
If TabControl1.selectedtab is tabPageFinance
'do stuff
End if
'or suppose *finance tab* index is 0
If TabControl1.selectedindex = 0
'do stuff
End if
End Sub
Yes it is possible !!
You can use panel with combobox in it
About how to fill combobox fill with your data you can see this link
http://vb.net-informations.com/dataset/bind-combobox.htm