Advertisement
The_Newt

[Guide & Examples] BASIC for Visual Basic

Oct 6th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  1. -----REMOVE BELOW THIS LINE-----
  2. if you got this from pastebin, you might wonder why it's not highlighted in a programming language syntax.
  3. that's because i didn't find BASIC there, there was just random shit like VB.NET, Basic4GL and VisualBasic which didn't work on this guide. It will work when you open this up in Notepad++, though.
  4. -----REMOVE ABOVE THIS LINE-----
  5.  
  6. REM coded by newt
  7. REM not compatible with visual basic yet; for educational purposes only
  8. REM all codes below (except REM codes) are original and thought of in the brightest way possible
  9. REM they are all examples, so feel free to modify them in any manner if possible
  10.  
  11.  
  12. REM to display messages in seperate boxes:
  13. Private Sub Command1_click()
  14. MsgBox "My name is Newt"
  15. End Sub
  16.  
  17. REM to close the program:
  18. Private Sub Command1_click()
  19. End
  20. End Sub
  21.  
  22. REM to initialize a single variable when it is declared:
  23. Dim num As Integer = 12
  24. Dim str As String = "Newt"
  25.  
  26. REM to declare multiple variables. each variable may be initialized in a seperate declaration line:
  27. Dim str1 As String = "Newt", x = 5 As Integer, y As Integer = 10
  28.  
  29. REM to initialize the variable after the declaration has been done:
  30. Dim str1
  31. Str1 = "Newt"
  32.  
  33. REM to change the value of the variable anytime during the program after initialization has already been done:
  34. Dim x As Integer = 10
  35. x = 20
  36.  
  37. REM here, the variable x will have the value 4:
  38. Dim x As Integer
  39. x = 2 ^ 2 * 2 / 2
  40.  
  41. REM here, the variable y will have the value 3:
  42. Dim y As Integer
  43. y = 9\2 + 1 - 2
  44.  
  45. REM to display the current date the user's computer is displaying:
  46. Private Sub Command1_click()
  47. Print "Today is " & Date
  48. Print "The time is " & Time()
  49. End Sub
  50.  
  51. REM to use string functions:
  52. Private Sub Command1_click()
  53. Dim nm As String
  54. nm = "My friend is Newt"
  55. Print "The total number of characters in his name are " & Len(nm)
  56. Print "The two characters from the middle of his name are " & Mid(nm, 6, 3)
  57. End Sub
  58.  
  59. REM to add inputs in a text box:
  60. Private Sub Command1_click()
  61. Text3.Text = Val(Text1.Text) + Val(Text2.Text)
  62. End Sub
  63.  
  64. REM to display only the OK button in a text box (message box):
  65. Private Sub Command1_click()
  66. MsgBox "Hello people," & " my name is Newt."
  67. End Sub
  68.  
  69. REM to display only the OK and Cancel button in a text box:
  70. Private Sub Command1_click()
  71. x = MsgBox("Hello people", l, "WELCOME MESSAGE")
  72. End Sub
  73.  
  74. REM to use the InputBox() function in a text box:
  75. Private Sub Command1_click()
  76. Dim n As Integer
  77. n = InputBox("Enter a number", "Accept number", 10)
  78. Print n
  79. End Sub
  80.  
  81. REM code to calculate the area of a circle in a message box:
  82. Private Sub Command1_click()
  83. Dim AREA As Single
  84. AREA = 3.14 * Val(Text1.Text) * Val(Text1.Text)
  85. MsgBox "The area of the circle is " & AREA
  86. End Sub
  87.  
  88. REM here we have a couple of codes which all represent a function or input used in list codes in basic:
  89. Private Sub Shift_click()
  90. List2.AddItem List1.Text
  91. List1.RemoveItem List1.ListIndex
  92. End Sub
  93.  
  94. Private Sub Add_click()
  95. Dim CUISINE As String
  96. CUISINE = InputBox("Enter the name of the cuisine in List1")
  97. List1.AddItem CUISINE
  98. End Sub
  99.  
  100. Private Sub Remove_click()
  101. List1.RemoveItem List1.ListIndex
  102. End Sub
  103.  
  104. Private Sub Clear_click()
  105. List1.Clear
  106. End Sub
  107.  
  108. REM codes to add two strings into a list:
  109. Private Sub Add_click()
  110. Dim fname, lname As String
  111. fname = InputBox("Enter the first name")
  112. lname = InputBox("Enter the last name")
  113. Combo1.AddItem fname
  114. Combo2.AddItem lname
  115. End Sub
  116.  
  117. Private Sub Remove_click()
  118. Combo1.RemoveItem Combo1.ListIndex
  119. Combo2.RemoveItem Combo2.ListIndex
  120. End Sub
  121.  
  122. Private Sub Concatenate_click()
  123. Text1.Text = Combo1.Text + " " + Combo2.Text
  124. End Sub
  125.  
  126. Private Sub Clear_click()
  127. Combo1.Clear
  128. Combo2.Clear
  129. End Sub
  130.  
  131. REM code without the END IF statement:
  132. Private Sub Calculate_click()
  133. If TxtAmount.Text > 10000 Then
  134. TxtDiscount.Text = TxtAmount.Text * 0.1
  135. End If
  136. End Sub
  137.  
  138. REM using the END IF statement:
  139. Private Sub Calculate_click()
  140. If TxtAmount.Text > 10000 Then
  141. TxtDiscount.Text = TxtAmount.Text * 0.1
  142. Price = TxtAmount.Text - Discount
  143. MsgBox "The price is " & Price
  144. End If
  145. End Sub
  146.  
  147. REM using IF, THEN, ELSE:
  148. Private Sub Command1_click()
  149. If TxtAmount.Text > 10000 Then
  150. TxtDiscount.Text = TxtAmount.Text * 0.1
  151. Else
  152. TxtDiscount.Text = TxtAmount.Text * 0.05
  153. End If
  154. End Sub
  155.  
  156. REM using IF, THEN, ELSEIF
  157. Private Sub Command1_click()
  158. If TxtAmount.Text > 20000 Then
  159. TxtDiscount.Text = TxtAmount.Text * 0.15
  160. ElseIf TxtAmount.Text > 10000 Then
  161. TxtAmount.Text = TxtAmount.Text * 0.1
  162. ElseIf TxtAmount.Text > 5000 Then
  163. TxtAmount.Text = TxtAmount.Text * 0.05
  164. Else
  165. TxtAmount.Text = TxtAmount.Text * 0.02
  166. End If
  167. End Sub
  168.  
  169. REM code for comparing two given numbers:
  170. Private Sub Calculate_click()
  171. Dim N1, N2 As Integer
  172. N1 = Text1.Text
  173. N2 = Text2.Text
  174. If N1 < N2 Then
  175. MsgBox N1 & " is smaller"
  176. ElseIf N2 < N1 Then
  177. MsgBox N2 & " is smaller"
  178. Else
  179. MsgBox " Both numbers are the same."
  180. End If
  181. End Sub
  182.  
  183. REM code for formatting a given text:
  184. Private Sub Check1_click()
  185. If Check1.Value = 1 Then
  186. Text1.FontBold = True
  187. Else
  188. Text1.FontBold = False
  189. End If
  190. End Sub
  191.  
  192. Private Sub Check3_click()
  193. If Check3.Value = 1 Then
  194. Text1.FontItalic = True
  195. Else
  196. Text1.FontItalic = False
  197. End If
  198. End Sub
  199.  
  200. Private Sub Check2_click()
  201. If Check2.Value = 1 Then
  202. Text1.FontUnderline = True
  203. Else
  204. Text1.FontUnderline = False
  205. End If
  206. End Sub
  207.  
  208. Private Sub Check4_click()
  209. If Check4.Value = 1 Then
  210. Text1.FontStrikethru = True
  211. Else
  212. Text1.FontStrikethru = False
  213. End If
  214. End Sub
  215.  
  216. Private Sub check3_Change()
  217. If Check4.Value = 1 Then
  218. Text1.FontStrikethru = True
  219. Else
  220. Text1.FontStrikethru = False
  221. End If
  222. End Sub
  223.  
  224. Private Sub check2 Click()
  225. If Check2.Value = 1 Then
  226. Text1.FontUnderline = True
  227. Else
  228. Text1.FontUnderline = False
  229. End If
  230. End Sub
  231.  
  232. Private Sub check4 Change()
  233. If Check4.Value = 1 Then
  234. Text1.FontStrikethru = True
  235. Else
  236. Text1.FontStrikethru = False
  237. End If
  238. End Sub
  239.  
  240. Private Sub Text2_change()
  241. End
  242. End Sub
  243.  
  244. REM code for calculating and displaying a message for the addition of two numbers:
  245. Private Sub Command1_click()
  246. firstnum = Val(Text1.Text)
  247. secondnum = Val(Text2.Text)
  248. total = Val(Text3.Text)
  249. If total - firstnum + secondnum Then
  250. Label5.Caption = "Your answer is correct!"
  251. Else
  252. Label5.Caption = "Your answer is wrong"
  253. End If
  254. End Sub
  255.  
  256. REM code for calculating the area of a rectangle and circle:
  257. Private Sub Command1_click()
  258. If Option1.Value = True Then
  259. Dim L, B, R, AREA As Integer
  260. L = InputBox("Enter the length")
  261. B = InputBox("Enter the length")
  262. AREA = L * B
  263. MsgBox "The area is " & AREA
  264. ElseIf Option2.Value = True Then
  265. Dim R, C, AREA As Integer
  266. R = InputBox("Enter the radius")
  267. AREA = 22.7 * R ^ 2
  268. MsgBox "The area is " & AREA
  269. End If
  270. End Sub
  271.  
  272. REM to display results on a label using select case:
  273. Private Sub Form_load()
  274. Dim IntAge
  275. IntAge = InputBox("Enter a number from 4-20 and you will tell yourself what kind of a gamer you are!")
  276. Select Case IntAge
  277. Case 4
  278. Label1.Caption = "You: I don't even play games"
  279. Case 5
  280. Label1.Caption = "You: I know how to use the AWSD keys"
  281. Case 6
  282. Label1.Caption = "You: I play games on the medium difficulty"
  283. Case 7
  284. Label1.Caption = "You: I have a PlayStation 2, PC and more than 50 games"
  285. Case 8
  286. Label1.Caption = "You: I pawn other players online and I make them cry"
  287. Case 9
  288. Label1.Caption = "You: I have a PlayStation 2, PlayStation 3, Xbox One, Xbox 360, PC, Nintendo Wii, Nintendo DS 3D and more than 750 games"
  289. Case Else
  290. Label1.Caption = "You: I am as good a gamer as Newt!"
  291. End Select
  292. End Sub
  293.  
  294. REM to display in which kind of school the user is in:
  295. Private Sub Form_load()
  296. Dim class
  297. class = InputBox("Enter the class you are in right now")
  298. Select Case class
  299. Case 1, 2, 3, 4, 5
  300. MsgBox "You are in Primary School"
  301. Case 6, 7, 8
  302. MsgBox "You are in Primary Secondary School"
  303. Case 9, 10
  304. MsgBox "You are in Senior School"
  305. Case 11, 12
  306. MsgBox "You are in Senior Secondary School"
  307. Case Else
  308. MsgBox "Wrong class! Enter a number between 1 and 12"
  309. End Select
  310. End Sub
  311.  
  312. REM to find whether a given figure is a square:
  313. Private Sub Command1_click()
  314. Dim SIDE1, SIDE2, SIDE3, SIDE4 As Integer
  315. SIDE1 = InputBox("Enter the first side of the 4 sided figure")
  316. SIDE2 = InputBox("Enter the second side of the 4 sided figure")
  317. SIDE3 = InputBox("Enter the third side of the 4 sided figure")
  318. SIDE4 = InputBox("Enter the fourth side of the 4 sided figure")
  319. If SIDE1 = SIDE2 And SIDE1 = SIDE3 And SIDE1 = SIDE4 Then
  320. MsgBox "The figure is a square"
  321. Else
  322. MsgBox "The figure is not a square"
  323. End If
  324. End Sub
  325.  
  326. REM to display text 5 times:
  327. Private Sub Command1_click()
  328. Dim VAR As Integer
  329. For VAR = 1 To 5 Step 1
  330. Print "I am Newt"
  331. Next VAR
  332. End Sub
  333.  
  334. REM to display numbers from 1 to 10:
  335. Private Sub Command1_click()
  336. Dim x As Integer
  337. For x = 1 To 10
  338. Print x
  339. Next
  340. End Sub
  341.  
  342. REM to display odd numbers from 1 to 10:
  343. Private Sub Command1_click()
  344. Dim x As Integer
  345. For x = 1 To 10 Step 2
  346. Print x
  347. Next
  348. End Sub
  349.  
  350. REM to display the series of 5 in the same line:
  351. Private Sub Command1_click()
  352. For x = 5 To 50 Step 5
  353. Print x & Space$ (2);
  354. Next
  355. End Sub
  356.  
  357. REM to display the sum of 5 numbers entered by the user:
  358. Private Sub Command1_click()
  359. Dim sum, num, I As Integer
  360. sum = 0
  361. For I = I To 5
  362. num = InputBox("Enter a number")
  363. sum = sum + num
  364. Next I
  365. Print "The sum of the five numbers is " & sum
  366. End Sub
  367.  
  368. REM to print the series of 1 to 10 in reverse order:
  369. Private Sub Command1_click()
  370. Dim I As Integer
  371. For I = 10 To 1 Step -1
  372. Print I
  373. Next I
  374. Print "The program ends"
  375. End Sub
  376.  
  377. REM to display text 5 times using DO LOOP:
  378. Private Sub Command1_click()
  379. Dim VAR As Integer
  380. VAR = 1
  381. Do
  382. Print "I am Newt"
  383. VAR = VAR + 1
  384. Loop While VAR <= 5
  385. End Sub
  386.  
  387. REM to accept a name in a text box and print the following pattern:
  388. Private Sub Print_click()
  389. I = 1
  390. Do While I <= Len(Text1.Text)
  391. Print Left(Text1.Text, I)
  392. I = I + 1
  393. Loop
  394. End Sub
  395.  
  396. REM to print the last three characters of the 5 names entered by the user in an input box:
  397. Private Sub Print_click()
  398. Dim str As String
  399. I = 0
  400. Do
  401. str = InputBox("Enter a name")
  402. Print Right(str, 3)
  403. I = I + 1
  404. Loop While I <= 5
  405. End Sub
  406.  
  407. REM to accept a number and display the sum of the digits of the number:
  408. Private Sub Command1_click()
  409. Dim sum, num, l As Integer
  410. sum = 0
  411. num = Val(Text1.Text)
  412. Do
  413. l = num Mod 10
  414. sum = sum + 1
  415. num = Int(num / 10)
  416. Loop While num > 0
  417. MsgBox "The sum of the digits is " & "sum"
  418. End Sub
  419.  
  420. REM to display the cube of a number till the user wants:
  421. Private Sub Display_click()
  422. Dim ans As String
  423. ans = "y"
  424. Do While ans = "y"
  425. n = InputBox("Enter a number")
  426. Print "The cube of "; " is " & n ^ S
  427. ans = InputBox("Do you wish to continue?")
  428. Loop
  429. End Sub
  430.  
  431. REM to print *** in different rows and columns:
  432. Private Sub Display_click()
  433. Dim row, col As Integer
  434. For row = 1 To 3
  435. For col = 1 To 3
  436. Print "*"
  437. Next col
  438. Print
  439. Next row
  440. End Sub
  441.  
  442. REM to print numbers in the form of a right angled triangle:
  443. Private Sub Command1_click()
  444. Dim row, col As Integer
  445. For row = 2 To 4
  446. For col = 1 To row
  447. Print col,
  448. Next col
  449. Next row
  450. End Sub
  451.  
  452. REM to print numbers in a decreasing kind of order:
  453. Private Sub Command1_click()
  454. Dim row, col As Integer
  455. For row = 1 To 5
  456. For col = 5 To row Step -1
  457. Print col,
  458. Next col
  459. Print
  460. Next row
  461. End Sub
  462.  
  463. REM to print numbers in the pattern i customized:
  464. Private Sub Command1_click()
  465. Dim row, col As Integer
  466. For row = 2 To 8 Step 2
  467. For col = 5 To row
  468. Print row,
  469. Next col
  470. Print
  471. Next row
  472. End Sub
  473.  
  474. REM to accept a number and generate the next 5 consecutive numbers and to repeat the whole process till desired:
  475. Private Sub Command1_click()
  476. Dim and As String
  477. Dim no, * As Integer
  478. ans = "y"
  479. Do While ans = "y"
  480. no = InputBox("Enter a number")
  481. For I = 1 To 5
  482. Print no + I
  483. Next I
  484. ans = InputBox("Do you wish to continue?")
  485. Loop
  486. End Sub
  487.  
  488. REM to show a form2 if you have multiple forms:
  489. Private Sub Form_load()
  490. Form2.Show
  491. End Sub
  492.  
  493. REM to show multiple forms when the user has pressed a function key:
  494. Private Sub f1_click()
  495. Form1.Show
  496. End Sub
  497.  
  498. Private Sub f2_click()
  499. Form2.Show
  500. End Sub
  501.  
  502. Private Sub f3_click()
  503. Form3.Show
  504. End Sub
  505.  
  506. Private Sub E1_click()
  507. End
  508. End Sub
  509.  
  510.  
  511. REM congratulations! you have finished newt's basic example guide!
  512. REM if you read through everything and used some of it - GREAT!
  513. REM if you just scrolled to the end - LOSER!
  514.  
  515. REM copyright 2014 newt drost
  516. REM distributed by the newt productions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement