answersLogoWhite

0

if we want to make our code short and easy then With Block is a very nice choise in some cases. Consider the following code for loading picture in a picture box. On Error Resume Next

CommonDialog1.CancelError = True

CommonDialog1.Filter = "Pictures (*.bmp;*.jpg)|*.bmp;*.jpg"

CommonDialog1.Flags = cdlOFNFileMustExist

CommonDialog1.ShowOpen

If Err.Number = cdlCancel Then

Exit Sub

ElseIf Err.Number <> 0 Then

MsgBox "Error Loading File"

End If

Picture1.Picture = LoadPicture(CommonDialog1.FileName) You may notice that there are a lot of name I have written repeating again and again which may become boring sometime. What about some shortcut. With block is the answer. we can replace the same code as following easyl which is more convenient.

On Error Resume Next

With CommonDialog1

.CancelError = True

.Filter = "Pictures (*.bmp;*.jpg)|*.bmp;*.jpg"

.Flags = cdlOFNFileMustExist

.ShowOpen

If Err.Number = cdlCancel Then

Exit Sub

ElseIf Err.Number <> 0 Then

MsgBox "Error Loading File"

End If

Picture1.Picture = LoadPicture(.FileName)

End With With block must need to be closed with "End With".

User Avatar

Wiki User

16y ago

What else can I help you with?