How to remove highlight of combobox when an Item is selected in Vb? - vb.net

In load page event:
I read some data from DB and then add this data to Combo box then select an item as default and enable of combo box changes to false.
When I load this page, the item witch selected highlights with blue color.
How to remove this highlight?

You can modify its SelectionLength property, which gets or sets how many characters have been selected (highlighted).
Just set it to 0 after you have selected the default item and you should be good to go:
ComboBox1.SelectionLength = 0
EDIT:
In your case this code is executed before the Load event has finished. Due to this the form has not been rendered yet, which is why it is not working for you.
The simple fix is to add this in the form's Shown event too:
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
ComboBox1.SelectionLength = 0
End Sub
If you don't want the caret to be in the beginning you can also add this line to set it to the end of the text:
ComboBox1.SelectionStart = ComboBox1.Text.Length

The proposed solution does not work in VB.NET 2016
The easiest way to do is to pass focus to another element like a label in the SelectedIndexChanged event

Related

How to check if some text in a RichTextBox is selected

I am creating a text editor and I want to add copy, paste and cut functions in the Edit Menu. I want these MenuItems to only to be enabled when there's an active selection.
I have a function that searches and highlights but there doesn't seem to be a trigger to check for in order to do it.
The RichTextBox.SelectionChanged event (documentation) fires when the selection of text within the control has changed.
The RichTextBox.SelectionLength property (documentation) will return the number of characters selected in control.
Handle the SelectionChanged event and set the Enabled property of the menu items based on if the SelectionLength is greater than 0:
Private Sub MyRichTextBox_SelectionChanged(sender as Object, e as EventArgs) Handles MyRichTextBox.SelectionChanged
MyMenuItem1.Enabled = MyRichTextBox.SelectionLength > 0
MyMenuItem2.Enabled = MyMenuItem1.Enabled
MyMenuItem3.Enabled = MyMenuItem1.Enabled
End Sub

Forcing a combo box to update its text field

I have a typical combobox with drop down items. An item in the dropdown consists of a string with a code then a space, then the description of the code. I'm trying to get the text of the combo box to show just the code after the selection has been made, but there is a race condition in which my the text field is not being updated by the combobox until AFTER I change it. How do I force the combobox to update itself, so I can change the text afterwards.
With code_combo_box
AddHandler .SelectedIndexChanged, AddressOf update_desc
End With
Private Sub update_desc()
If code_combo_box.SelectedIndex >= 0 Then
Dim temp_string As String() = code_combo_box.SelectedItem.split(" ")
code_combo_box.Text = temp_string(0)
End If
End Sub
The combobox gets updated to the selected item after update_desc is called wiping out my change.
I figured out the answer which is to flag the event as handled when I enter the update_desc subroutine. So I needed to catch the events when entering the routine with
update_desc(sender as object, e as system.eventargs)
and then inside the routine simply use
e.handled = true
and the event would not continue to fire after leaving the routine and my changes would remain.

Show ContextMenuStripItem without clicking off the current cell

I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.
However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.
This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?
I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.
Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu?
The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.
EDIT
I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?
My most recent effort was done with the following code:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
But this wasn't triggered until the cell was no longer in edit mode.
NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER
When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
I have tried to write an event handler for the MouseUp event with this code
Private Sub grid_MouseUp(sender As Object, e as MouseEventArgs) Handles grid.KeyUp
grid.PerformAction(UltraGridAction.ExitEditMode)
grid.ContextMenuStrip.Show()
End Sub
and it works.
The ContextMenuStrip was added in code with this text (as example)
ContextMenuStrip ctx = new ContextMenuStrip()
ctx.Items.Add("Test1")
ctx.Items.Add("Test2")
ctx.Items.Add("Test3")
grid.ContextMenuStrip = ctx

VB.net modify a textfile by using listbox and textbox

I want to be able to edit/delete/insert lines of text from a textfile by using listbox and textbox.
I want to display all the contents of the textfile per line in a listbox and when I click a line of text it will then display it in the textbox giving me the option to either edit or delete that line of text.
My insertion of text would be inserted after the last line of text being displayed in the listbox. Is this possible? I just want a starting point and I will continue it from there. Thanks in advance.
Here is an answer with a listview , button 9 is to fill Listview, listview click sends the text to textbox and button 10 saves it back to the listview
This is probably the simplest way to do what you want to achieve.
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
ListView1.HeaderStyle = ColumnHeaderStyle.None
ListView1.HideSelection = False
For i As Integer = 0 To 50
ListView1.Items.Add("Line number" & i.ToString)
Next
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.Click
TextBox8.Text = ListView1.SelectedItems(0).Text
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
ListView1.SelectedItems(0).Text = TextBox8.Text
End Sub
Probably a good starting point for you anyway and expand this code from here on.
Yes. It is possible. I would suggest to use already existing text editors rather than re-inventing the wheel. If you still want to go ahead and create a new one from start, then you can try the following.
Create a window form application in vb.net with ListBox control for showing the lines, a textbox control for entering the file name, a button to browse to the given file, a button on clicking on which the file content should get loaded. Refer file objects for this.
Utilise ContextMenu class in vb.net to allow right click to read selected listbox line and accordingly perform add/delete/edit operation by modifying the selected listitem value.

Setting Focus on a Tab

I have a tab in a windows form called Wafer Map that has three sub-tabs. The First sub-tab is the called Map and has a Load and Skip button. I am trying to set the focus on the Wafer sub-tab on the Load button click. This is the following code I have tried to use.
Private Sub Load_Wafer_Layout_Map_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Load_Wafer_Layout_Map.Click
Wafer_Info.Enabled = True
Wafer_Info.Show()
End Sub
The Wafer_Info.Enabled = True is used to enabled all of the controls on the Wafer tab and works properly when the button is clicked. I have tried using .Focus() and .Show() to bring focus to the next tab but I am not have any luck getting to switch. Anyone have any suggestions?
Just set it:
tabControl.SelectedTab = yourTab
On the Tab Controls Tab Pages, just ensure you name the tab you are attempting to reference. Additionally, see MSDN TabControl.SelectedTab
The code that worked for me is Tab_WaferMap.SelectTab(1). Tab_WaferMap is my main tab and the 1 is the index of the sub tab I wanted to show
I came across this thread as i was looking for a solution to my own focus issue. I have a TabControl with many TabPages. Each TabPage is set to auto scroll due to overflowing content. The problem I ran into was the mouse scroll wheel would not function if the TabPage did not have focus. Since there is not an event for each tab click it made setting focus to each TabPage a challenge. It was not hard, but a challenge none the less. So, here is my code (assuming auto scroll true).
On form load sets focus to main TabPage:
Private Sub frmParent_Load(sender As Object, e As System.EventArgs) Handles Me.Load
TabControl1.TabPages(0).Focus()
End Sub
Sets focus to current TabPage by getting the index then setting focus.
This is triggered by TabControl1.SelectedIndexChange event.
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim intTabIndex As Integer = TabControl1.SelectedIndex
TabControl1.TabPages(intTabIndex).Focus()
End Sub
I hope someone find this useful. It was very useful for me.
Joshua
You can also set the Selected Index of the tab (and sub-tab) using a (zero based) numeric value:
TabParent.SelectedIndex = 3
TabSub.SelectedIndex=2