Textalignment in a Textbox in VBA - strange behaviour - vba

I'm trying to align text in a textbox to the right side. Either through code TextBox.TextAlign = fmTextAlignRight or setting the property "directly".
Unfortunately, it becomes left-aligned as soon as the text (string) gets too big to be displayed fully in the textbox. Like this example which has all letters from a to z (abcdefghijklmnropqrstuvwxyz) and should be right-aligned, but it's not:
It should look like this:
Any ideas on how to change this behavior / how to force the alignment? Thanks!

Probably this TextBox has the property Enabled set to false. You need to turn it on, and then the TextAlign property will work as you wish. Also, you can use this function:
Public Function SetTextBox(itm1 As MSForms.textbox, ByVal txtAlign1 As MSForms.fmTextAlign)
Dim notEnabled1 As Boolean
If Not itm1.Enabled Then itm1.Enabled = True: notEnabled1 = True
itm1.SetFocus
itm1.TextAlign = txtAlign1
itm1.SelStart = Len(itm1.Text)
If notEnabled1 Then itm1.Enabled = False
End Function

Related

How can I get the checkbox.checked event to fire when I pass it as a parameter in vb.net?

I have programmed a long time but I’m relatively new to vb.net. And I’ve avoided subroutines and functions in which I passed parameters because I always get stuck. I’m trying to write a subroutine to pass information that will fill a TextBox or a checkbox with either the value from a table or clear the field or set to false. The first code below is an example of what I’ve been doing and this works. I trying to write a subroutine to pass 1.the name of the textbox or checkbox control on my form,2.the data row value, and 3.the column name in the table. The problem is when I passed a checkbox I can’t get the checked event to show on my control(CoreCol) that I passed. It knows it’s a checkbox and it will set the text of the checkbox too true or false but it won’t change the box checked.
This is an example of the old way that works. For a TextBox and a checkbox
' A Machine
If Not IsDBNull(r("A Machine")) Or Not IsNothing(r("A Machine")) Then
TbXMachA.Text = r("A Machine")
Else
TbXMachA.Text = ""
End If
If Not IsDBNull(r("A CO2 Box?")) Or Not IsNothing(r("A CO2 Box?")) Then
CkbxCO2BoxA.Checked = r("A CO2 Box?")
Else
CkbxCO2BoxA.Checked = False
End If
This works
LoadData2TextBox(Me. TbXMachA, r, "A Machine ")
This doesn’t
LoadData2TextBox(Me.CkbxCO2BoxA, r, "A CO2 Box?")
this is the sub routine I'm writing
Private Sub LoadData2TextBox(ByRef CoreCol As Control, CoreRow As DataRow, BoxStage As String)
If Not IsDBNull(CoreRow(BoxStage)) Then
If TypeOf CoreCol Is TextBox Then
CoreCol.Text = CoreRow(BoxStage)
End If
If TypeOf CoreCol Is CheckBox Then
CoreCol.??? = CoreRow(BoxStage)
End If
Else
CoreCol.Text = ""
End If
You know that CoreCol is a CheckBox so you can cast it as one then use it as a CheckBox.
If TypeOf CoreCol Is CheckBox Then
Dim myCheckBox = DirectCast(CoreCol, CheckBox)
myCheckBox.Checked = DirectCast(CoreRow(BoxStage), Boolean)
End If
Another cast in getting the boolean value out of CoreRow(BoxStage). The above code assumes this will work, but I am not sure what is in CoreRow(BoxStage). You may need to add some logic based on the value depending on what it is. For example:
myCheckBox.Checked = CoreRow(BoxStage) = "somevalue"

How do i hide a control in a report based on a field value

I have a checkbox control that i need to hide if a Boolean value is false. I have tried using the Detail_Format event to no avail. i put the field value into another checkbox, then tried setting the visible property from the value in the checkbox, no dice. In the image below, the rightmost checkbox is showing the value of the field that determines if the checkbox to the left of it should be shown. I have the following code
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.chkRequireverify = True Then
Me.chkVerified.Visible = True
Else
Me.chkVerified.Visible = False
End If
'Me.chkVerified.Visible = Me.chkRequireverify
End Sub
when i tried using the Detail_Paint event, it errored out telling me that i cant change the visible property in this event.
Your code looks to be correct and could be shortened to simply:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.chkVerified.Visible = Me.chkRequireverify
End Sub
However, I believe the event handler for the OnFormat event will only be evaluated when viewing the report in Print Preview view, rather than in Report view.
Whilst the OnPaint event fires in Report view, you cannot modify certain properties after the report has been formatted, the Visible property being one such property, along with the Height & Width properties (and so you also cannot shrink the checkboxes to zero size).
It's been a while since I've used Access and VBA, but I believe what you're missing is .Value after the checkbox name in the if clause.
Try this : If Me.chkRequireverify.Value = True

Underline property of textbox not working like the others

I simply want to underline some text in a worksheet textbox using VBA, from a specific character to another. It should be extremely simple, and I can do it without problem with Bold and Italic.
I have the following sub
Sub ew()
Dim txt1 As Shape
Set txt1 = Sheet1.Shapes("txt_1")
txt1.TextFrame.Characters.Text = "Bold and Underline this"
txt1.TextFrame.Characters.Font.Bold = True
txt1.TextFrame.Characters.Font.Italic = True
txt1.TextFrame.Characters.Font.Underline = True
End Sub
The code fails on the last line, which is extremely strange because it worked for the 2 previous lines. the error (1004) says something like "Impossible to define the Underline function of the Font property".
To recreate the problem, take my sub to a new Excel document and create a textbox named "txt_1", that's all you need to run it.
If anyone has any idea why it fails, please help!
You need to define the Underline style. Taking your last line
txt1.TextFrame.Characters.Font.Underline = xlUnderlineStyleSingle
Use TextFrame2 for underline
txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine

show/hide textbox by change value of combobox

i'm having problem regarding combobox in vb.net,
im trying to show & hide specific text boxes, on the base of an item when it is selected from combobox,
here is the code i'm writing, whats mistake in it?
Dim PatSearchID, PatSearchName, PatSearchCat As String
PatSearchID = txtSearchID.Text
PatSearchCat = cmbSearchPat.SelectedItem.ToString()
If PatSearchCat = "RegNo" Then
txtSearchID.Show()
txtSearchName.Hide()
End if
kindly correct what the problem is?
txtSearchName.Visible = False
30chars
instead of this (txtSearchID.Show() txtSearchName.Hide()) try to use the visible property of textbox

How to set tooltips to combobox items # VB.NET?

I have a combobox with a given width.
It may occur that one row of the data is partially hidden (the combobox might be too narrow). I'd like to show the whole line by using a tooltip or right-click context menu.
Currently I can't find how to 'catch' the row that I'm currently holding on or passing over by mouse. Please tell me.
Thanks in advance!
Have you tried to increase the DropDownWidth property so that everything is visible?
Edit: To find the ideal width based on the items in the list:
var maxLength = 0;
// using the ComboBox to get a Graphics object:
using (var g = Graphics.FromHwnd(comboBox2.Handle)) {
foreach (var item in comboBox2.Items.Cast<string>()) {
var itemLength = g.MeasureString(item, comboBox2.Font);
maxLength = Math.Max((int) itemLength.Width, maxLength);
}
}
if (comboBox2.Items.Count > comboBox2.MaxDropDownItems) {
// correction when ScrollBar is displayed
maxLength += 15;
}
comboBox2.DropDownWidth = maxLength;
I put this code in the DropDown event of the ComboBox for testing. Maybe you can find a better place for it, like after populating the ComboBox...
Going in the same direction that Julien went, here is a generic extension method that will resize the drop down area regardless of how the combobox is filled (manually with strings or via data binding).
<Extension()> _
Public Sub AutosizeDropDownWidth(ByVal combobox As ComboBox)
Dim longestItem As Integer = 0
Using g = Graphics.FromHwnd(combobox.Handle)
Dim itemsAsText = (From item In combobox.Items _
Select combobox.GetItemText(item))
longestItem = CInt(itemsAsText.Max(Function(text) g.MeasureString(text, combobox.Font).Width))
End Using
' Account for scrollbar
If (combobox.Items.Count > combobox.MaxDropDownItems) Then longestItem += 15
' Resize as needed (but never smaller than the default width)
combobox.DropDownWidth = Math.Max(longestItem, combobox.Width)
End Sub
To use it then you can simply do the following...
Private Sub MyCombobox_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyCombobox.DropDown
MyCombobox.AutosizeDropDownWidth()
End Sub
Note, I didn't test corner cases like an empty combobox in this code example.
Your're right, there really isn't a "Item.OnMouseOver", but I suppose you could (off the top of my head, so I've likely forgotten something)...
inherit from ComboBox,
override OnDrawItem (you may need to turn change .DrawMode to "Owner Drawn").
you will know which item is hovered in the OnDrawItem event/override from the EventArgs.
Set the tooltip on the control at that point.
optionally set a timer to manually show the tooltip if the above doesn't work automatically.