I cannot figure out why the code below will not work. The first list box displays all of the controls except the textboxes. Of course, the MsgBox is doing nothing - since it is only looking for textboxes. Why is it not recognizing the textboxes? By the way, I have changed the names on some of my textboxes because I have so many. Can it not recognize what type of control it is if I have changed the name?
Dim ctl As Control
For Each ctl In Me.Controls
ListBox1.Items.Add(ctl.Name)
If TypeOf ctl Is TextBox Then
MsgBox(ctl.Name)
'validate that it is numeric
If ctl.Text = "" Then
'if not show error and exit sub
MessageBox.Show("Please fill all blanks.")
Exit Sub
End If
End If
Next
I appreciate the help.
Related
I'm use Visual Basic 6 to create a table with many Textbox which named txtNo1, txtNo2, txtNo3,...
I want to use the "For...Next..." loop to assign a content to these Textbox.
How can I call all these Textbox in the simplest way?
For i = 1 to 100
txtNo (......) .txt = "ABC"
Next i
Instead of using unique textboxes, each with a unique name, you should use a (textbox) control array:
Place the 1st textbox on the form, name it 'txtNo'
Copy it and paste it onto the form
VB will ask you "There's already a control named 'txtNo'. Would you like to create a control array?". Answer "Yes"
Paste as the textbox as many times as you need it
Then your code looks like
' Control arrays typically start at index 0
For i = 0 to 100
txtNo(i) .txt = "ABC"
Next i
Jim Mack's solution works as well, code for it:
' Assuming your form is named 'Form1'
For each ctrl in Form1.Controls
If TypeOf ctrl Is Textbox
For i = 1 To 100
If ctrl.Name = "txtNo" & CStr(i) Then
ctrl.Text = "ABC"
End If
End If
End If
It's a bit more complex, but therefore more flexible as works with multiple control types (in one loop).
If you need an easiest way to create your textboxes as a table, you can Load the controls at runtime. You have to add only the first TextBox control to your form, set the name to "txtNo", and Index to 0 in the Properties window.
In your code, call Load() to create additional controls, and you can set the Top/Left and other properties
For i = 1 To 100
Load txtNo(i)
txtNo(i).Top = txtNo(i - 1).Top + txtNo(i - 1).Height + 150
txtNo(i).Left = txtNo(i - 1).Left
txtNo(i).Text = "Textbox " & i
txtNo(i).Visible = True
Next i
If you need again to change any control property, from your list of controls, you can iterate only over your control list, instead of all controls of your Form
For i = txtNo.LBound() To txtNo.UBound()
Form1.Controls("txtNo")(i).Text = "New text " & i
Next i
I want to print Notes-documents directly to an pdf-printer. The documents are selected in a view. I do not want to open the printer dialog form.
Using the "NotesUIView.Print"- method works in principle, however, the generated pdf-documents sometimes look not exactly like the Notes-documents (especially regarding tables).
Therefore I tried to use the "NotesUIDocument.Print" - method:
Option Public
Option Explicit
Const pdfAppName = "PDF-XChange Standard"
Dim dc As NotesDocumentCollection
Dim curDoc As NotesDocument
Dim uidoc As NotesUIDocument
Dim workspace As New NotesUIWorkspace
...
Set dc = curDB.UnprocessedDocuments
...
Set curdoc = dc.GetFirstDocument
Call workspace.EditDocument(False,curDoc)
Set uidoc = workspace.Currentdocument
Call uidoc.Print(1,0,0,False,pdfAppName)
...
Dispite the first parameter in "uidoc.print" is set to "1" the printer dialog form opens. In the printer dialog form the printer "PDF-XChange Standard" is selected correctly. Selecting the "OK"-Button prints the document correctly.
Many thanks in advance for hints.
I had a form which originally used Listview control but I need to change it into Listbox control. Now here's the original code which use for listview. I'm trying to change it for populate listbox using adodb recordset but keep getting errors. lv1 is the listbox1; lv2 is the listbox2. Listbox doesn't have listitem.
Private Sub PopulateListView()
Dim cnPop As ADODB.Connection
Dim rsPop As ADODB.Recordset
Dim lst As ListItem
Dim lngRecs As Long
On Error GoTo ErrPopulate
Set cnPop = New ADODB.Connection
cnPop.CursorLocation = adUseClient
cnPop.Open gcnORA
Set rsPop = New ADODB.Recordset
rsPop.Open sSQL, cnPop, adOpenDynamic, adLockReadOnly
lv1.ListItems.Clear
Do While rsPop.EOF = False
Set lst = lv1.ListItems.Add(, , rsPop!Customer_Number)
lst.SubItems(1) = rsPop!Customer_Name
rsPop.MoveNext
Loop
If rsPop.RecordCount > 0 Then
SelectButtons True
End If
With lblCount
.Caption = Format((rsPop.RecordCount), "#,##0") & IIf((rsPop.RecordCount) <= 1, " Customer", " Customers") & " found"
.Refresh
End With
rsPop.Close
cnPop.Close
Set rsPop = Nothing
Set cnPop = Nothing
Exit Sub
ErrPopulate:
MsgBox Err.Description, vbCritical, "Populate Error"
End Sub
A listview can have multiple columns, you apparently had at least two columns in your previous listview: Customer Number and Customer Name.
A listbox only has one column. You'll need to decide how to display/add your two columns of data to the listbox. Don't be confused by the columns property of the listbox, when set to multiple columns, the control still only has a single column of data - it simply snakes and scrolls horizontally instead of scrolling vertically.
Adding an item to the listbox:
lv1.Additem rsPop!Customer_Name
Listbox documentation: VB6 Listbox
I have a Windows Form Multiline Textbox.
I want to use Shift+Enter Instead of using Enter key to make a new line in textbox, and the traditional Enter key will be used to focus on the next control.
I mean Shift+Enter will work exactly like Enter key on normal multiline textbox (Regard to textbox maxlength --> So you cant enter newline, or current selected text will be delete when you insert newline,...)
I've tried to override OnKeyDown, to insert newline on Shift+Enter, but it doesn't work as I expected.
you'll need to override OnKeyDown and check for enter key using the KeyDownEvent's KeyCode property.
If enter was pressed and the keydownevent's modifiers property does not equal Keys.Shift you'll need to suppress the key press. Here's the documentation on how to do that:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress(v=vs.110).aspx
If shift+enter was pressed you'll need to insert Environment.NewLine at the cursor position, then move the cursor after the inserted Environment.NewLine. Here's how to do that:
How to paste text in textbox current cursor?
Here is my implementation in VB .NET.
Regard to Maxlength of the textbox and current selected text
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
If (Me.Multiline AndAlso e.KeyData = (Keys.Shift Or Keys.Enter)) Then
Dim intRemainCharCount As Integer = Me.MaxLength - Me.Text.Length + Me.SelectedText.Length
'' Check if have enough space to place NewLine
If (Environment.NewLine.Length > intRemainCharCount) Then
MyBase.OnKeyDown(e)
Return
End If
Dim intPos = Me.SelectionStart
Me.Paste(Environment.NewLine)
'' Reset selection start (Reset cusor position)
Me.SelectionStart = intPos + Environment.NewLine.Length
e.Handled = True
e.SuppressKeyPress = True
Return
End If
MyBase.OnKeyDown(e)
End Sub
Change the property for Accept Return for the Multiline doesn't even require coding anything, unless you have an event listening to the KeyDown, in which case you can put a condition to check if the Textbox is focussed.
How do I use an if statement to get the status of a button that was clicked in a `MessageDlg'?
Heres my code:
if MessageDlg('Message',mtError,[mbYesNoCancel],0) = No
then ShowMessage('Message2');
I saw my IT teacher write something like this a while ago, but I don't remember the syntax.
The documentation says:
MessageDlg returns the value of the button the user selected. The
following table lists the TMsgDlgBtn values for each type of button
that can appear in the message box, and the corresponding value that
is returned if the user selects that button:
TMsgDlgBtn Value Corresponding return value
mbOK mrOk
mbCancel mrCancel
mbYes mrYes
mbNo mrNo
mbAbort mrAbort
mbRetry mrRetry
mbIgnore mrIgnore
mbAll mrAll
mbNoToAll mrNoToAll
mbYesToAll mrYesToAll
mbClose mrClose
So you need to test for mrNo.
I do recommend that you learn where to find documentation to make your life easier.