Visual Basicfinal Course2
Visual Basicfinal Course2
اﻟﻣرﺣﻠﺔ :اﻻوﻟﻰ
اﻟﻘﺳم :اﻟﻔﯾزﯾﺎء
ﻋﻣﻠﻲ – اﻟﻛورس اﻟﺛﺎﻧﻲ
ﺑﺄﺷراف م .ﺣﯾدر ﻋﺑدﷲ ﻛﺎظم
اﻋداد
م ﻋﻠﻲ ﺟﺎﺳم ﻣﺣﻣد ﻋﻠﻲ
م .ﺳﻌد ﻧﺎﻓﻊ اﺑراھﯾم
ﯾﺘﻄﻠﻊ ﻣﺨﺘﺒﺮ اﻟﺤﺎﺳﺒﺎت اﻟﻰ اﻟﺘﻤﯿﺰ ﻣﻦ ﺧﻼل ﺗﻘﺪﯾﻢ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﻤﺨﺘﺒﺮات اﻟﺒﺮﻣﺠﯿﺔ ﻟﻠﻤﻮاد اﻟﺨﺎﺻﺔ ﺑﻘﺴﻢ اﻟﻔﯿﺰﯾﺎء ﺑﺤﯿﺚ ﯾﺤﻘﻖ
اﻟﻤﺴﺘﻮﯾﺎت اﻟﻤﻌﯿﺎرﯾﺔ اﻟﻤﺮﻣﻮﻗﺔ ﻓﻲ ﻣﺠﺎل اﻟﺤﺎﺳﺒﺎت ﻣﻤﺎ ﯾﺴﺎﻋﺪ ﻋﻠﻰ إﻋﺪاد ﻛﻔﺎءات وطﻨﯿﺔ ﻣﺆھﻠﺔ وﻣﺪرﺑﺔ .اﻟﺘﻤﯿﺰ ﻓﻲ ﻣﺠﺎل اﻟﺘﻌﻠﯿﻢ
واﻟﺒﺤﺚ اﻟﻌﻠﻤﻲ ﻟﻠﺘﻨﻤﯿﺔ اﻟﻤﺠﺘﻤﻌﯿﺔ ﺑﻤﻌﺎﯾﯿﺮ ﻣﻤﺘﺎزة.
اﻟﺮﺳﺎﻟﺔ
ﺗﻨﺒﺜﻖ رﺳﺎﻟﺔ ﻣﺨﺘﺒﺮ اﻟﺤﺎﺳﺒﺎت ﻣﻦ رﺳﺎﻟﺔ ﻗﺴﻢ اﻟﻔﯿﺰﯾﺎء ﺣﯿﺚ ﯾﮭﺪف ﻗﺴﻢ اﻟﻔﯿﺰﯾﺎء إﻟﻰ إﻋﺪاد ﻣﺘﺨﺼﺼﯿﻦ ﻣﺘﻤﯿﺰﯾﻦ ﻓﻲ ﻣﺠﺎل اﻟﺤﺎﺳﻮب
ﻓﻲ اﻟﻔﯿﺰﯾﺎء ،وﺗﻌﺰﯾﺮ اﻟﺒﺤﺚ اﻟﻌﻠﻤﻲ ،واﻹﺳﮭﺎم ﻓﻲ ﺧﺪﻣﺔ اﻟﻤﺠﺘﻤﻊ ﻣﺤﻠﯿﺎً وإﻗﻠﯿﻤﯿﺎ وﻓﻘﺎً ﻟﻤﻌﺎﯾﯿﺮ اﻟﺠﻮدة اﻟﺸﺎﻣﻠﺔ واﻟﻤﺘﻄﻠﺒﺎت اﻻﺧﻼﻗﯿﺔ
واﻟﻤﮭﻨﯿﺔ؛ وذﻟﻚ ﻣﻦ ﺧﻼل اﻋﺪاد ﺧﻄﻂ دراﺳﯿﺔ ﺗﺘﻮاﻛﺐ ﻣﻊ اﻟﻤﺴﺘﺠﺪات ﻓﻲ ﺗﺨﺼﺺ ﻋﻠﻮم اﻟﺤﺎﺳﻮب ,وﺗﻮظﯿﻒ ﻋﻠﻮم اﻟﺤﺎﺳﻮب ﻓﻲ
ﻋﻤﻠﯿﺘﻲ اﻟﺘﻌﻠﯿﻢ واﻟﺘﻌﻠﻢ وﺗﺒﻨﻲ اﺣﺪث اﺳﺘﺮاﺗﯿﺠﯿﺎت اﻟﺘﻌﻠﻢ ﻟﺒﻨﺎء اﻟﻤﻌﺮﻓﺔ واﻟﻘﺪرات وﺻﻘﻞ ﻣﮭﺎرات اﻟﻄﻠﺒﺔ .اﻋﺪاد وﺗﻄﻮﯾﺮ اﻟﻜﻮادر
اﻟﻌﻠﻤﯿﺔ اﻟﻤﺘﺨﺼﺼﺔ ﻓﻲ اﻟﻤﺨﺘﺒﺮ ﻟﯿﺼﺒﺢ ﻟﻘﺴﻢ اﻟﻔﯿﺰﯾﺎء أﻧﻤﻮذﺟﺎ أﻛﺎدﯾﻤﯿﺎ راﺋﺪا وﻣﺘﻤﯿﺰا ﻣﻦ ﺧﻼل إﻋﺪاد اﻟﻄﻠﺒﺔ ﻟﺴﻮق اﻟﻌﻤﻞ وﺗﻨﻤﯿﺔ
ﻗﺪراﺗﮭﻢ ﻋﻠﻰ اﻟﺘﻔﺎﻋﻞ واﻟﺘﻮاﺻﻞ ﻣﻊ اﻵﺧﺮﯾﻦ وﺻﻮﻻ ﻟﻤﺠﺘﻤﻊ اﻟﻤﻌﻠﻮﻣﺎﺗﯿﺔ.
اﻷھﺪاف
ان اﻷھﺪاف اﻟﺮﺋﯿﺴﯿﺔ ﻟﻠﻤﺨﺘﺒﺮ ھﻲ ﺿﻤﺎن أن ﯾﺘﺨﺮج اﻟﻄﺎﻟﺐ وھﻮ ﯾﻤﺘﻠﻚ اﻟﻤﮭﺎرات واﻟﻤﻌﺎرف اﻟﺘﺎﻟﯿﺔ:
1|Page
ﻣﻘﺪﻣﺔ ﻓﻲ اﻟﺒﺮﻣﺠﺔ
Visual Basicﻟﻠﺘﻄﺒﯿﻘﺎت ) (VBAھﻲ ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ اﻟﻤﺴﺘﺨﺪﻣﺔ ﻓﻲ Excel.ﻣﻊ ذﻟﻚ ﯾﻤﻜﻨﻚ أﺗﻤﺘﺔ اﻟﻤﮭﺎم،
إﺿﺎﻓﺔ ﻣﯿﺰات ﻣﺨﺼﺼﺔ وظﺎﺋﻒ ،وﺣﺘﻰ إﻧﺸﺎء ﺗﻄﺒﯿﻘﺎت ﻛﺎﻣﻠﺔ .
ﻓﻲ Excelوﻣﺆﺗﻤﺘﺔ اﻟﻤﮭﺎم ﻣﻦ ﻗﺒﻞ وﺣﺪات اﻟﻤﺎﻛﺮو -ﺳﻠﺴﻠﺔ ﻣﻦ اﻟﺘﻌﻠﯿﻤﺎت اﻟﻤﻜﺘﻮﺑﺔ ﻓﻲ VBأن أﻗﻮل إﻛﺴﻞ ﻣﺎ
ﯾﺠﺐ اﻟﻘﯿﺎم ﺑﮫ .ﻟﺘﺴﮭﯿﻞ ﺧﻠﻘﮭﻢ ﯾﻮﻓﺮ Excelﻣﯿﺰة ﺗﺴﻤﻰ ﻣﺴﺠﻞ اﻟﻤﺎﻛﺮو -اﻟﺬي ﯾﻜﺘﺐ وﺣﺪات اﻟﻤﺎﻛﺮو ﺑﺎﻟﻨﺴﺒﺔ ﻟﻚ!
ﻷﻧﮫ ﯾﺨﺰن اﻹﺟﺮاءات ﻟﻮﺣﺔ اﻟﻤﻔﺎﺗﯿﺢ واﻷواﻣﺮ اﻟﺘﻲ ﺗﺴﺘﺨﺪﻣﮭﺎ .ﻓﻲ وﻗﺖ ﻻﺣﻖ ﯾﻤﻜﻦ أن ﺗﻘﻮم ﺑﮫ ﻣﺮة أﺧﺮى ،أو
ﺗﺸﻐﯿﻞ ،اﻟﻤﺎﻛﺮو ﻟﺘﻜﺮار ﺗﻠﻘﺎﺋﯿﺎ اﻹﺟﺮاءات .ورﺑﻤﺎ ھﺬا ھﻮ أﺳﮭﻞ طﺮﯾﻘﺔ ﻟﻠﻮﺻﻮل اﻟﻰ ﺑﺮﻣﺠﺔ VBواﻟﻜﻠﻲ .ﺳﻮف
ﺗﻜﺘﺸﻒ ﻗﺮﯾﺒﺎ ﻛﯿﻒ ﺣﺪات اﻟﻤﺎﻛﺮو ﻣﻔﯿﺪة ﯾﻤﻜﻦ أن ﯾﻜﻮن وﯾﺮﯾﺪون ﺗﻌﺪﯾﻞ اﻟﺘﻌﻠﯿﻤﺎت اﻟﺒﺮﻣﺠﯿﺔ اﻟﻤﺴﺠﻞ وذﻟﻚ
ﻟﺘﺤﺴﯿﻦ أو ﻻﺳﺘﺨﺪاﻣﮭﺎ ﻓﻲ وﺿﻊ ﻣﺨﺘﻠﻒ ﻗﻠﯿﻼ.
إن ﻛﻨﺖ ﺗﻌﺮف اﻟﻤﺰﯾﺪ ﺣﻮل ، Excelوأﻓﻀﻞ اﺳﺘﻌﺪادا ﺳﻮف ﺗﻜﻮن ﻟﻮﺣﺪات اﻟﻤﺎﻛﺮو و VB.ﻣﻌﻈﻢ وﺣﺪات
اﻟﻤﺎﻛﺮو ﺗﺆدي ﺳﻠﺴﻠﺔ ﻣﻦ اﻹﺟﺮاءات ﻓﻲ Excel.ﻣﻌﻈﻢ ﺗﻌﻠﯿﻤﺎت اﻟﻜﻠﯿﺔ ﺗﻌﺎدل اﻷواﻣﺮ أو اﻹﺟﺮاءات ﻓﻲ
Excel.ﺗﻠﻚ اﻟﺘﻲ ﻟﯿﺴﺖ ،ﻣﻊ ذﻟﻚ ،ﻣﻦ ﺑﯿﻦ أﻛﺜﺮ ﻓﺎﺋﺪة! وﻣﻊ ذﻟﻚ اﻟﺒﯿﺎﻧﺎت واﻟﻮظﺎﺋﻒ اﻟﻤﺴﺘﺨﺪﻣﺔ ﻟﻜﺘﺎﺑﺔ اﻟﺘﻌﻠﯿﻤﺎت
ھﻲ أﺳﮭﻞ ﻟﻠﻔﮭﻢ إذا ﻛﻨﺖ ﺗﻌﺮف اﻟﻤﯿﺰات اﻟﺘﻲ ﯾﻤﺜﻠﻮﻧﮭﺎ ﻓﻲExcel.
VBﯾﻤﻜﻦ أن ﯾﺒﺪو اﻟﺴﺎﺣﻘﺔ وﺧﺎﺻﺔ إذا ﻛﺎن ﻟﺪﯾﻚ أي ﺧﺒﺮة ﻣﻊ ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ .ﻟﻠﺘﻐﻠﺐ ﻋﻠﻰ ھﺬا ﯾﻤﻜﻨﻨﺎ اﺳﺘﺨﺪام
ﻣﺮﻓﻖ ﻣﺴﺠﻞ اﻟﻤﺎﻛﺮو Excelﻟﻨﺮى ﻛﯿﻒ ﺗﺘﺮﺟﻢ إﻟﻰ إﺟﺮاءات إﻛﺴﻞ VBﺗﻌﻠﯿﻤﺎت واﻟﻌﻜﺲ ﺑﺎﻟﻌﻜﺲ .ﺗﻜﺘﯿﻚ
واﺣﺪ ﻟﻜﺘﺎﺑﺔ ﺟﺰء ﻣﻦ اﻟﻤﺎﻛﺮو اﻟﺨﺎص ﺑﻚ ﻋﻦ طﺮﯾﻖ ﻛﺘﺎﺑﺔ اﻟﺒﯿﺎﻧﺎت ﻣﺒﺎﺷﺮة VBﻓﻲ اﻟﻤﺎﻛﺮو ،ﺛﻢ ﺗﺴﺠﯿﻞ ﺟﺰء
ﻣﻨﮫ ﺑﺎﺳﺘﺨﺪام ﻣﺴﺠﻞ اﻟﻤﺎﻛﺮو( ﻏﺎﻟﺒﺎ ﻷﻧﻚ ﻻ ﺗﻌﺮف ﻛﯿﻒ ﺗﻜﺘﺐ ھﺬا اﻟﺠﺰء ﻓﻲ )! VBودﻣﺞ اﻷﺟﺰاء .ﺗﺴﺠﯿﻞ
ﺟﺰء ﻣﻦ اﻟﻤﺎﻛﺮو ﻏﺎﻟﺒﺎ ﻣﺎ ﯾﻜﻮن أﺳﺮع وأﺳﮭﻞ ﻣﻦ ﻛﺘﺎﺑﺔ ﺧﺎرج اﻟﻘﻮاﺋﻢ VB.ﻓﻲ وﺣﺪة ﻧﻤﻄﯿﺔ VBﯾﻤﻜﻨﻚ ﻛﺘﺎﺑﺔ
اﻟﻜﻠﻤﺔ ،وﻣﻊ ﻧﻘﻄﺔ اﻹدراج ﻓﻲ ﻣﻜﺎن ﻣﺎ ﻓﻲ اﻟﻜﻠﻤﺔ ،اﺿﻐﻂ F1ﻟﻌﺮض اﻟﺘﻌﻠﯿﻤﺎت ﻋﻠﻰ ھﺬه اﻟﻜﻠﻤﺔ .ﺑﺎﻟﻨﺴﺒﺔ ﻟﻜﺜﯿﺮ
ﻣﻦ اﻟﻜﻠﻤﺎت اﻟﺮﺋﯿﺴﯿﺔ ﯾﺘﻢ إﻋﻄﺎء ھﻨﺎك ﻣﺜﺎل ﻋﻠﻰ اﺳﺘﺨﺪاﻣﮫ واﻟﺬي ﯾﻤﻜﻨﻚ ﻧﺴﺦ وﻟﺼﻖ إﻟﻰ ﻣﺎﻛﺮو اﻟﺨﺎص ﺑﻚ.
2|Page
أﺳﻠﻮب اﻟﺒﺮﻣﺠﺔ
وﻣﻦ اﻟﻀﺮوري أن ﯾﻜﻮن ھﻨﺎك اﻟﻜﺜﯿﺮ ﻣﻦ اﻟﺘﻌﻠﯿﻘﺎت ﻓﻲ ﻣﺎﻛﺮو ﺷﺮح ﻣﺎ ﻣﺨﺘﻠﻒ أﻗﺴﺎم رﻣﺰ ﯾﻔﻌﻠﻮن .ھﺬه
اﻟﺘﻌﻠﯿﻘﺎت ﯾﺴﺎﻋﺪ ﻛﺜﯿﺮا ﺗﺼﺤﯿﺢ اﻟﺘﻌﻠﯿﻤﺎت اﻟﺒﺮﻣﺠﯿﺔ .ﻛﻨﺖ ﻻ ﺗﺮﯾﺪ أن ﯾﻜﻮن ﻟﻠﻌﻤﻞ اﻟﺨﺮوج ﻓﻲ ﻛﻞ ﻣﺮة ﻣﺎ ﺗﻘﻮم ﺑﮫ.
إذا ﻛﺎن ﺑﻌﺪ ﻓﺘﺮة طﻮﯾﻠﺔ ﺗﻌﻮد إﻟﻰ رﻣﺰ ﺑﺪون اﻟﺘﻌﻠﯿﻘﺎت اﻟﺘﻲ ﻣﻦ ﻏﯿﺮ اﻟﻤﺮﺟﺢ أن ﻧﺘﺬﻛﺮ ﻣﺎ ﯾﻔﻌﻠﮫ اﻟﻤﺎﻛﺮو ﻓﻲ
اﻟﺘﻔﺎﺻﯿﻞ ،ﻋﻠﻰ ﻛﻞ ﺣﺎل .اﻟﻔﺎﺻﻠﺔ اﻟﻌﻠﯿﺎ )'( ﺑﺘﻘﺪﯾﻢ ﺗﻌﻠﯿﻖ ﻓﻲ اﻟﺘﻌﻠﯿﻤﺎت اﻟﺒﺮﻣﺠﯿﺔ .ﻻ ﯾﺘﻢ ﺗﻨﻔﯿﺬ ﺗﺼﺮﯾﺤﺎت ﻟﻠﯿﻤﯿﻦ
"ﻋﻠﻰ ﻧﻔﺲ اﻟﺨﻂ.
وﻣﻦ اﻟﻤﻔﯿﺪ ﺗﺒﻨﻲ أﺳﻠﻮب اﻟﺒﺮﻣﺠﺔ ﻟﺴﮭﻮﻟﺔ ﻗﺮاءة اﻟﺘﻌﻠﯿﻤﺎت اﻟﺒﺮﻣﺠﯿﺔ .وﺳﻨﻌﻤﻞ ﻋﻠﻰ اﻻﺳﺘﻔﺎدة أﺳﻤﺎء وﺣﺪات
اﻟﻤﺎﻛﺮو واﻟﺪاﻻت اﻟﻤﻌﺮﻓﺔ ﻣﻦ ﻗﺒﻞ اﻟﻤﺴﺘﺨﺪم .ﻻ ﯾﻤﻜﻦ أن ﺗﺘﻀﻤﻦ أﺳﻤﺎء ووظﺎﺋﻒ اﻟﻜﻠﻰ اﻟﻤﺴﺎﺣﺎت ﺣﺘﻰ إذا
ﯾﺘﻜﻮن اﺳﻢ أﻛﺜﺮ ﻣﻦ ﻛﻠﻤﺔ واﺣﺪة وﺑﻌﺒﺎرة أﺧﺮى ﻗﺪ رﺳﺎﺋﻠﮭﻢ اﻷوﻟﯿﺔ رﺳﻤﻠﺘﮭﺎ ،ﻋﻠﻰ ﺳﺒﯿﻞ اﻟﻤﺜﺎل، TaxReturns
اﻟﺪورات اﻟﺪراﺳﯿﺔ.
وﯾﺒﺪو أن اﻟﺤﺠﺞ وأﺳﻤﺎء اﻟﻤﺘﻐﯿﺮات ﻣﻊ اﻷﺣﺮف اﻷوﻟﻰ ﻓﻲ ﺻﻐﯿﺮة -ﻟﺘﻤﯿﯿﺰھﺎ ﻋﻦ وﺣﺪات اﻟﻤﺎﻛﺮو ،وظﺎﺋﻒ،
ﺧﺼﺎﺋﺺ وأﺳﺎﻟﯿﺐ وأﺳﻤﺎء اﻟﻜﺎﺋﻨﺎت.
ﺣﺮف ﻣﺘﺎﺑﻌﺔ اﻟﺴﻄﺮ ،ﺗﺴﻄﯿﺮ ،(_) ،ﯾﺸﯿﺮ إﻟﻰ أن رﻣﺰ اﺳﺘﻤﺮ ﻣﻦ ﺳﻄﺮ واﺣﺪ إﻟﻰ اﻟﺘﺎﻟﻲ ھﻮ ﺟﺰء ﻣﻦ اﻟﺒﯿﺎن ﻧﻔﺴﮫ
Else
Discount = 0
End If
)Discount = Application.Round(Discount,2
End Function
3|Page
Visual basic statements 1.1
* Variables
Notes:
- Variable name must start with character (not number or function) and
maximum length 256 character, and does not contain point or symbol.
The variable has to be declared. Variable type is defined by its content .The
content may be data as numeric or character or string or Boolean or date, or any
type of data (called variant), these types declared as:
Or
Global variable name as type
4|Page
Note: The Dim declaration written in general part of the form or in any place
in form or sub procedure which used for one form. While Global declaration
used for all forms
The types of variables that are allowed in visual basic are stated in the table
below.
Types of variables
* Constants
It is a space in memory filled with fixed value that will not be changed.
Constant may be declared as:
5|Page
Inputting variables
There are methods to input variable x as stated in the following:
Note: To enter many variables we usually use the second method with loop.
6|Page
e2x Exp(2*x)/(cos(2*x)+sin(x))
cos(2x) sin(x)
Sol:
Area
code stage:
Const p = 3.14159
Dim a, r As Single
7|Page
. ﻛﺗﺎﺑﺔ ﺑرﻧﺎﻣﺞ ﻟﺗﻧﻔﯾذ اﻟﻌﻣﻠﯾﺎت اﻷرﺑﻊ وﻓﻘﺎ ﻟﺗﺻﻣﯾم اﻟﺗﺎﻟﯾﺔ:(2) ﻣﺛﺎل
Label1 Command1
Label2 Command2
Label3 Command3
Command4
Command6 Command5
Code stage:
Dim a,b, c as single
Private sub command1_click ()
a=val(text1.text)
b=val(text2.text)
c=a + b
End sub
Private sub command2_click ()
a=val(text1.text)
b=val(text2.text)
c=a - b
End sub
Private sub command3_click ()
a=val(text1.text)
b=val(text2.text)
c=a * b
End sub
Private sub command4_click ()
a=val(text1.text)
b=val(text2.text)
c=a / b
End sub
Private sub command5_click ()
Text3.text=cstr(c)
End sub
Private sub command6_click ()
Text1.text=" "
8|Page
Text2.text=" "
Text3.text=" " End sub
Check box:
Any number of check boxes can be used on a form. They work independently.
Its Property value could be changed in design stage manually, or in running stage by code.
ﺗﻐﯿﯿﺮ: ﺗﺼﻤﯿﻢ ﻧﻤﻮذج ﻣﻊ ﻣﺮﺑﻊ ﻧﺺ واﺣﺪ وﺛﻼﺛﺔ ﺧﺎﻧﺎت اﻻﺧﺘﯿﺎر ﺑﺤﯿﺚ ﻋﻨﺪﻣﺎ ﺗﻀﻐﻂ ﻋﻠﻰ ﻣﺮﺑﻌﺎت ﯾﺘﻢ اﻟﺘﺎﻟﯿﺔ:(3) ﻣﺜﺎل
.Sol: ﺗﺴﻄﯿﺮ، ﻣﺎﺋﻞ،اﻟﻜﺘﺎﺑﺔ ﻟﻌﺮﯾﺾ
Text1
Text ﻓﺎﺭﻍ
Text1
Check1
caption Bold
Check2 Check1
caption Italic Check2
Check3
caption underline Check3
Run stage:
9|Page
2.1Conditional statements
1- If statement
2- Select case
The comparison operations are: (<, <=, >, >=, =, <>, and, or)
Example (4): write a program to enter a mark of a student then print (pass) if
he successful.
Sol:
Dim x as integer
Private sub command1_click()
X= val(text1.text)
If x>= 50 then text2.text= "pass"
End sub
10 | P a g e
b) If block structure: Used for running many programming statements if the
required condition satisfied.
If condition then
Statements
End if
ﺗﺼﻤﯿﻢ ﻧﻤﻮذج ﻣﻊ ﻣﺮﺑﻊ ﻧﺺ واﺣﺪ وﺛﻼﺛﺔ ﺧﺎﻧﺎت اﻻﺧﺘﯿﺎر ﺑﺤﯿﺚ ﻋﻨﺪﻣﺎ ﺗﻀﻐﻂ ﻋﻠﻰ ﻣﺮﺑﻌﺎت ﯾﺘﻢ:(3) ﻣﺜﺎل
. ﺗﺴﻄﯿﺮ، ﻣﺎﺋﻞ، ﺗﻐﯿﯿﺮ اﻟﻜﺘﺎﺑﺔ ﻟﻌﺮﯾﺾ:اﻟﺘﺎﻟﯿﺔ
Sol:
Dim x as integer
Private sub command1_click()
X= val(text1.text)
If x>= 50 then
text2.text= "pass"
text2.fontsize=18
end if
End sub
11 | P a g e
If.. Then.. Else structure: Used for running many programming statements if the
required condition satisfied. And running another programming statements (after
else) if the required condition not satisfied.
If condition then
Statements Else
Statements End if
ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻹدﺧﺎل ﻋﻼﻣﺔ اﻟﻄﺎﻟﺐ ﺛﻢ طﺒﺎﻋﺘﮭﺎ )ﺗﻤﺮﯾﺮ( إذا ﻛﺎن ﻧﺎﺟﺤﺎ واﻟﻄﺒﺎﻋﺔ )ﺗﻔﺸﻞ( ﺧﻼف:(6) ﻣﺜﺎل
.ذﻟﻚ
Sol:
Dim x As Integer
Private Sub command1_click()
x = val(Text1.Text)
If x >= 50 Then
Text2.Text = "pass"
Else
Text2.Text = "fail"
End If
End Sub
12 | P a g e
،( أول واﺣﺪ ل)ﻣﻨﻰ. ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻹدﺧﺎل اﺳﻢ اﻟﻤﺴﺘﺨﺪم وﻋﺮض اﻟﺮﺳﺎﻟﺔ )ﻣﺮﺣﺒﺎ( ﺛﻼث ﻣﺮات:(7) ﻣﺜﺎل
.واﻟﺜﺎﻧﯿﺔ ﻋﻦ )ﻣﮭﺎ( واﻟﺜﺎﻟﺜﺔ ﻷي ﻣﺴﺘﺨﺪم ﻛﻀﯿﻒ
Sol:
Dim x As String
Private Sub command1_click() Hello Muna
x = Text1.Text
If x = "Muna" Then
Label1.caption= "hello, Muna"
ElseIf x = "Maha" Then
Label1.caption= "hello,Maha"
Else
Label1.caption= "hello, guest"
End If
End Sub
ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﺘﺼﻨﯿﻒ أي ﻋﺪد دﺧﻠﺖ وﻓﻘﺎ ﻟﻌﻼﻣﺔ وﻋﺮض ﻋﺒﺎرة )رﻗﻢ ﺳﺎﻟﺐ( ﻋﻨﺪﻣﺎ ﻛﺎن اﻟﺮﻗﻢ:(8) ﻣﺜﺎل
.( وإﻻ ﻋﺮض ﻋﺒﺎرة )ﻻ اﯾﺠﺎﺑﯿﺔ وﻻ ﺳﻠﺒﯿﺔ،ﺳﺎﻟﺒﺎ وﻋﺒﺎرة )رﻗﻢ ﻣﻮﺟﺐ( ﻋﻨﺪﻣﺎ ﯾﻜﻮن ﻋﺪد إﯾﺠﺎﺑﯿﺔ
Sol:
Dim x As Single
Private Sub command1_click ()
x = Val(Text1.Text)
If x > 0 Then
Label1.caption= "positive
number" ElseIf x < 0 Then
Label1. caption = "negative
number" Else
Label1. caption = "neither positive nor
negative"
End If
End Sub
13 | P a g e
ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻹدﺧﺎل رﻗﻤﯿﻦ وﺣﺴﺎب ﻋﻤﻠﯿﺎت اﻟﻀﺮب واﻟﻘﺴﻤﺔ:(9) ﻣﺜﺎل
ﺑﺎﺳﺘﺨﺪام زر اﻟﺨﯿﺎر ﻣﻊ اﻟﻌﺮض ﻋﺒﺎرة )ﻋﻤﻠﯿﺔ ﺗﻘﺴﯿﻢ ﻏﯿﺮ ﻗﺎﻧﻮﻧﯿﺔ( ﻋﻨﺪﻣﺎ
.اﻟﻘﺎﺳﻢ ھﻮ ﺻﻔﺮ
Sol:
Dim a, b, c As Single
Private Sub command1_click()
a = Val(Text1.Text)
b = Val(Text2.Text)
If Option1.Value Then
c=a*b
Text3.Text =c elseIf b <>
0 Then
c=a/b
Text3.Text =c)
Else
Text3.Text = "illegal division operation"
End If
End If
End Sub
-Select statement
Used for applying many statements depending on one variable. The general
form is:
14 | P a g e
ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﻄﺒﺎﻋﺔ أﯾﺎم اﻷﺳﺒﻮع ﻋﻨﺪﻣﺎ ﻧﺪﺧﻞ رﻗﻤﮫ:(10) ﻣﺜﺎل
Sol:
Dim x As Integer
Private Sub Command1_Click()
x = val(Text1.Text)
Select Case x
Case 1
Label1. caption ="Sunday"
Case 2
Label1. caption = "Monday"
Case 3
Label1. caption = "Tuesday"
Case 4
Label1. caption = "Thursday"
Case 5
Label1. caption = "Wednesday
Case 6
Label1. caption =
"Friday"
Case 7
Label1. caption =
"Saturday"
End Select
End Sub
15 | P a g e
: ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻹﻋﻄﺎء ﺗﻘﯿﯿﻢ ﻟﻌﻼﻣﺎت ﻣﺨﺘﻠﻔﺔ ﻋﻠﻰ اﻟﻨﺤﻮ اﻟﺘﺎﻟﻲ:(11) ﻣﺜﺎل
mark evaluation
90-100 Excellent
80-89 Very good
70-79 Good
60-69 Medium
50-59 Pass
0-49 Fail
Dim x As Integer
Private Sub Command1_Click()
x=
val(Text1.Tex
t)
Select Case x
Case 90 To 100
Label1. caption
= "excellent"
Case 80 To 89
Label1. caption
= "very good"
Case 70 To 79
Label1.
caption =
"good"
Case 60
To 69
Label1.
caption =
"medium"
Case 50 To 59
Label1.
caption =
"pass"
Case 0 To
49
Label1. caption = "fail"
Case Else
Label1. caption = "the range is 0-100"
16 | P a g e
End Select
End Sub
3.Loop statement:
Visual basic supports statement to perform loops. The loops statements could
have different structures as follows:
1- Counter loop.
2- Conditional loop.
1- Counter loop:
Loops apply programming statements for fixed number of times using counter
(for… next) statement.
Sol:
Dim i as integer
Private Sub Command1_Click ()
For i = 1 To 5
Print "hello"
Next i
End Sub
Sol:
Dim i as integer
Private Sub Command1_Click ()
For i = 2 To 10 step 2
Print i
Next i
End Sub
Notes:
1-The variable's value that we use as counter must be integer value (integer, long).
2- If we don’t determined the step value then the assumed value is 1.
3- If the final value smallest than the initial value, then the step value must
be negative.
17 | P a g e
4.Nested loop:
The nested loops are the loops that are placed inside each other. The most
inner
loop will be executed first, then the outer ones. These loops should neither
intersect, nor have the same index. As follows:
For i = 1 To n
For j = 1 To m
Statements
Next j
Next i
ﺛم ﺟﻌل ﺗﻐﯾﯾر اﻟﻠون وﺣﺟم. وأول واﺣد ﻣﻊ ﺣﺟم اﻟﻌﺎدﯾﺔ واﻟﻠون، ﻛﺗﺎﺑﺔ ﺑرﻧﺎﻣﺞ ﻟطﺑﺎﻋﺔ )ﺗرﺣﯾب( ﻋﺷر ﻣرات:(12) ﻣﺛﺎل
.أﻛﺑر ﻓﻲ ﻛل ﻣرة
Sol:
Dim i As Integer
Private Sub
Command1_Click()
Print "welcome"
For i = 1 To 9
FontSize = 10 + i
ForeColor =
QBColor(i)
Print "welcome"
Next i
End Sub
18 | P a g e
.1 ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﺘﻮﻟﯿﺪ اﻷرﻗﺎم ﻓﻲ اﻟﻨﻤﻮذج اﻟﺘﺎﻟﻲ:(13) ﻣﺜﺎل
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sol:
Dim I, j As Integer
Private Sub
command1_click() For I =
1 To 5
For j = 1 To i
Print
j;
Nexj
Print
NexI
End
Sub
Dim I, j As Integer
Private Sub
command1_click() For I =
1 To 10
For j = 1 To
10 p = I * j
Print I; "*"; j;
"="; p, Next j
Print
Next
I
End
Sub
19 | P a g e
Series:
To compute the value of series, we use suitable loop statements according to
the
boundaries (limits) of each series.
20 | P a g e
Input - output boxes
There are two types of dialog boxes which are inputbox and messagebox. The first is
used to input variable and the second to output variable or message. Both needs code
and appear at run time.
a) Inputbox
Inputbox used to input value or characters for one variable from keyboard at running
stage.
This box needs a code in code sheet and could be written in any event or command
X=inputbox(" prompt or remark", "title")
21 | P a g e
Message box
It is used to output a message to the user (at running stage) the code needed could be
written in code sheet and in any event or command.
vbquestion 32
vbexclamation 48
vbinformation 64
For example if we write the following statement then a message box will be appear as
shown below
or
MsgBox "please close your program", vbcritical, "Error"
or
MsgBox "are you sure you want to delete this file",vbQuestion+vbYesNo,"delete"
Sol:
Example (16): write a program to move the text (excellent) from textbox to message box
and change the color of the text after click on command button (display).
Sol:
22 | P a g e
Text1: text="excellent" Command1:
caption=""ضﺮﻋ
Sub Command1_Click()
X= Text1.Text
MsgBox (X)
Text1.BackColor = QBColor(9)
Text1.Text = " "
End Sub
23 | P a g e
Menu
وﯾﻤﻜﻦ. وﻟﻜﻦ ﯾﻤﻜﻦ ﻟﻠﻤﺴﺘﺨﺪم إﺿﺎﻓﺘﮫ، ﺷﻜﻞ ھﻮ ﻣﻌﯿﺎرﻋﺮض دون اﻟﻘﺎﺋﻤﺔ.اﻟﻘﺎﺋﻤﺔ ھﻮ ﺷﺮﯾﻂ ﻓﻲ أﻋﻠﻰ اﻟﻨﻤﻮذج
ﻓﻲ اﻟﻤﻘﻄﻊ اﻟﺘﺎﻟﻲ ﻣﺤﺮر اﻟﻘﺎﺋﻤﺔ وﺳﻮف ﺗﻨﺎﻗﺶ رﻣﺰ.إدراج ھﺬه اﻟﻘﺎﺋﻤﺔ ﻓﻲ اﻟﻨﻤﻮذج ﺑﺎﺳﺘﺨﺪام ﻣﺤﺮر اﻟﻘﺎﺋﻤﺔ
.اﻟﻤﻄﻠﻮﺑﺔ
Menu Editor
To use menu there are three ways:
1- Press menu icon from toolbar .
2- press (ctrl+E) from key board.
3- click on: tools>Menu Editor.
Menu editor box appears as shown below.
24 | P a g e
To add many items Use the menu Editor
25 | P a g e
Create sub Menu list Use menu Editor
Click on item
اﺣﺗوت اﻟﻘﺎﺋﻣﺔ ﻋﻠﻰ اﻟﻠون ﻋﻧﺻر واﺣد ﻣﻊ ﻋﻧﺎﺻر اﻟﻘﺎﺋﻣﺔ. ﺗﺻﻣﯾم ﻧﻣوذج ﻣﻊ اﻟﻘﺎﺋﻣﺔ واﻟﺗﺳﻣﯾﺔ ﻣﻊ ﻋﻧوان ﻣﺣدد:(17) ﻣﺛﺎل
. ﻟﺗﻠوﯾن اﻟﺗﺳﻣﯾﺔ ﻓﻲ اﻷﺣﻣر واﻷﺧﺿر واﻷزرق ﺛم اﻟﺧروج ﻣن اﻟﺑرﻧﺎﻣﺞ، اﻷﺣﻣر واﻷﺧﺿر واﻷزرق واﻟﺧروج:اﻟﻔرﻋﯾﺔ
( ﻣﻊ أي ﺷرح ﻋﻠﻰ ﺳﺑﯾل اﻟﻣﺛﺎل )ﻣرﺣﺑﺎLabel1 وﺿﻊ:ﺳول
26 | P a g e
Caption: green Do the same thinks for other items>ok
Name:command3
Caption: blue
Name:command4
Caption: exit
Name:command5
Shortcut: ctrl+E
27 | P a g e
Assignment statement
There are many statements ways to fill a variable as follows:
Variable = expression
Function Description
Abs(x) Absolute of x
Sqr(x) Square root of x
Int(x) Integer of x
Exp(x) Exponential of x (ex)
Fix(x) Take the integer part
Sin(x), cos(x), tan(x) Trigonometric functions
Log(x) Natural logarithms
Len(x) Number of character of variable x
Lcase(x) Change the text x to small letters
Ucase(x) Change the text x to capital letters
Cint(x) Convert x to integer
Clong(x) Convert x to long integer
Cdbl(x) Convert x to double precision
Cstr(x) Convert variable x to string
Val(x) Convert string x to numerical variable
Note: the last five functions are called conversion functions.
The instruction print could be very helpful to display data and used as
follows:
Design stage:
Form1
Caption String length
Command1
caption Click here
Label1
Caption Length=
Text1
Text
29 | P a g e
Code stage:
Dim s As String
Private Sub Command1_Click()
s = InputBox("inter string")
L = Len(s)
Text1.Text = CStr(L)
End Sub
Running stage:
اﺳﺘﺨﺪم ﻣﺮﺑﻊ رﺳﺎﻟﺔ. ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﺠﻤﻊ وطﺮح رﻗﻤﯿﻦ ﺻﺤﯿﺢ ﺑﻌﺪ وﺿﻊ ﺗﺼﻤﯿﻢ ﻣﻨﺎﺳﺐ:19 ﻣﺜﺎل
.ﻹﺧﺮاج
Design stage:
form
caption calculator
Command1
caption +
Command2
Caption -
Label1
Caption Enter no.1
Label2
Caption Enter no.2
text ﻤﻥ ﺨﺎﺼﻴﺔtext1, text2 ﻭﻨﻔﺭﻍ
30 | P a g e
Code stage:
Dim x, y, z as integer
Private sub command1_click ()
X=val(text1.text)
Y=val(text2.text)
Z=x + y
Msgbox("addition result="&z)
End sub
Private sub command2_click ()
X=val(text1.text)
Y=val(text2.text)
Z=x - y
Msgbox("subtraction result="&z)
End sub
Running stage
Enter two values in text1 and text2. When click on command (+) or (-) the
addition or subtraction result appears in message box.
Label1 Command1
Label2 Command2
Label3 Command3
Command4
Command6 Command5
Code stage:
Dim a,b, c as single
Private sub command1_click ()
a=val(text1.text)
b=val(text2.text)
c=a + b
End sub
31 | P a g e
Private sub command2_click ()
a=val(text1.text)
b=val(text2.text)
c=a - b
End sub
Private sub command3_click ()
a=val(text1.text)
b=val(text2.text)
c=a * b
End sub
Private sub command4_click ()
a=val(text1.text)
b=val(text2.text)
c=a / b
End sub
Private sub command5_click ()
Text3.text=cstr(c)
End sub
Private sub command6_click ()
Text1.text=" "
Text2.text=" "
Text3.text=" "
End sub
اﻟﻘﯿﻤﺔ، ﻣﺮﺑﻊ، ﻗﯿﻤﺔ ﻋﺪدﯾﺔ، اﻟﺠﯿﺐ وﺟﯿﺐ اﻟﺘﻤﺎم:ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﺤﺴﺎب وظﺎﺋﻒ ھﻲ: (21)ﻣﺘﺎل
.:اﻟﻤﻄﻠﻘﺔ
Design stage:
Dim x, y As Single
Private Sub command1_click()
x = Val(Text1.Text)
y = Abs(x)
Text1.Text = CStr(y)
End Sub
Private Sub Command2_Click()
x = Val(Text1.Text)
y = Sqr(x)
Text1.Text = CStr(y)
End Sub
32 | P a g e
Private Sub Command3_Click()
x = Val(Text1.Text)
y = Int(x)
Text1.Text = CStr(y) End Sub
Private Sub Command4_Click()
x = Val(Text1.Text)
y = Sin(x * 3.14159 / 180) Text1.Text =
CStr(y)
End Sub
Private Sub Command5_Click()
x = Val(Text1.Text)
y = Cos(x * 3.14159 / 180) Text1.Text =
CStr(y)
End Sub
Running stage
33 | P a g e
ﺗﻤﺎرﯾﻦ
Q1 /ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﺤﺴﺎب ﻣﺴﺎﺣﺔ اﻟﻤﺜﻠﺚ ﻣﻊ اﻟﺘﺼﻤﯿﻢ.
Q2 /ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻹدﺧﺎل اﺳﻢ اﻟﻄﺎﻟﺐ وﻋﻼﻣﺘﻲ ﻓﻲ أي ﻣﻮﺿﻮع ﻣﻦ ﻗﺒﻞ ﻣﺮﺑﻊ اﻹدﺧﺎل ﺛﻢ ﯾﺤﺴﺐ اﻟﻤﺘﻮﺳﻂ ،وﻋﺮض اﺳﻢ واﻟﻤﺘﻮﺳﻂ ﻓﻲ اﺛﻨﯿﻦ ﻣﻦ
اﻟﺘﺴﻤﯿﺎت.
Q3 /اﻛﺘﺐ ﺑﺮﻧﺎﻣﺠﺎ ﻹدﺧﺎل رﻗﻢ ﺣﻘﯿﻘﻲ ﺛﻢ ﺗﺠﺪ ﻣﺮﺑﻊ ﻟﮭﺎ ﺑﻌﺪ وﺿﻊ ﺗﺼﻤﯿﻢ ﻣﻨﺎﺳﺐ.
Q4 /ﺗﺼﻤﯿﻢ ﻣﺸﺮوع ﻣﻊ ﺛﻼﺛﺔ أﺷﻜﺎل ،ﻣﺜﻞ أن form1ﯾﺤﺘﻮي ﻋﻠﻰ زري اﻷﻣﺮ "اﻟﻤﻌﺎدﻟﺔ "Pethagorsو "ﻣﻨﻄﻘﺔ اﻟﻤﺜﻠﺚ" ھﺬه اﻟﺘﻲ
ﻋﻨﺪﻣﺎ اﻧﻘﺮ ﻓﻮق " Command1ﻓﯿﺜﺎﻏﻮرس اﻟﻤﻌﺎدﻟﺔ" form2 ،ﺳﻮف ﺗﻈﮭﺮ ﺣﺘﻰ ﻓﻲ ھﺬا اﻟﺸﻜﻞ اﻟﻀﻠﻊ اﻟﺜﺎﻟﺚ ﻣﻦ اﻟﻤﺜﻠﺚ ﺳﯿﻜﻮن ﺣﺴﺎب ﻣﻊ
ﻓﯿﺜﺎﻏﻮرس اﻟﻤﻌﺎدﻟﺔ ﺑﻌﺪ دﺧﻮل اﻟﺠﺎﻧﺐ اﻷول واﻟﺜﺎﻧﻲ ،وﻋﻨﺪﻣﺎ ﺗﻀﻐﻂ ﻋﻠﻰ "ﻣﻨﻄﻘﺔ اﻟﻤﺜﻠﺚ" ﺷﻜﻞ ﺛﺎﻟﺚ ﻋﺮض ذﻟﻚ أن اﻟﻤﻨﻄﻘﺔ ﺳﻮف ﯾﻜﻮن ﺣﺴﺎب
ﺑﻌﺪ دﺧﻮل اﻟﻘﺎﻋﺪة واﻻرﺗﻔﺎع .اﺳﺘﺨﺪام اﻟﻤﺨﺮﺟﺎت واﻟﻤﺪﺧﻼت اﻟﻄﺮق اﻟﻤﻨﺎﺳﺒﺔ.
Q5 /ﻛﺘﺎﺑﺔ ﺑﺮﻧﺎﻣﺞ ﻟﻌﺮض اﻟﺮﺳﺎﻟﺔ أدﻧﺎه ﺑﻌﺪ اﻟﻨﻘﺮ ﻋﻠﻰ زر اﻷﻣﺮ "ﺗﺸﻐﯿﻞ".
34 | P a g e
References
- Balena, Francesco. Programming Microsoft Visual Basic. NET Version 2003. Antonio Faustino, 2004.
- Kurniadi, Adi. "Pemrograman Microsoft Visual Basic 6." Elex Media Komputindo, Jakarta (2000).
- Petroutsos, Evangelos. Mastering Database Programming with Visual Basic 6. SYBEX Inc., 1999.
35 | P a g e