Основы офисного программирования и язык VBA

       

Dim NewCtrl As Control Dim


Dim NewCtrl As Control Dim Inserted As Boolean
Private Sub UserForm_Initialize () Inserted = False Opb1. Value = True End Sub
Private Sub CommandButton1_Click () If Inserted = False Then If Opb1. Value = True Then ' выбрано поле ввода ' добавляем 2 поля ввода Set NewCtrl = Controls. Add ("Forms. TextBox. 1", "Text1") NewCtrl. Left = 96 NewCtrl. Top = 12 NewCtrl. Width = 80 NewCtrl. Height = 20 NewCtrl. Text = "Введите имя" Set NewCtrl = Controls. Add ("Forms. TextBox. 1", "Text2") Controls ("Text2"). Left = 96 Controls ("Text2"). Top = 50 Controls ("Text2"). Width = 80 Controls ("Text2"). Height = 20 Else ' добавляем список Set NewCtrl = Controls. Add ("Forms. ListBox. 1", "NewList") NewCtrl. Left = 96 NewCtrl. Top = 12 NewCtrl. Width = 80 NewCtrl. Height = 70 NewCtrl. AddItem ("Анна") NewCtrl. AddItem ("Елена") NewCtrl. AddItem ("Ирина") NewCtrl. AddItem ("Мария") End If Inserted = True Else: MsgBox ("Элемент уже добавлен!" & vbCrLf & "Второй добавить не могу!") End If
End Sub
Пример 13.1.
Закрыть окно




Option Explicit Private GlobalName As String
Private Sub CommandButton1_Click () Controls (GlobalName). Cut
End Sub
Private Sub CommandButton2_Click () Controls (GlobalName). Copy End Sub
Private Sub CommandButton3_Click () Controls (GlobalName). Paste End Sub
Private Sub TextBox1_Exit (ByVal Cancel As MSForms. ReturnBoolean) GlobalName = "TextBox1" Cancel = False End Sub
Private Sub TextBox2_Exit (ByVal Cancel As MSForms. ReturnBoolean) GlobalName = "TextBox2" Cancel = False End Sub
Private Sub TextBox3_Exit (ByVal Cancel As MSForms. ReturnBoolean) GlobalName = "TextBox3" Cancel = False End Sub
Private Sub UserForm_Initialize () TextBox1. Text = "Это мой текст" TextBox2. Text = "Это ее текст" TextBox3. Text = "Это его текст"
End Sub
Пример 13.2.
Закрыть окно




Private Sub CommandButton1_Click () If UndoAndRedo. CanUndo = True Then ' отмена возможна UndoAndRedo. UndoAction Else ' отмена невозможна Beep End If
End Sub
Private Sub CommandButton2_Click () If NewText. Text <> "" Then ' вставка новой рубрики ListBox1. AddItem (NewText. Text) Else ' текст не введен Beep End If End Sub
Private Sub CommandButton3_Click () If UndoAndRedo. CanRedo = True Then ' восстановление возможно UndoAndRedo. RedoAction Else ' восстановление невозможно Beep End If End Sub
Private Sub UserForm_Initialize () ListBox1. AddItem "черный" ListBox1. AddItem "белый"
End Sub
Пример 13.3.
Закрыть окно



Содержание раздела