|
-
Dec 13th, 2010, 04:52 AM
#1
Thread Starter
New Member
Converter!
Hi!
i'm trying to make a converter, i have 4 textboxes, 2 of them are encode, the other 2 are decoders.
on the encode process i use:
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar().Equals(Chr(13)) Then TextBox2.Text = TextBox2.Text + "I89" '[enter]
If e.KeyChar().Equals(Chr(33)) Then TextBox2.Text = TextBox2.Text + "G33" '!
If e.KeyChar().Equals(Chr(34)) Then TextBox2.Text = TextBox2.Text + "P78" '"
If e.KeyChar().Equals(Chr(35)) Then TextBox2.Text = TextBox2.Text + "G31" '#
If e.KeyChar().Equals(Chr(36)) Then TextBox2.Text = TextBox2.Text + "S93" '$
If e.KeyChar().Equals(Chr(37)) Then TextBox2.Text = TextBox2.Text + "O05" '%
If e.KeyChar().Equals(Chr(38)) Then TextBox2.Text = TextBox2.Text + "B34" '&
If e.KeyChar().Equals(Chr(39)) Then TextBox2.Text = TextBox2.Text + "F23" ''
If e.KeyChar().Equals(Chr(40)) Then TextBox2.Text = TextBox2.Text + "U93" '(
and so on.. I also use KeyDown, but thats not really the problem. The problem is the decoder!
I want it to be decoded when a button is pressed, because I cant use the KeyPress or KeyDown function because it will most likely be copy-pasted.
So how would i do that, for instant the char A Will be converted from "W11" to "A".
Now i know there is a function called Convert, but i have no idea on how it works or if it even is proper for this task.
Please help me, this program is only for me to lear more about programming.
Thanks!
-
Dec 13th, 2010, 07:11 AM
#2
Fanatic Member
Re: Converter!
The logic is not really clear to me, but it appears you have some sort of input string stored in a textbox(say tbxInput), and you want to convert the encoded symbol consisting of 3 characters, back to a single character and possibly build up a corresponding string in say tbxOutput.
The logic would be something like
Code:
dim s as string
' grab the right three characters from encoded string
s = tbxInput.text.substring(tbxInput.text.length -3)
select case s
case "W11"
tbxOutput.text = tbxOutput.text & "A"
end select
you would need to figure out which event to use.
-
Dec 13th, 2010, 08:07 AM
#3
Thread Starter
New Member
Re: Converter!
 Originally Posted by John_SC
The logic is not really clear to me, but it appears you have some sort of input string stored in a textbox(say tbxInput), and you want to convert the encoded symbol consisting of 3 characters, back to a single character and possibly build up a corresponding string in say tbxOutput.
The logic would be something like
Code:
dim s as string
' grab the right three characters from encoded string
s = tbxInput.text.substring(tbxInput.text.length -3)
select case s
case "W11"
tbxOutput.text = tbxOutput.text & "A"
end select
you would need to figure out which event to use.
Thank you, it could work, but will it display in the correct order?
-
Dec 13th, 2010, 08:13 AM
#4
Fanatic Member
Re: Converter!
I am not sure what you mean by 'the correct order'. What you are doing is replacing some text in a source string with some other text in the same location. The basic content of the source is not changed except for the targetted text replacements. Both approaches will accomplish this (if done properly).
-
Dec 13th, 2010, 08:30 AM
#5
Thread Starter
New Member
Re: Converter!
 Originally Posted by John_SC
I am not sure what you mean by 'the correct order'. What you are doing is replacing some text in a source string with some other text in the same location. The basic content of the source is not changed except for the targetted text replacements. Both approaches will accomplish this (if done properly).
Well, i want to encode and decode a text containig more than 1 char.
Your "select case" version of fixing it works fine untill we get more than 1 chars in the textboxin.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String
text = TextBox3.Text.Substring(TextBox3.Text.Length - 3)
Select Case text
Case "W11" 'A
TextBox4.Text = TextBox4.Text + "A"
Case "P32" 'B
TextBox4.Text = TextBox4.Text + "B"
Case "R23" 'C
TextBox4.Text = TextBox4.Text + "C"
Case "C44" 'D
TextBox4.Text = TextBox4.Text + "D"
Case "G65" 'E
TextBox4.Text = TextBox4.Text + "E"
Case "N56" 'F
TextBox4.Text = TextBox4.Text + "F"
End Select
End Sub
End Class
if textbox3 is "W11" then textbox4 becomes "A"
A = W11 B=P23
if textbox3 is "W11P23" then textbox4 becomes "B"
if textbox3 is "P23W11N56" then textbox4 becomes "F"
I really hope you understand my problem!
Last edited by ovenicolai; Dec 13th, 2010 at 08:41 AM.
-
Dec 13th, 2010, 08:49 AM
#6
Fanatic Member
Re: Converter!
The best way to debug this is set a break point, and then see what the variable values are as you step through the code. You see the values by either hovering over the value or opening up the Locals (or Auto) window from the Debug / Windows menu. This should help you debug your code.
Do not use "+" to connect strings. Use "&". This is likely not your problem, but it is good programming practice (you do not add text, you add numbers).
-
Dec 13th, 2010, 09:00 AM
#7
Thread Starter
New Member
Re: Converter!
 Originally Posted by John_SC
The best way to debug this is set a break point, and then see what the variable values are as you step through the code. You see the values by either hovering over the value or opening up the Locals (or Auto) window from the Debug / Windows menu. This should help you debug your code.
Do not use "+" to connect strings. Use "&". This is likely not your problem, but it is good programming practice (you do not add text, you add numbers).
Break point?
I don't understand
-
Dec 13th, 2010, 09:06 AM
#8
Fanatic Member
Re: Converter!
On the far left, next to the code but to the right of the Toolbox is a vertical gray bar. If you click the gray bar it will insert a red dot and color the line red. When you run the application, it will stop at this point. One line will be highlighted in Yellow. This line is the NEXT line to be executed. You then can step through the code. These are the three buttons next to the green run arrow on the menu bar.
When you are in break mode, go to Debug, Window and click Local. This will pull up the Local window and tell you the values of your variables.
YOU WILL LOVE Breakpoints. It makes coding a lot easier.
-
Dec 13th, 2010, 10:26 AM
#9
Re: Converter!
Here is an option, which condenses the logic and is easier to maintain.
Code:
Private MyContainer As New Dictionary(Of Integer, String)
Private Sub Form1_Load() Handles MyBase.Load
MyContainer.Add(13, "I89")
MyContainer.Add(33, "G33")
MyContainer.Add(34, "P78")
MyContainer.Add(35, "G31")
MyContainer.Add(36, "S93")
MyContainer.Add(37, "O05")
MyContainer.Add(38, "B34")
MyContainer.Add(39, "F23")
MyContainer.Add(40, "U93")
End Sub
Private Sub TextBox1_KeyPress1( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
Dim Value As Integer = Asc(e.KeyChar)
If Value.IsBackSpace Then
TextBox2.RemoveLastCode()
End If
If MyContainer.ContainsKey(Value) Then
TextBox2.Text &= MyContainer.Item(Value)
End If
End Sub
Place the following in a code module
Code:
Module Extensions
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Sub RemoveLastCode(ByVal sender As TextBox)
If sender.Text.Length >= 3 Then
sender.Text = sender.Text _
.Substring(0, sender.Text.Length - 3)
End If
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function IsBackSpace(ByVal sender As Integer) As Boolean
Return sender = 8
End Function
End Module
-
Dec 13th, 2010, 12:18 PM
#10
Thread Starter
New Member
Re: Converter!
 Originally Posted by kevininstructor
Here is an option, which condenses the logic and is easier to maintain.
Code:
Private MyContainer As New Dictionary(Of Integer, String)
Private Sub Form1_Load() Handles MyBase.Load
MyContainer.Add(13, "I89")
MyContainer.Add(33, "G33")
MyContainer.Add(34, "P78")
MyContainer.Add(35, "G31")
MyContainer.Add(36, "S93")
MyContainer.Add(37, "O05")
MyContainer.Add(38, "B34")
MyContainer.Add(39, "F23")
MyContainer.Add(40, "U93")
End Sub
Private Sub TextBox1_KeyPress1( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
Dim Value As Integer = Asc(e.KeyChar)
If Value.IsBackSpace Then
TextBox2.RemoveLastCode()
End If
If MyContainer.ContainsKey(Value) Then
TextBox2.Text &= MyContainer.Item(Value)
End If
End Sub
Place the following in a code module
Code:
Module Extensions
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Sub RemoveLastCode(ByVal sender As TextBox)
If sender.Text.Length >= 3 Then
sender.Text = sender.Text _
.Substring(0, sender.Text.Length - 3)
End If
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function IsBackSpace(ByVal sender As Integer) As Boolean
Return sender = 8
End Function
End Module
I don't really get the code (at all,) but how do i activate it? probably a stupid question..
-
Dec 13th, 2010, 12:38 PM
#11
Re: Converter!
 Originally Posted by ovenicolai
I don't really get the code (at all,) but how do i activate it?  probably a stupid question..
Simply type one of your characters into TextBox1 and the resulting character item is appended to TextBox2.
See attached demo
Last edited by kareninstructor; Aug 11th, 2011 at 08:01 AM.
-
Dec 13th, 2010, 02:58 PM
#12
Thread Starter
New Member
Re: Converter!
 Originally Posted by kevininstructor
Simply type one of your characters into TextBox1 and the resulting character item is appended to TextBox2.
See attached demo
I don't think you understood my problem, What I need is the Decoder, not the encoder:
my encode-script is like this:
Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.A Then TextBox2.Text = TextBox2.Text + "W11"
If e.KeyCode = Keys.B Then TextBox2.Text = TextBox2.Text + "P32"
If e.KeyCode = Keys.C Then TextBox2.Text = TextBox2.Text + "R23"
If e.KeyCode = Keys.D Then TextBox2.Text = TextBox2.Text + "C44"
If e.KeyCode = Keys.E Then TextBox2.Text = TextBox2.Text + "G65"
If e.KeyCode = Keys.F Then TextBox2.Text = TextBox2.Text + "N56"
If e.KeyCode = Keys.G Then TextBox2.Text = TextBox2.Text + "H97"
If e.KeyCode = Keys.H Then TextBox2.Text = TextBox2.Text + "D88"
If e.KeyCode = Keys.I Then TextBox2.Text = TextBox2.Text + "S09"
If e.KeyCode = Keys.J Then TextBox2.Text = TextBox2.Text + "Q20"
If e.KeyCode = Keys.K Then TextBox2.Text = TextBox2.Text + "K79"
If e.KeyCode = Keys.L Then TextBox2.Text = TextBox2.Text + "O98"
If e.KeyCode = Keys.M Then TextBox2.Text = TextBox2.Text + "A37"
If e.KeyCode = Keys.N Then TextBox2.Text = TextBox2.Text + "B56"
If e.KeyCode = Keys.O Then TextBox2.Text = TextBox2.Text + "Z45"
If e.KeyCode = Keys.P Then TextBox2.Text = TextBox2.Text + "E14"
If e.KeyCode = Keys.Q Then TextBox2.Text = TextBox2.Text + "X83"
If e.KeyCode = Keys.R Then TextBox2.Text = TextBox2.Text + "Y32"
If e.KeyCode = Keys.S Then TextBox2.Text = TextBox2.Text + "V01"
If e.KeyCode = Keys.T Then TextBox2.Text = TextBox2.Text + "U42"
If e.KeyCode = Keys.U Then TextBox2.Text = TextBox2.Text + "F53"
If e.KeyCode = Keys.V Then TextBox2.Text = TextBox2.Text + "T64"
If e.KeyCode = Keys.W Then TextBox2.Text = TextBox2.Text + "M85"
If e.KeyCode = Keys.X Then TextBox2.Text = TextBox2.Text + "L76"
If e.KeyCode = Keys.Y Then TextBox2.Text = TextBox2.Text + "J97"
If e.KeyCode = Keys.Z Then TextBox2.Text = TextBox2.Text + "I18"
If e.KeyCode = Keys.Space Then TextBox2.Text = TextBox2.Text + "W09"
If e.KeyCode = Keys.Back And Not TextBox2.Text = "" Then TextBox2.Text = TextBox2.Text.Substring(0, TextBox2.Text.Length - 3)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar().Equals(Chr(13)) Then TextBox2.Text = TextBox2.Text + "I89" '[enter]
If e.KeyChar().Equals(Chr(33)) Then TextBox2.Text = TextBox2.Text + "G33" '!
If e.KeyChar().Equals(Chr(34)) Then TextBox2.Text = TextBox2.Text + "P78" '"
If e.KeyChar().Equals(Chr(35)) Then TextBox2.Text = TextBox2.Text + "G31" '#
If e.KeyChar().Equals(Chr(36)) Then TextBox2.Text = TextBox2.Text + "S93" '$
If e.KeyChar().Equals(Chr(37)) Then TextBox2.Text = TextBox2.Text + "O05" '%
If e.KeyChar().Equals(Chr(38)) Then TextBox2.Text = TextBox2.Text + "B34" '&
If e.KeyChar().Equals(Chr(39)) Then TextBox2.Text = TextBox2.Text + "F23" ''
If e.KeyChar().Equals(Chr(40)) Then TextBox2.Text = TextBox2.Text + "U93" '(
If e.KeyChar().Equals(Chr(41)) Then TextBox2.Text = TextBox2.Text + "Q23" ')
If e.KeyChar().Equals(Chr(42)) Then TextBox2.Text = TextBox2.Text + "C43" '*
If e.KeyChar().Equals(Chr(43)) Then TextBox2.Text = TextBox2.Text + "G39" '+
If e.KeyChar().Equals(Chr(44)) Then TextBox2.Text = TextBox2.Text + "Y67" ',
If e.KeyChar().Equals(Chr(45)) Then TextBox2.Text = TextBox2.Text + "A34" '-
If e.KeyChar().Equals(Chr(46)) Then TextBox2.Text = TextBox2.Text + "I98" '.
If e.KeyChar().Equals(Chr(47)) Then TextBox2.Text = TextBox2.Text + "J88" '/
If e.KeyChar().Equals(Chr(48)) Then TextBox2.Text = TextBox2.Text + "K82" '0
If e.KeyChar().Equals(Chr(49)) Then TextBox2.Text = TextBox2.Text + "U13" '1
If e.KeyChar().Equals(Chr(50)) Then TextBox2.Text = TextBox2.Text + "H13" '2
If e.KeyChar().Equals(Chr(51)) Then TextBox2.Text = TextBox2.Text + "X12" '3
If e.KeyChar().Equals(Chr(52)) Then TextBox2.Text = TextBox2.Text + "L34" '4
If e.KeyChar().Equals(Chr(53)) Then TextBox2.Text = TextBox2.Text + "U31" '5
If e.KeyChar().Equals(Chr(54)) Then TextBox2.Text = TextBox2.Text + "P75" '6
If e.KeyChar().Equals(Chr(55)) Then TextBox2.Text = TextBox2.Text + "Z12" '7
If e.KeyChar().Equals(Chr(56)) Then TextBox2.Text = TextBox2.Text + "O11" '8
If e.KeyChar().Equals(Chr(57)) Then TextBox2.Text = TextBox2.Text + "V19" '9
If e.KeyChar().Equals(Chr(61)) Then TextBox2.Text = TextBox2.Text + "K21" '=
If e.KeyChar().Equals(Chr(95)) Then TextBox2.Text = TextBox2.Text + "I95" '_
If e.KeyChar().Equals(Chr(197)) Then TextBox2.Text = TextBox2.Text + "H78" 'Å
If e.KeyChar().Equals(Chr(198)) Then TextBox2.Text = TextBox2.Text + "S22" 'Æ
If e.KeyChar().Equals(Chr(230)) Then TextBox2.Text = TextBox2.Text + "S22" 'æ
If e.KeyChar().Equals(Chr(164)) Then TextBox2.Text = TextBox2.Text + "L09" '¤
If e.KeyChar().Equals(Chr(216)) Then TextBox2.Text = TextBox2.Text + "B78" 'Ø
If e.KeyChar().Equals(Chr(248)) Then TextBox2.Text = TextBox2.Text + "B78" 'ø
If e.KeyChar().Equals(Chr(229)) Then TextBox2.Text = TextBox2.Text + "H78" 'å
If e.KeyChar().Equals(Chr(197)) Then TextBox2.Text = TextBox2.Text + "H78" 'Å
End Sub
Thats not the problem, i need to pick out parts form a long string like:
"O98S09K79G65W09U42D88S09V01I98"
and then decode each part of it so that it looks like this:
"Like this."
Each new char begins with a letter and ends with 2 numbers.
this is how I'm going too have to split them, somehow.
do you think of a way to solve it, the DECODER.
-
Dec 13th, 2010, 03:30 PM
#13
Re: Converter!
 Originally Posted by ovenicolai
Thats not the problem, i need to pick out parts form a long string like:
"O98S09K79G65W09U42D88S09V01I98"
and then decode each part of it so that it looks like this:
"Like this."
Each new char begins with a letter and ends with 2 numbers.
this is how I'm going too have to split them, somehow.
do you think of a way to solve it, the DECODER.
Using your example you can use a for-next step by 3 to get each part of the large string.
Code:
Dim Values As String = "O98S09K79G65W09U42D88S09V01I98"
For x As Integer = 0 To Values.Length - 1 Step 3
Console.WriteLine(Values.Substring(x, 3))
Next
Output
Code:
O98
S09
K79
G65
W09
U42
D88
S09
V01
I98
-
Dec 14th, 2010, 09:49 AM
#14
Re: Converter!
I don't understand where the output will be screened, or even how this is commands work, i am still the learningproccess and hope you will lecture me in an easier way.
In regards to where does the output go using Console.WriteLine (in a Windows forms project), the output goes to the output window in Visual Studio’s IDE. From the IDE main menu select; View->Other Windows->Output. When the code that uses Console.WriteLine executes you would view the output of Console.WriteLine in the output window. With that said those that have two display monitors can place the application being debugged in one window and the IDE in another window so that you need not have to toggle between the two.
The alternative is to use controls on a form to work your logic. One possibility is shown below.
Code:
ListBox1.Items.Clear()
For x As Integer = 0 To TextBox1.Text.Length - 1 Step 3
ListBox1.Items.Add(TextBox1.Text.Substring(x, 3))
Next
The problem here is that if the code is being used without the UI (User Interface) than once the code is proven you need to remove the UI elements. It is a preference more than anything else to choose UI versus console feedback.
The reason I used Console.WriteLine for you is so you could easily add the code snippet into a project and use it without adding any controls.
Suggestion while learning is when not understanding something like Console.WriteLine is to open help, select the index tab and type in whatever you do not understand or from your web browser open say Google and type in whatever you do not understand from the Framework. For instance, type the following into Google search TextBox and click “Search”.
msdn console class site:msdn.microsoft.com/en-us/library
When you can not find information on something this is when to ask questions but simply asking questions you generally will a) get a direction to move in to create a solution to a problem b) one or more solutions. The issue here is that usually the quality of advice is very high on this forum which is good but many who get responses will simply use it and not learn from it. So back to my suggestion, while learning .NET explore the help system when a problem presents itself and then every day spend time exploring what .NET offers. One day you might explore working with Strings until you know them like the back of your hand then push yourself farther etc. Hopefully this is helpful information for you in your learning process.
Console class on MSDN
http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|