VB.NET ComboBox - Need to force a redraw when a key is pressed when it is dropped down - vb.net

I am using a DrawItem and MeasureItem events to paint a combobox with a DrawMode of OwnerDrawVariable.
Basically, I'm trying to have the user highlight a selection with the mouse, and then press the space bar to toggle the Save status of a song list. Then I call the Me.Refresh() event for the form in an attempt to redraw the form and the ComboBox.
The problem that I am running into is that only the Combobox itself (not the drop-down area) that is a control on the main form is redrawing, and the text that is behind the mouse-highlighted selection of the drop-down list is not changing from Red to Black as I believe it should. If I move the mouse to another selection, then the color does in fact update.
Here is a snippet of the code.
If (e.KeyCode = Keys.Space) Then
If cmbList.SelectedItem IsNot Nothing Then
With DirectCast(cmbList.SelectedItem, SongTitle)
.bSave = Not .bSave
End With
End If
End If
e.Handled = True
Me.Refresh()
Thanks for any help you can provide.

You need to use .RefreshItem/.RefreshItems instead of .Refresh.
See this question: Dynamically changing the text of items in a Winforms ComboBox

Related

Word VBA - easily remove form frame?

I'm trying to create a user entry form which both captures the users input and displays a status update message. The slickest way I think of doing it is to have my modal form for the user entry display over a modeless form. After the user enters their info and clicks OK, the info from the modal form is copied to the modeless form, the modal form is closed and status updates get pushed to the modless form as things change during processing:
Hopefully, with a lot of messing about with positions, it will look relatively seamless. My challenge is getting rid of the frame on my modal form. I've done a lot of searching and it seems to involve completely redrawing the form from base libraries - is there seriously no easier way to do it?
I would not use a 2nd form but just place a simple Frame on top of your form. When you want to show the "modal form", just set the visibility of the frame to True, and when you want to hide it, set it to False - all controls (in your case, the input field and the OK and Cancel button) that are placed on the frame are automatically shown or hidden.
If you have controls outside the "modal form" frame that you don't want to be active at that time, set them to enabled = False. You could handle this with a simple routine within your form.
In this example I have a frame FrameModal painted on top of the form. Note that this frame could be places over other controls.
Option Explicit
Private Sub UserForm_Activate()
showHideModalFrame False
End Sub
Private Sub buttonShowModal_Click()
' Show the "modal" dialog
showHideModalFrame True
End Sub
Private Sub buttonOK_Click()
' Do your stuff here...
Me.tbUpdates = Me.tbUpdates & vbCrLf & Me.tbInput
' Hide the "modal" dialog"
showHideModalFrame False
End Sub
Private Sub showHideModalFrame(show As Boolean)
Me.FrameModal.Visible = show
Me.buttonShowModal.Enabled = Not show
End Sub
Start the form
Click the show button:

How does Tab Index work if the control's enabled property is false

If I'm viewing a form and I set the enabled property of the control with tab index = 0, does the cursor then move to the next tab index? Do I need to, and is there a way, to force the tab to set to the first control with Enabled = True?
So in order to achieve this (assuming there are no panels on your form), this is how you could iterate through the controls in tab order. The first control which you encounter and which is enabled, you set the focus on it and leave the Sub. The myFirstControl variable is initialized by you with the first control in the tab order list of the form.
Private Sub IterateControls()
Dim ctrl As Control = myFirstControl
While ctrl IsNot Nothing
If ctrl.Enabled = True Then
Me.ActiveControl = ctrl
Exit Sub
End If
ctrl = Me.GetNextControl(ctrl, True)
End While
End Sub
If you have panels also, you should build a dictionary of panels (with the panel as key, its first control as value) and take them one by one using a For loop. The For loop should be placed to include the whole method's code, but this time you initiate the ctrl variable with the first control from the panel (i.e. the value of the current dictionary entry), instead of the first control of the Form, and also you would call myPanel.GetNextControl(...) instead of Me.GetNextControl(...). The other code lines should remain the same. If this is not helpful enough, add a comment and I will edit my answer.

Using Radio Buttons VBA in PowerPoint

I'm making a game inside of PPT for work and I'm trying to get radio buttons to work. I want there to be three radio buttons and, depending on which one is clicked, an "advance slide" button will redirect the user to a different slide
Simplying it to a single radio button, it should be (psuedocode)
If radioButtonA.value = true then
GoToSlide (n)
End If
I can't even get it to work with a message box! Just to see that I am correctly pulling the boolean value from the radio button being clicked or not.
I've tried
If (ActivePresentation.Slides(1).Shapes("radioButton1").value = true) then
MsgBox("Plz work")
End if
^^ doesn't do anything
I've also tried
Dim radioBoolean as Boolean
radioBoolean = ActivePresentation.Slides(1).shapes("radioButton1").value
if radioBoolean = true
MsgBox("Plz work")
end if
Nothing happens
Use this instead:
ActivePresentation.Slides(1).Shapes("radioButton1").OLEFormat.Object.Value = True
Radio buttons are named OptionButtonX so I'm assuming that you've renamed yours to radioButtonX.

Remove previous selection highlighting in RichTextBox without scrolling

I have a form with a RichTextBox (RTB) and a listBox.
When the end user selects an item in the listbox, any matched text in the RTB is highlighted (full code removed for brevity).
re = New Regex(txtToFind)
For Each m In re.Matches(rtbMain.Text)
rtbMain.[Select](m.Index, m.Length)
rtbMain.SelectionColor = Color.White
rtbMain.SelectionBackColor = System.Drawing.SystemColors.Highlight
Next
When the user left mouse clicks in the RTB I want the previously highlighted text to be cleared. This is the standard windows behaviour - If you manually select some text in an RTB with the mouse, it is highlighted, click anywhere back in the RTB and the highlighting disappears. My programatically selected text remains selected.
I have some partially working code (below). I can clear all the highlighted text, but it is by process of selecting everything, changing the colour back and then deselecting it again. I know it is not efficient, the RTB flickers and I am sure it is not the correct way to do it. Can I emulate the standard windows behaviour?
Also using my code, it scrolls to the first line when entering the RTB a second time.
I get around this the first time by returning the top visible line index before clearing the text and then selecting that line again afterwards and using ScrollToCaret(). This only works on the first pass. Subsequent MouseDown events select the top row regardless of where the user has clicked so nothing can be manually highlighted in the RTB.
Private Sub rtbMain_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtbMain.MouseDown
Dim topIndex As Integer = rtbMain.GetCharIndexFromPosition(New System.Drawing.Point(1, 1))
Dim topLine As Integer = rtbMain.GetLineFromCharIndex(topIndex)
If e.Button = Windows.Forms.MouseButtons.Right Then
'Do nothing (Context Menu)
Else
rtbMain.SelectAll()
rtbMain.SelectionColor = Color.Black
rtbMain.SelectionBackColor = Color.White
rtbMain.DeselectAll()
rtbMain.Select(topIndex, 0)
rtbMain.ScrollToCaret()
End If
End Sub
I need my code to emulate the standard windows behaviour - clear selected text highlighting on MouseDown and leave the mouse cursor where the user has clicked.
Any help anyone can offer is gratefully appreciated.
I think you may be overthinking this one.
In the right click event, try RtbMain.SelectionLength = 0

Place a cursor (for editing) in datagridview cell text using a right click

Im trying to piece together vb.net code that will allow the user to right click in a datagridview and then place a cursor (or is it a caret?) into the text of the cell.
I can already get the row and cell using
Dim hti As DataGridView.HitTestInfo = sender.HitTest(e.X, e.Y)
and isolate the right click on a mouse down
If e.Button = Windows.Forms.MouseButtons.Right Then
I also have the position where the user right clicked using
Dim pt As Point = Me.PointToClient(Control.MousePosition)
what seams to elude me is how to put it all together to place the cursor where the user right clicked.
What I don't want is to place a cursor into a cell just anywhere, I need to place a cursor into the cells text (if any is there) where the user right clicked in the cell.
The point of the code will be to allow the user to place some text into the cell at the clicked position based on a selection from a right click menu. Appreciate any helpful suggestions.
Kind regards
Michael
Something like this possibly... should work reasonably well.
In your DataGridView CellMouseDown event handler:
If e.Button = MouseButtons.Right Then
'Get coordinates within cell
_point = New Point(e.X, e.Y)
'we are going to handle setting the edit state of the cell
_handleEdit = True
'Set current cell to the one we right clicked in (this will trigger the CellEnter event)
dataGridView1.CurrentCell = dataGridView1(e.ColumnIndex, e.RowIndex)
End If
In your DataGridView CellEnter event handler:
If _handleEdit Then
'enter edit state
dataGridView1.BeginEdit(False)
'set the seletionstart property based on position
CType(dataGridView1.EditingControl, TextBox).SelectionStart = CType(dataGridView1.EditingControl, TextBox).GetCharIndexFromPosition(_point)
'Done handling the edit state
_handleEdit = False
End If