I would like to load string from a text file as a variable. For example a insecure login screen which the user enters their credentials into a textbox which the program will read what has been entered into the textbox and will load the next form when the credentials are the same stored in the text file.
Update:Solved it over 1 and half hours of googling
Public Class Form1
Public pass As String = String.Empty
Public user As String = String.Empty
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox2.Text = (user) And TextBox1.Text = (pass) Then
Login.Show()
Me.Close()
Else
MessageBox.Show("Wrong Password or User Name")
End If
TextBox2.Text = ""
TextBox1.Text = ""
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text = "" And TextBox1.Text = "" Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pass = IO.File.ReadAllText("pass.txt")
user = IO.File.ReadAllText("user.txt")
End Sub
End Class
just used IO.File.ReadAllText("user.txt") Which will read all the string in the text file and store them into the empty variable not the best method but works for this application
Related
I'm trying to connect to a database (mdb file of my choice) in a login screen and i want to save it for faster logon next times i boot the software.
I click on choose database button, opendialog lets me choose the file, i click OK and the db location shows in a textbox.
there's a checkbox beneath to save it before i connect to it.
But i can't manage to keep the checkbox checked, nor the textbox filled after i restart te program.
here's my current code:
Public Class LoginScreen
Private Sub Loginscreen_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBar2.Minimum = 0
ProgressBar2.Maximum = 100
ProgressBar2.Visible = False
Panel1.Visible = False
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub btnExit2_Click(sender As Object, e As EventArgs) Handles btnExit2.Click
Application.Exit()
End Sub
Private Sub tmrLogin_Tick(sender As Object, e As EventArgs) Handles tmrLogin.Tick
ProgressBar2.Value = ProgressBar2.Value + 20
lblLoginMessages.Text = ProgressBar2.Value & "%" & " Completed"
If ProgressBar2.Value >= 100 Then
tmrLogin.Enabled = False
If txtUser.Text = "azert" And txtPassword.Text = "azert" Then
ProgressBar2.Value = 0
Else
lblLoginMessages.Text = "Wrong credentials, Try again!"
pboxClosed.Visible = True
PboxOpen.Visible = False
ProgressBar2.Value = 0
txtPassword.Text = ""
txtUser.Text = ""
End If
End If
End Sub
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
ProgressBar2.Visible = True
tmrLogin.Enabled = True
pboxClosed.Visible = False
PboxOpen.Visible = True
''navraag doen om dit correct in te stellen! ! ! ! !!
'If ProgressBar2.Value = 100 Then
'lblLoginMessages.Text = "Logging in..."
Me.Hide()
Mainscreen.Show()
'End If
If chkSavePassword.Checked = True Then
My.Settings.databaselocation = txtDatabaselocationshow.Text
My.Settings.SaveLocation = True
End If
End Sub
Private Sub btnDBConnect_Click(sender As Object, e As EventArgs) Handles btnDBConnect.Click
If Panel1.Visible = False Then
Panel1.Visible = True
Else
Application.Exit()
End If
End Sub
Private Sub btnChoose_Click(sender As Object, e As EventArgs) Handles btnChoose.Click
Dim strtext As String
OpenFileDialog1.Filter = "Database Files | *.mdb"
OpenFileDialog1.InitialDirectory = "F:\GoogleDrive\EINDWERK VBNET"
OpenFileDialog1.Title = "Choose your Database"
OpenFileDialog1.ShowDialog()
strtext = OpenFileDialog1.FileName
txtDatabaselocationshow.Text = strtext
'If OpenFileDialog1.ShowDialog = DialogResult.OK Then
' strtext = OpenFileDialog1.FileName
' txtDatabaselocationshow.Text = strtext
'Else
' MsgBox("Error: the database file could not be read, try again.")
'End If
End Sub
Private Sub tmrshowloginpanel_Tick(sender As Object, e As EventArgs) Handles tmrshowloginpanel.Tick
Panel1.Width += 5
If Panel1.Width >= 700 Then
tmrshowloginpanel.Stop()
End If
End Sub
End Class
i've scoured the net but can't really find what to do?
if you need more information, shoot!
Walk through this with me:
Make a new project, one form, add a textbox and a combobox.
Click the textbox:
In the properties grid at the top click (Application Settings), then the 3 dots next to Property Binding. Scroll to Text. Drop down the setting and choose New at the bottom.
Give it a name of ChosenDatabasePath or something useful and descriptive like that
Repeat from "Click the textbox" for the checkbox instead, and this time bind the Checked property not the Text property.. Call the checkbox's Checked binding SavePassword or similar
Close all the dialogs so you're back at the form
Click anywhere on the background of the form when switch to Events in the property grid, find FormClosing and double click it
Put My.Settings.Save() in the FormClosing event handler
Run the app, write something in the textbox, close the form (by the X, not by stopping debugging), then immediately open the app again (run it)
I have orders in text files in the debug folder and when i type the name of the order in a text box it displays the order is a list box and there is a combo box underneath where i can change the status of the meal preparation (Being prepared, ready to deliver etc,). If i go back to that form and type in the same order name into the textbox i need to previous prepartion status to be already in the textbox. Thanks for any help!
Public Class frmOrderStatus
Private Sub btnStatus_Click(sender As Object, e As EventArgs) Handles btnStatus.Click
Dim sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
lstOrder.Items.Add(sr.ReadLine)
Loop
End Sub
Private Sub OrderStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstOrder.Items.Clear()
btnStatus.Enabled = False
ChangeStatus.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
strTxtOrderNum = txtOrderNum2.Text
btnStatus.Enabled = True
ChangeStatus.Enabled = True
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
strSaveStatus = ChangeStatus.SelectedIndex
End Sub
Private Sub ChangeStatus_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ChangeStatus.SelectedIndexChanged
End Sub
End Class
It recognizes the file; it just tells you it is in use. A Stream must be closed and disposed. I don't see the StreamReader even being closed let alone disposed. A `Using...End Using block will close and dispose of objects even if there is an error.
I just used a text file I happened to have to test.
Private strTxtOrderNum As String = "host"
Private Sub ReadFile()
Using sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
ListBox1.Items.Add(sr.ReadLine)
Loop
End Using
End Sub
Private Sub WriteFile()
Dim strSelectedItem = ComboBox1.Text
Using swVar As IO.StreamWriter = IO.File.AppendText(strTxtOrderNum & ".txt")
swVar.WriteLine(strSelectedItem)
End Using
End Sub
i have start to develop a new application to generate md5 hashes from strings and the it save automatic to my.settings.md5_hashes
and now i need to check if a certain value all ready exist in my settings md5_hashes. i have find some examples but it give me all ways a error on it
the error
Error 1 Value of type 'System.Collections.Specialized.StringCollection' cannot be converted to 'String'.
how can i check if a value its all ready exist on my settings?
This is my code
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If My.Settings.md5_hashes = (TextBox1.Text) Then
End If
If Not My.Settings.md5_hashes = (TextBox1.Text) Then
Me.Show()
End If
End Sub
End Class
This is the code to generate the hashes and save it to my.settings.md5_hashes
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
ListBox1.Items.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Form2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim W As IO.StreamWriter
Dim i As Integer
W = New IO.StreamWriter("C:\MD5.txt", True)
For i = 0 To ListBox1.Items.Count - 1
W.WriteLine(ListBox1.Items.Item(i))
Next
W.Close()
MsgBox("You File Is Save You Can Locate It At C:\MD5.txt")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Form3.Show()
End Sub
End Class
well i have just change some part of the code
this is my new code
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text)
ListBox1.Items.Add(TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text)
My.Settings.md5_hashes.Add(TextBox2.Text)
My.Settings.md5_hashes.Add("<--->")
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
ListBox1.Items.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Form2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim W As IO.StreamWriter
Dim i As Integer
W = New IO.StreamWriter("C:\MD5.txt", True)
For i = 0 To ListBox1.Items.Count - 1
W.WriteLine(ListBox1.Items.Item(i))
Next
W.Close()
MsgBox("You File Is Save You Can Locate It At C:\MD5.txt")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Form3.Show()
End Sub
End Class
well now its return true but how can i get the value to show on a msgbox the results?
well i solved my self the way to get results of the name for example if i search with the name to retrieved the hash , but how to do if i search for a hash and get the name how to take the name i stored initial there ?
Use the Contains method:
If My.Settings.md5_hashes.Contains(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
Tip: Check what members IntelliSense shows you when you type My.Settings.md5_hashes. (notice the dot on the end). By doing so you would most likely have found the answer to this yourself.
EDIT:
In your specific case it gets a little more complicated because you must separate the input and the hash before you can check that the hash exists.
Try this (this is for the first code you posted in the question):
Private Function HashExists(ByVal Hash As String) As Boolean
For Each Entry As String In My.Settings.md5_hashes 'Iterate through every hash entry.
Dim Parts As String() = Entry.Split(New String() {"<--->"}, 2, StringSplitOptions.RemoveEmptyEntries) 'Split the entry on "<--->".
If Parts.Length = 1 AndAlso Parts(0) = Hash Then
Return True 'No "<--->" was present, but the entire entry matches 'Hash'.
ElseIf Parts.Length >= 2 AndAlso Parts(1) = Hash
Return True 'The entry was split successfully and the second part and 'Hash' are equal.
End If
Next
Return False 'No valid hash found.
End Function
Then use this like:
If HashExists(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
EDIT 2:
To get the name from a hash or a hash from a name the above code just need to be modified slightly to return any of the two parts.
Private Function GetEntry(ByVal Input As String, ByVal NameFromHash As Boolean) As String
For Each Entry As String In My.Settings.md5_hashes 'Iterate through every hash entry.
'Parts(0) is the name.
'Parts(1) is the hash.
Dim Parts As String() = Entry.Split(New String() {"<--->"}, 2, StringSplitOptions.RemoveEmptyEntries) 'Split the entry on "<--->".
If Parts.Length >= 2 Then
If NameFromHash = True AndAlso Parts(1) = Input Then
Return Parts(0) 'Input was a valid hash, return name.
ElseIf NameFromHash = False AndAlso Parts(0) = Input Then
Return Parts(1) 'Input was a valid name, return hash.
End If
End If
Next
Return Nothing 'No valid entry found.
End Function
Usage:
'Get the name from a hash.
Dim Name As String = GetEntry(TextBox1.Text, True)
'Get the hash from a name.
Dim Hash As String = GetEntry(TextBox1.Text, False)
If the function returns Nothing that means it couldn't find the name/hash you specified. To avoid getting a NullReferenceException you can check so in an If-statement:
'Name from hash.
If Name IsNot Nothing Then
'The specified hash was found, do something with the name.
Else
'Hash not found.
MessageBox.Show("The specified hash was not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Hash from name.
If Hash IsNot Nothing Then
'The specified name was found, do something with the hash.
Else
'Name not found.
MessageBox.Show("The specified name was not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
First, let's have a look at my code:
Private Sub FormLoad(sender As Object, e As EventArgs) Handles MyBase.Load
txtMDF.Text = My.Settings.MDF
End Sub
Assume My.Settings.MDF has a string value of Application.StartupPath + "\MyDB.mdf". I get this result:
But I want the result to be:
I have tried the following links and methods:
How to convert string to code in vb.net and to view static or public variables of main project
CodeDom - But didn't understand it.
As ProGamer Suggested,
First
save you My.Settings.MDF String = None
Second
Edit your code as follows:
Private Sub FormLoad(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.MDF = "None" Then
txtMDF.Text = Application.StartupPath + "\MyDB.mdf"
Else
txtMDF.Text = My.Settings.MDF
End If
txtMDF.Text = My.Settings.MDF
End Sub
Third
Add the following code to YourFormClose_Event
Private Sub FormClosing(sender As Object, e As CancelEventArgs) Handles Me.Closing
My.Settings.MDF = txtMDF.Text
My.Settings.Save()
End Sub
And NOTE that you should select "User" from 'Scope Drop Down' in the Settings for MDF instead of "Application" or else My.Settings.Save() will not work and it will remain "None"
Example:
I'm currently making a login form to my program where I have a watermark for the two textboxes Email and Password.
When a textbox is empty, its watermark text will appear in it like "Username" and "Password".
My code is:
Public Class frmLogin
Private Sub TextBox2_LostFocus(sender As Object, e As System.EventArgs)
If TextBox2.Text = "" Then
TextBox2.ForeColor = Color.DarkGray
TextBox2.Text = "Username"
End If
End Sub
Private Sub TextBox2_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox2.GotFocus
TextBox2.Text = ""
TextBox2.ForeColor = Color.Black
End Sub
Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
If TextBox2.Text = "" Then
TextBox2.ForeColor = Color.DarkGray
TextBox2.Text = "Username"
End If
TextBox1.Text = ""
TextBox1.ForeColor = Color.Black
End Sub
Private Sub TextBox1_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox1.LostFocus
If TextBox1.Text = "" Then
TextBox1.ForeColor = Color.DarkGray
TextBox1.Text = "Password"
End If
End Sub
End Class
But now my problem is that I want to use password characters for the password. But I still want the watermark text to be in regular text. When I check to use a password char it turns my watermark into "**" instead of "Password". How can I fix that?
This seems good:
Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
Textbox1.PasswordChar = "*"
Textbox1.Clear()
End Sub
Private Sub TextBox1_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox1.LostFocus
TextBox1.PasswordChar = ControlChars.NullChar
End Sub