I'm trying to make something along the lines of the Branah Unicode Converter to decode lines like this for instance: [99,97,108,108,32,99]. http://www.branah.com/unicode-converter. I found this. I'm guessing the form has a textbox and 2 buttons. One is using Asc to get the textbox's ASCII code and the other using Chr to convert the ASCII code in the textbox into a character.
So, last time I posted this, it got closed because it wasn't "a real question". Let me be blunt. I haven NO idea how to get this working. Labeling of things is very confusing. What do I declare Text1, Text2, Text3 and Label 6 as because it's giving me the error that they're not declared? I'm also getting an error that TextBox1 cannot be converted to an integer from this line: Text2 = Text2 + Chr(TextBox1). How do I fix that? Am I not importing something that I should be?
Private Sub EncodeButton_Click()
'Code the character
TextBox1 = Asc(Text1)
'clears text box
Text1 = ""
' adds the coded character to the textbox (Textbox1)
LABEL6 = LABEL6 + "," + TextBox1
' set focus to textbox
Text1.SetFocus
'decodes the coded character
Text2 = Text2 + Chr(TextBox1)
End Sub
Private Sub DecodeButton_Click()
'this part decodes the coded number
TextBox1 = Text3
Text3 = ""
LABEL6 = LABEL6 + "," + TextBox1
Text2 = Text2 + Chr(TextBox1)
Text3.SetFocus
End Sub
Here's how to make this code work in VB.NET:
Create a new Windows Forms project.
In the project there is a form, open it and create the following items:
Three TextBoxes, named Text1, Text2, Text3
Two Buttons, named EncodeButton and DecodeButton
A Label named Label6
Right click the form in solution explorer and select "View Code".
Paste the following code into the code window:
Public Class Form1
Private Sub EncodeButton_Click(sender As System.Object, e As System.EventArgs) Handles EncodeButton.Click
Dim sTextBox1 As String
sTextBox1 = Asc(Text1.Text)
Text1.Text = ""
Label6.Text = Label6.Text & "," & sTextBox1
Text1.Focus()
Text2.Text = Text2.Text + Chr(sTextBox1)
End Sub
Private Sub DecodeButton_Click(sender As System.Object, e As System.EventArgs) Handles DecodeButton.Click
Dim sTextBox1 As Integer
sTextBox1 = Val(Text3.Text)
Text3.Text = ""
Label6.Text = Label6.Text & "," & sTextBox1
Text2.Text = Text2.Text & Chr(sTextBox1)
Text3.Focus()
End Sub
End Class
Run the project.
Type a single letter into Text1 and click EncodeButton
Type a number into Text3 (try one of the numbers that appeared in Label6 first) and click DecodeButton
This should accomplish everything the sample you pasted was designed to accomplish. If you have difficulty with any of these steps, I suggest looking up a good Windows Forms tutorial online.
Part of your problem is probably that you need to understand that controls like TextBoxes have properties and methods.
So, to GET the value from a TextBox called Text1 and SET it to a TextBox called TextBox1 you'd do this:
TextBox1.Text = Asc(Text1.Text)
If you type in the name of a control and hit the fullstop (".") character afterwards, the intellisense will give you a very helpful list of propertyies and methods you can use.
If you've copied this code from somewhere else, you may also need to create a Form, and drag/drop some TextBox controls onto it and give then names to match your code.
Related
I'm writing a simple Windows Form app in VB using VS Community 2017.
I have 64 checkboxes with 64 associated labels, named chk1 / lbl1 up to chk64 / lbl64. When a checkbox is checked, I want to extract a character from a string and show the answer in the label: e.g. if chk12 is checked, I want lbl12 to be enabled and the text to display the 12th character of the string.
To save writing 64 separate handlers I'm trying to do it in one. I can extract the checked number (e.g. 12) OK and write it to a string, but when I try to manipulate the label control I get an 'Object reference not set to an instance of an object' error.
The code I've come up with so far (largely from searching in here) is:
Private Sub CheckedChanged(sender As Object, e As EventArgs) _
Handles chk1.CheckedChanged, chk2.CheckedChanged 'etc. to 64
' wanted behaviour
'If chk1.Checked Then
' lbl1.Enabled = True
' lbl1.Text = GetChar(userString, 1)
'End If
'If chk2Checked Then
' lbl2.Enabled = True
' lbl2.Text = GetChar(userString, 2)
'End If
' etc. (to 64)
Dim resultsLabel As String
Dim userCheckedBox As Integer
userCheckedBox = CInt(DirectCast(sender, CheckBox).Text)
resultsLabel = "lbl" & DirectCast(sender, CheckBox).Text
Me.Controls(resultsLabel).Enabled = True
Me.Controls(resultsLabel).Text = GetChar(userString, userCheckedBox)
End Sub
I'd be very grateful if someone can nudge me over the line with this. Many thanks!
There is the old trick to use the Tag property of your checkboxes.
You can set the Tag property to the matching label name at design time using the WinForms Designer. So in the chk1.Tag property you will have the "lbl1" string assigned and so on for all the 64 checkboxes.
At this point your code in the event handler is simply
Dim chk = DirectCast(sender, CheckBox)
if chk IsNot Nothing Then
Me.Controls(chk.Tag.ToString()).Enabled = True
Me.Controls(chk.Tag.ToString()).Text = GetChar(userString, CInt(chk.Text))
End If
I have create a simple function to append text to a Richtextbox. I want that this text is append always on top scrolling the old text to the bottom.
Private Sub BoxMessaggio(ByVal testo As String, ByVal errore As Integer)
Me.ActiveControl = RichTextBox1
RichTextBox1.Focus()
If errore Then
RichTextBox1.SelectionColor = Color.Red
Else
RichTextBox1.SelectionColor = Color.Black
End If
RichTextBox1.AppendText(testo + vbNewLine)
RichTextBox1.SelectionStart = RichTextBox1.Text.Length
'RichTextBox1.Select(RichTextBox1.TextLength, 0)
RichTextBox1.ScrollToCaret()
End Sub
I call the function in this way:
BoxMessaggio(Now + ": " + ex.Message, 1)
I tried a lot of different solutions found here on StackOverflow or in some forums but no one works for me, text is always add at the bottom....
Treat the RichTextBox1.Text as a String (because it is), call Insert, and place the new text at the front of the strong.
RichTextBox1.Text = RichTextBox1.Text.Insert(0, testo + vbNewLine)
As for the scrolling. Don't call Focus or ScrollToCaret and it will stay at the top viewing the most recently added text.
I am Making a chat for my Game launcher, when you launch it up theres a button that Opens a Chat Forum with a richtextbox on it that it refreshes/timer checks when the text has changed in the txt file on the server has changed from textbox1 then it will reprint the text but thats not the problem, i have an admin panel for it, but i want to highlight lines where there is "!ADMINISTRATOR!" in it so it shows the user is an admin and it highlighted to show what he is saying. i've tried this
Dim index As Integer = Me.RichTextBox1.Find("!ADMINISTRATOR!")
If index <> -1 Then
Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
If last = -1 Then last = Me.RichTextBox1.TextLength
Me.RichTextBox1.Select(first, last - first)
Me.RichTextBox1.SelectionBackColor = Color.Yellow
End If
But that works, in a way, it does not always highlight it, while it will highlight sometimes, it also may highlight the text a just just put, but that i don't mind, the main problem is that it only highlights the First line with the text in it, so if the admin posts a message on it it will highlight <3 but then a user will chat and then admin posts another message, it only highlights the first line that contains that text. if you need any more info oh and for testing purposes im using a local file on my pc, ive tested the ftp it works, but just to save time im using a txt file this is the code i use for the timer
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If (System.IO.File.ReadAllText(Get_Directory_Current() & "\FilesDB\Chat\Chat.txt") = RichTextBox1.Text) Then
Else
Try
RichTextBox1.Text = System.IO.File.ReadAllText(Get_Directory_Current() & "\FilesDB\Chat\Chat.txt")
RichTextBox1.SelectionStart = RichTextBox1.Text.Length
RichTextBox1.ScrollToCaret()
ColorChat() ' this is the color chat that kinda dont work
Catch ex As Exception
End Try
End If
End Sub
if you can help thanks :) but for now i'm just trying new things. i guess it would kinda work a bit like syntax highlighting if you make it highlight a line instead of word.
Oh and if you wanted to know this is how i add the text to the chat / formatting
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
RichTextBox1.AppendText("<[" & Get_Time() & " " & strDate & "] " & "!ADMINISTRATOR!" & " | " & Environment.UserName & "> " & TextBox3.Text & vbNewLine)
End Sub
Use String.Replace() to inject the Rtf formatting where your target string appears.
Dim stringToFind = "!ADMINISTRATOR!"
Dim txt = Me.RichTextBox1.Text
Dim sb = New System.Text.StringBuilder()
sb.Append("{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}")
sb.Append(txt.Replace(stringToFind, String.Format("\cf2{0}\cf1", stringToFind)))
sb.Append("}")
Me.RichTextBox1.Rtf = sb.ToString()
I am not knowing about how to delete textboxes on the click of a button in my Windows form.
Here is my story:
Initially, I wanted to add textboxes on the click of a button, and upon searching the net, I was able to find out, on this forum, how to do this. https://stackoverflow.com/questions/15461978/adding-new-textbox-with-button-click
I used the code that user "Rajaprabhu Aravindasam" (2nd answer) gave. Here is only part of my code that I used (in order not to confuse you):
Private Sub Button_AddTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AddTask.Click
count += 1
TabPage_TaskStructure.Controls.Add(New Label() With _
{.Name = "Label_Task" & count})
TabPage_TaskStructure.Controls.Add(New RichTextBox() With _
{.Name = "RichTextBox" & count})
End Sub
Now, as you can see, the purpose of the button 'Button_AddTask" ('+' button on my form) is to create rich textboxes and their respective label. Assume that the rich textboxes and the labels are being created below one another.
Beside the '+' button, there is a '-' button. What I want is to use this '-' button to delete all created textboxes sequentially up. That is, if I have created 4 textboxes with the button '+', textbox no.4 will be deleted first when I click the '-' button, then no. 3 after a second click, then no.2 after a third click and so on.
The sequential part is not a problem, I know perfectly how to do it. Here is part of the code I tried:
Private Sub Button_DeleteTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DeleteTask.Click
TabPage_TaskStructure.Controls.Remove(New Label() With _
{.Name = "Label_Task" & count})
TabPage_TaskStructure.Controls.Remove(New RichTextBox() With _
{.Name = "RichTextBox" & count})
count -= 1
If count = 1 Then
Button_DeleteTask.Visible = False
End If
End Sub
What I did, logically, was simply replace 'Add' with 'Remove', but I am not knowing with what to replace 'New'. And so my question is: What needs to be used instead of 'New'? If I need to use an entirely different code, please do tell me.
Any help is greatly appreciated.
Update:
Ok, I have done some research since I've asked this question and I've been able to deduce that the removal of the controls (Label & Rich Text Box) can be done by using a For Each loop. In my code for the '-' button, I have replaced the first 4 lines of code with this:
Dim Ctrl As Control
For Each Ctrl In TabPage_TaskStructure.Controls
If TypeOf Ctrl Is Label And Ctrl.Name = "Label_Task" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
ElseIf TypeOf Ctrl Is RichTextBox And Ctrl.Name = "RichTextBox" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
Next
And so, when I click the '-' button, the program checks whether each control on the tab page (TabPage_TaskStructure) is a label with the name ("Label_Task" & count') or a rich text box with the name ("RichTextBox" & count), and if they are, they will be removed. The rest of the code is the same.
It's working, however not completely. It's working for the labels as they are successfully being removed, but not for the rich text boxes. I cannot understand why. I have tried the code for other controls such as date time pickers and text boxes, and it's not working for any of these either. It seems to be working for labels only.
Also, I have tried using the code on a default rich text box, and it worked! But for rich text boxes being created at run time, it's not working.
Can anyone clarify me on this?
I'm not really good at vb but from simple logic if and when an "IF" argument is accepted the code will move to "End If" and skip "Else" altoghter.
Try this instead:
Dim Ctrl As Control
For Each Ctrl In TabPage_TaskStructure.Controls
If TypeOf Ctrl Is Label And Ctrl.Name = "Label_Task" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
If TypeOf Ctrl Is RichTextBox And Ctrl.Name = "RichTextBox" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
Next
I'm trying to make a TextBox with syntax highlighting (for (HTML/CSS) in VB.NET 2008.
I figured that if I use RichTextBox.Find(), I can color specific text, but then I need to call RichTextBox.DeselectAll().
The problem is that the the cursor jumps to the beginning of the RTB.
I'm using WinForms.
Any ideas?
You can get and set the cursor position using the SelectionStart property.
Therefore, you can write,
Dim selStart As Integer = rtb.SelectionStart
'Do things
rtb.SelectionStart = selStart
Imports System.Text.RegularExpressions
Public Class Form1
'Create a Html Keyword Regex
Dim htmlkeywords As New System.Text.RegularExpressions.Regex("<html>|</html>|<head>|</head>|<meta|<p>|</p>|<div>|</div>") <----add as many terms as you like between the () don't forget the pipe symbol between each term in the Regex.
'Then in your Richtextbox textchanged event add this
Private Sub rtText_TextChanged(sender As Object, e As EventArgs) Handles rtText.TextChanged
Dim selStart As Integer = rtText.SelectionStart
Do Until False
For Each keyWordMatch As Match In htmlkeywords.Matches(rtText.Text)
rtText.Select(keyWordMatch.Index, keyWordMatch.Length)
rtText.SelectionColor = Color.Purple
rtText.SelectionStart = rtText.Text.Length 'this puts the caret at the end
rtText.SelectionLength = 0 ' of the word
Next keyWordMatch
Exit Do
Loop
rtText.SelectionColor = Color.Black
rtText.SelectionStart = selStart ' this makes sure that if your caret is behind a word and you press enter to move a text down a line; the caret will stay in position on the next line that you start typing on. You can remove this code to see what I'm talking about
End Sub
rtText is my RichTextBox name. This will change the word you want to whatever color then change it back to black, which you can change which colors do what. Hope this helps!