473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

My first program in VB

Parul Bagadia
188 100+
Hi people..... this is my first program in VB.
Before this i have done programming in C,C++.
I read some of the chapters from Black book of VB; but still having problem in the initial part itself.
The program is I have to take a number in any of the forms; Octal, binary or hexadecimal convert it in the form required by user and display the result.
Am done with the logic of program; but having problems..
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. Dim intext As String
  4. intext = Text1.Text
  5.  
  6. Dim outtext As String
  7. outtext = Text2.Text
  8. if (option1.Value=true) and (option4.Value=true)
  9. outtext = intext
  10. End If
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. End Sub
First of all am not understanding why it creates so many procedures; every time i click something.......
And its showing error in if statement dont know why..
Is it because of option? i deleted some sub procedures above it; before that also it was showing an error.
Am using Microsoft Visual Basic.
Please tell me fast.
Aug 25 '08 #1
36 2506
Parul Bagadia
188 100+
Sorry folks, i troubled u i got the mistake; i had forgotten to write THEN.
But u can still tell me what's the use of different procedures; which we get when we click any of the controls?
I mean are they private? or public? Are they excluded from other code?
What if we dont make them?
Aug 25 '08 #2
vdraceil
236 100+
Sorry folks, i troubled u i got the mistake; i had forgotten to write THEN.
But u can still tell me what's the use of different procedures; which we get when we click any of the controls?
I mean are they private? or public? Are they excluded from other code?
What if we dont make them?
In visual basic,when you just double-click a control,it automatically enters the code window creating the procedure for the basic event of the control you clicked..for example,if you double click command button,it creates a procedure Command1_Click,because click is the most usual event one will perform on a command button.if you want some other event you can choose in the code window.

This feature makes the task of developers easier.If you dont want to write anything inside the created procedure,just leave it or if you still feel annoying delete it.(Try not to double click any of the unnecessary controls in design time).

Since you are a beginner,you may feel this annoying,but once you get involved with visual basic,you'd really love this feature-it saves your time!

Remember visual basic does an event driven programming.
Aug 26 '08 #3
Parul Bagadia
188 100+
It's the same program; where i got to convert a number from one number system to another..... m a bit confused regarding scope of different variables.
Here is my program that i wrote in Microsoft Visual Basic 6.0; for now its having only 3 options and that too converting a number from one to same number system.. Not actually converting also.. but its not displaying anything in text2.
Here is the code.I dont know how will i copy the form of VB.

Public Sub Text1_Change()
Static inno As Integer
inno = Text1.Text
End Sub

Public Sub Text2_Change()
Static outno As Integer
outno = Text2.Text
End Sub

Public Sub Command1_Click()

If Option1.Value = True And Option4.Value = True Then
outno = inno
End If

If Option2.Value = True And Option5.Value = True Then
outno = inno
End If

If Option3.Value = True And Option6.Value = True Then
outno = inno
End If

'If Option1.Value = True And Option6.Value = True Then
'outno = inno
'End If
'Dim counter As Integer
'For counter = 0 To 9
End Sub
Sep 2 '08 #4
Parul Bagadia
188 100+
In visual basic,when you just double-click a control,it automatically enters the code window creating the procedure for the basic event of the control you clicked..for example,if you double click command button,it creates a procedure Command1_Click,because click is the most usual event one will perform on a command button.if you want some other event you can choose in the code window.

This feature makes the task of developers easier.If you dont want to write anything inside the created procedure,just leave it or if you still feel annoying delete it.(Try not to double click any of the unnecessary controls in design time).

Since you are a beginner,you may feel this annoying,but once you get involved with visual basic,you'd really love this feature-it saves your time!

Remember visual basic does an event driven programming.
Thanks.
But m not sure whether i understand the meaning of event driven programming.
Sep 2 '08 #5
jg007
283 100+

Here is the code.I dont know how will i copy the form of VB.

Public Sub Text1_Change()
Static inno As Integer
inno = Text1.Text
End Sub

Public Sub Text2_Change()
Static outno As Integer
outno = Text2.Text
End Sub

Public Sub Command1_Click()

If Option1.Value = True And Option4.Value = True Then
outno = inno
End If

End Sub
I have chopped your code a little for size but looking at it you do not seem to be calling the Text2_Change sub inorder to change the text so all the program does when you click on the command1 is set the value of outno

also you are declaring the integers whenever you call the text change subs which does not look correct

i think if i understand what you are trying to do you will need to have something like -

Public Sub Command1_Click()

If Option1.Value = True And Option4.Value = True Then
outno = inno
End If

Text2_Change()

End Sub
Sep 2 '08 #6
Parul Bagadia
188 100+
Please reply fast. I'm stuck with first program itself.
Sep 2 '08 #7
vdraceil
236 100+
Thanks.
But m not sure whether i understand the meaning of event driven programming.
vb reacts to every possible event occuring to any control in the form.if it is a command button it may be subjected to many possible events like click,double click,mouse move etc. And the users can make their own coding under any event they wish to monitor.
This kind of programming is event driven programming.
Sep 2 '08 #8
Parul Bagadia
188 100+
vb reacts to every possible event occuring to any control in the form.if it is a command button it may be subjected to many possible events like click,double click,mouse move etc. And the users can make their own coding under any event they wish to monitor.
This kind of programming is event driven programming.
thank you so much...
Can u plz tell me wats wrong with my code?
Sep 2 '08 #9
vdraceil
236 100+
Please reply fast. I'm stuck with first program itself.
to display something in textbox2 u must have text2.text=some value(i mean text2.text must be on the left side-assignment is from right to left)..
Why use static variables and declare it inside text box change event?instead you can declare variables generally(outside all events-so that the variables become public to the entire form)
Sep 2 '08 #10
Parul Bagadia
188 100+
I have chopped your code a little for size but looking at it you do not seem to be calling the Text2_Change sub inorder to change the text so all the program does when you click on the command1 is set the value of outno

also you are declaring the integers whenever you call the text change subs which does not look correct

i think if i understand what you are trying to do you will need to have something like -

Public Sub Command1_Click()

If Option1.Value = True And Option4.Value = True Then
outno = inno
End If
Text2_Change()

End Sub
Thanks budy.
May be because m new in VB, i didnt understand why do i hav to keep it blank the text2 event?
And wats wrong with integer?
I mean earlier i had used string and instead of text i had used text1.text but i was getting a error that i cannot assign it to an array... M not sure wether i got that.
Sep 2 '08 #11
Parul Bagadia
188 100+
to display something in textbox2 u must have text2.text=some value(i mean text2.text must be on the left side-assignment is from right to left)..
Why use static variables and declare it inside text box change event?instead you can declare variables generally(outside all events-so that the variables become public to the entire form)
But then that will be global if m not wrong... and its not a good programming practise to declare variables as global.... so m willing to avoid that habit from beginning itself......... otherwise later it causes problems.
Sep 2 '08 #12
Parul Bagadia
188 100+
to display something in textbox2 u must have text2.text=some value(i mean text2.text must be on the left side-assignment is from right to left)..
Why use static variables and declare it inside text box change event?instead you can declare variables generally(outside all events-so that the variables become public to the entire form)
I did text2.text=outno.. but still invain.
Sep 2 '08 #13
Parul Bagadia
188 100+
I also used debugger but all that m gettin in here is this:
For all the text boxes, options, variables no value is getting displayed other than the message: expression not defined in context for inno and for rest ; out of context..
Why is it so?
Sep 2 '08 #14
vdraceil
236 100+
I also used debugger but all that m gettin in here is this:
For all the text boxes, options, variables no value is getting displayed other than the message: expression not defined in context for inno and for rest ; out of context..
Why is it so?
Are you sure that you made the right calculations for converting numbers(you didnt show that in your code) and stored it in outno?
Better have a command button so that the user may click it after entering the number in the text box and selecting the conversion type option button. Finally include text2.text=outno in command button's click event.

Okay..just give me some time and i'll design the proj and attach it.
Sep 2 '08 #15
Parul Bagadia
188 100+
Are you sure that you made the right calculations for converting numbers(you didnt show that in your code) and stored it in outno?
Better have a command button so that the user may click it after entering the number in the text box and selecting the conversion type option button. Finally include text2.text=outno in command button's click event.

Okay..just give me some time and i'll design the proj and attach it.
I have done the same things as u said........ given command button for conversion; option buttons for selecting a no. system and has also provided text box for entering data.
I dont know how to display that here.
There doesnt come any option to copy the entire form...
If u tell me how to do that i can display the form design over here.
Sep 2 '08 #16
Parul Bagadia
188 100+
can anybody plz tell me how do i attach my visual basic form over here?
So that u can understand the form design and whatever is goin wrong.
Sep 2 '08 #17
jg007
283 100+
Thanks budy.
May be because m new in VB, i didnt understand why do i hav to keep it blank the text2 event?
And wats wrong with integer?
I mean earlier i had used string and instead of text i had used text1.text but i was getting a error that i cannot assign it to an array... M not sure wether i got that.
Sorry, when I had used text2_change() that was as a call to the subroutine as I had slightly miss read the code but it was not very clear, please can you advise

1. how the form is set up
2. what you want to happen when the command button is clicked , including how the display changes


I am really unclear what you are trying to achieve with this form but I have posted an example of some cut down code below that might help , again apologies if this is completely different to what you are trying to do!!

also please note that I have not included any error checking for this so if you have this code and enter a string instead of a number into the box it will throw an exception also I don't have VB as I use vb.net so the subroutines may look a little different

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class Form1
  3.  
  4. ' declare two integers used to hold the in number and out number 
  5.  
  6.     Dim inno As Integer
  7.     Dim outno As Integer
  8.  
  9. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.  
  11. ' when the button is clicked and the IF statement matches change outno to a value of inno x2 
  12.  
  13.         inno = Text1.Text
  14.  
  15.         If "A" = "A" And "B" = "B" Then
  16.             outno = inno * 2
  17.  
  18. ' change text2.text to show outno
  19.  
  20.             Text2.Text = outno
  21.         End If
  22.  
  23.     End Sub
  24.  
  25. End Class
Sep 2 '08 #18
jg007
283 100+
sorry, my programming habits are probably not the best , I have just noticed the comment you made about the variables

I am not really fully sure how it works but possibly it may do what you want if you use -

Expand|Select|Wrap|Line Numbers
  1.  
  2.    Private inno As Integer
  3.    Private outno As Integer
  4.  
  5.  
Sep 2 '08 #19
Parul Bagadia
188 100+
Thanks a lot..
In my program two text boxes are there; one to insert the number and other to display the result. One command button; which when clicked will perform actual conversion. And two frames to chose the number to be converted is in which number system as well the number to be obtained should be in which no. system with in all 6 option buttons..
My code is now working for same system changes; where earlier it was not.
What i mean by same system changes is........Base of input as well as output is the same may be octal/binary/hexadecimal.
But now m getting some error while converting a no. from hexadecimal to binary.....
The code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Public outno As String
  2. Public inno As String
  3. Public Sub Text1_Change()
  4. inno = Text1.Text
  5. End Sub
  6.  
  7.  Public Sub Command1_Click()
  8.  
  9.  If Option1.Value = True And Option4.Value = True Then
  10.  outno = inno
  11.  End If
  12.  
  13.  If Option2.Value = True And Option5.Value = True Then
  14.  outno = inno
  15.  End If
  16.  
  17.  If Option3.Value = True And Option6.Value = True Then
  18.  outno = inno
  19.  End If
  20.  
  21.  If Option1.Value = True And Option6.Value = True Then
  22.  outno = hextobinary()
  23.  End If
  24.  
  25.  Text2.Text = outno
  26. End Sub
  27.  
  28. Public Sub Text2_Change()
  29.  Text2.Text = outno
  30. End Sub
  31. Function hextobinary()
  32. Dim i As Integer
  33. Dim store As Long
  34. Dim reminder As String
  35. Dim quot As Integer
  36. Dim temp As String
  37. For i = 0 To 9
  38.  If (inno(i) >= 0 And inno(i) <= 9) Then
  39.   store = store + inno(i) * pow(16, i)
  40.  
  41.   ElseIf (inno(i) = a) Then
  42.   inno(i) = 10
  43.   store = store + inno(i) * pow(16, i)
  44.   ElseIf (inno(i) = b) Then
  45.   inno(i) = 11
  46.   store = store + inno(i) * pow(16, i)
  47.   ElseIf (inno(i) = c) Then
  48.   inno(i) = 12
  49.   store = store + inno(i) * pow(16, i)
  50.   ElseIf (inno(i) = d) Then
  51.   inno(i) = 13
  52.   store = store + inno(i) * pow(16, i)
  53.   ElseIf (inno(i) = d) Then
  54.   inno(i) = 14
  55.   store = store + inno(i) * pow(16, i)
  56.   ElseIf (inno(i) = f) Then
  57.   inno(i) = 15
  58.   store = store + inno(i) * pow(16, i)
  59.  End If
  60.  Next i
  61.  
  62. If store! = 0 Then
  63. i = 0
  64.  Do
  65.   reminder(i) = store Mod 2
  66.   quot = store / 2
  67.   i = i + 1
  68.  While quot! = 0
  69. For j = 9 To 0
  70. outno(i) = reminder(j)
  71. i = i + 1
  72. j = j - 1
  73. Exit For
  74. Return
  75. End Function
its showing some error as an array is expected.....
I changed the data type of inno with array but there is no such data-type....
Sep 2 '08 #20
Parul Bagadia
188 100+
I understood, the error is because i have taken input in a number and accessing it as array in for loop.
But then can somebody tell me how do i get my number in an array?.... there is no such data-type.. or is there?
Sep 3 '08 #21
Parul Bagadia
188 100+
In case of the function oct..it converts an expression to octal system number...can we have another number in some number system which can get converted in octal...
Sep 3 '08 #22
jg007
283 100+
you seem to be accessing inno as an array ie: array(no of item)

if you are trying to select as specific character within the string / integer you will be better using the MID command I think

lol please bear in mind that there may be better ways of doing this and I have not tried to work out the conversion but here is some code to split the string
Expand|Select|Wrap|Line Numbers
  1.  
  2. For n = 1 To inno.Length
  3.  
  4.       value = (Mid(inno, n, 1))
  5.  
  6. ' whatever it is that you wish to do with this value
  7.  
  8. Next
  9.  
  10.  
to split it to an array -

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim value(inno.length) as string
  3.  
  4. For n = 1 To inno.Length
  5.  
  6.       value(n) = (Mid(inno, n, 1))
  7.  
  8. Next
  9.  
  10.  
Sep 3 '08 #23
vdraceil
236 100+
i have designed a simple application which helps in base conversion..
but the problem is i dont know how to attach it here..
anyone knows how??
Sep 8 '08 #24
Parul Bagadia
188 100+
i have designed a simple application which helps in base conversion..
but the problem is i dont know how to attach it here..
anyone knows how??
Even i have designed it. And even i dont know how ill post that here.......
Moreover i never asked for design..... M just gettin stuck somewhere in my code..
Sep 8 '08 #25
vdraceil
236 100+
Even i have designed it. And even i dont know how ill post that here.......
Moreover i never asked for design..... M just gettin stuck somewhere in my code..
As this is your first program in vb(and you have some difficulties with the coding),i thought you might need an example.. Do you need it? If yes i'll have to figure out a way..
Sep 8 '08 #26
Parul Bagadia
188 100+
you seem to be accessing inno as an array ie: array(no of item)

if you are trying to select as specific character within the string / integer you will be better using the MID command I think

lol please bear in mind that there may be better ways of doing this and I have not tried to work out the conversion but here is some code to split the string
Expand|Select|Wrap|Line Numbers
  1.  
  2. For n = 1 To inno.Length
  3.  
  4.       value = (Mid(inno, n, 1))
  5.  
  6. ' whatever it is that you wish to do with this value
  7.  
  8. Next
  9.  
  10.  
to split it to an array -

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim value(inno.length) as string
  3.  
  4. For n = 1 To inno.Length
  5.  
  6.       value(n) = (Mid(inno, n, 1))
  7.  
  8. Next
  9.  
  10.  
Am unable to do inno.length..
The compiler is showing error.
Is it the wrong way or sth else..
Sep 8 '08 #27
Parul Bagadia
188 100+
Is pow function not there in VB? what do we do for exponential?
Sep 9 '08 #28
Parul Bagadia
188 100+
I got how to do exponential thing........its just by using[b] '^['/B].
But now can someone tell me how we use not equal to sign in VB?
Sep 9 '08 #29
Parul Bagadia
188 100+
I got how to do exponential thing........its just by using[b] '^['/B].
But now can someone tell me how we use not equal to sign in VB?
I got that to by writting the expression in'( )' and using <> sign; but now m not getting equal to sign.
Sep 9 '08 #30
vdraceil
236 100+
I got that to by writting the expression in'( )' and using <> sign; but now m not getting equal to sign.
use NOT keyword..for example, 'if not a=b and not a=c then ...'
Sep 9 '08 #31
jg007
283 100+
Am unable to do inno.length..
The compiler is showing error.
Is it the wrong way or sth else..
what error message are you getting though ????
Sep 9 '08 #32
Parul Bagadia
188 100+
what error message are you getting though ????
I got an error as invalid qualifier. For that i used
Expand|Select|Wrap|Line Numbers
  1. len(inno)
; and got no error.
Sep 9 '08 #33
Parul Bagadia
188 100+
Finally i have reached till converting my no. in desired base; but now the problem is my output is stored in an array of string and i dont know how will i print it in VB.
I used print statement; nothing happened, compiler didnt even show the error message, but nothing happened as well.
I want the output in text2.text(a text box), so i directly used a for loop.
Right now my desired number is in reminder....
But still its not working, data type of both text2.text and reminder which is an array is string.
Can someone spot what should be the changes?
Expand|Select|Wrap|Line Numbers
  1. k = j - 1
  2. For i = 0 To j - 1
  3.   Text2.Text = reminder(k)
  4.   k = k - 1
  5. Next i
Sep 10 '08 #34
Parul Bagadia
188 100+
I used Debug.Print as well; but its printing the number in debugger.
This is quite important to me.
Someone if knows tell me how to print an array in text box
Sep 10 '08 #35
Parul Bagadia
188 100+
I even tried to convert an array in string using Join but; it was giving random answers...... I need to know how do we display an array in Textbox.. Can someone tell me?
Sep 10 '08 #36
juunas
1
I have something to say about this code:
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To 9
  2. If (inno(i) >= 0 And inno(i) <= 9) Then
  3.  
This was really weird, because you can't input anything else than a 10-digit number. I guess you need an array.

An array is created like this:

Dim numbers(0 To 9) As Integer

EDIT: Oh, and to answer your question, use something like this:
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To 9
  2. Text2.Text = Text2.Text & Array(i)
  3. Next
  4.  
Hope this helps you.
Nov 27 '08 #37

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
30
by: Pieter Provoost | last post by:
Hi, I just made my first c++ program, and I would like to know if there are things I can do better. Also, the exe is about 500 kb, can I make it smaller? Thanks! Pieter
27
by: hokiegal99 | last post by:
Hi Guys, This is my first C program. I started programming in Python. Please look over this source and let me know if I'm doing anything wrong. I want to start out right with C, so if you see...
13
by: Todd | last post by:
I am curious of the origins of the "Hello World" program. Who was the first to make this popular? That is, where did it start? I did some Google search and did not find an answer. Was it...
5
by: cj | last post by:
Thanks to everyone that has helped me. Now I'm trying to write my first program. I have an example of one that I need to write about. Any help getting me started is appreciated. I'm having trouble...
26
by: mwt | last post by:
Hello. Today I wrote my first program in C. It adds up the elements in an array. I am just beginning to learn this language. Any tips or pointers about better ways to write/structure/format/etc....
16
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to...
7
by: Chris Lasher | last post by:
Hi all, I have a simple script: --- #!/usr/bin/env python a = 1 b = 2
3
by: cs | last post by:
Hi, I'm new to C and would appreciate any feedback on the following program, asplit, which splits a file into 2 new files, putting a certain number of lines in the first file, and all the rest...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.