how to store string in date in vb.net? - vb.net

how to store string value in date variable in vb.net
i am using the following code
dim dtBL as Date
txtBLDate.text="23/11/2010"
dtBL = Format(CDate(txtBLDate.Text), "MM/dd/yyyy")
but i am getting the error which says that 'Conversion from string "23/11/2010" to type 'Date' is not valid.'
please advice on this

Two rules of thumb I give everyone with VB.Net:
Turn Option Strict On
Abandon the Microsoft.VisualBasic-Namespace ASAP
To answer your question, your date is in the format dd/MM/yyyy and not MM/dd/yyyy.

i am using vb.net in web applications
i put a page code that may be helping you dealing with dates
ASPX
code behind
Partial Class DateFormatConversions Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
lblDate.Text = Today.ToString("M/d/yyyy")
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
lblDate.Text = Today.ToString("MM/dd/yyyy")
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
lblDate.Text = Today.ToString("d/M/yyyy")
End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
lblDate.Text = Today.ToString("dd/MM/yyyy")
End Sub
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim DTFI As New System.Globalization.DateTimeFormatInfo
DTFI.ShortDatePattern = DropDownList1.SelectedValue
Dim addedDate As DateTime
addedDate = DateTime.Parse(TextBox1.Text, DTFI)
lblDateOutput.Text = addedDate.ToLongDateString
End Sub End Class

Imports Microsoft.VisualBasic Imports System.Globalization Public Class DatumKonvert1 Public Shared Function DK1(ByVal myDMstring As String) As Date
Dim source As String = myDMstring
Dim d As DateTime = DateTime.ParseExact(source, "d'/'M'/'yyyy", CultureInfo.InvariantCulture)
Dim resultMydate As String = d.ToString("M'/'d'/'yyyy")
Dim mdx = DateTime.ParseExact(resultMydate, "M'/'d'/'yyyy", CultureInfo.InvariantCulture)
Return mdx End Function End Class

Related

Error: Form1 is a type of windowsApplication cannot be used as an expression

How to solve error when passing and returning some data across the form in visual basic.
Error: Form1 is a type of windowsApplication cannot be used as an
expression
showin error on "Form 1" (Public Class Form1)
FORM 1 CODE
Public Class Form1
Dim eid As String = ""
Public Sub New(ByVal empid As String)
InitializeComponent()
eid = empid
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare a variable of string type
Dim pass As String = TextBox1.Text
Dim frm As New Form2(pass)
frm.ShowDialog()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label2.Text = eid.ToString()
End Sub
End Class
FORM 2 CODE
Public Class Form2
Dim eid As String = ""
Public Sub New(ByVal empid As String)
InitializeComponent()
eid = empid
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = eid.ToString()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim value As String = TextBox2.Text
Dim fr As New Form1(value)
fr.ShowDialog()
End Sub
End Class
To solve your error message add this to your Form1 Class
Public Sub New()
InitializeComponent()
End Sub
If you are trying to pass values between forms, you might find this link useful...
http://grantwinney.com/passing-data-between-two-forms-in-winforms/
Public Class Form1
Dim eid As String = ""
Public Sub New(ByVal empid As String)
InitializeComponent()
eid = empid
End Sub
Public Sub New()
InitializeComponent()
End Sub
startup form isn't being called with any parameters
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare a variable of string type
Dim pass As String = TextBox1.Text
Dim frm As New Form2(pass)
frm.ShowDialog()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label2.Text = eid.ToString()
End Sub
End Class
It happens when you change the project type from class library to windows forms in visual studio.
Head to Application.Designer.vb in your project and there you'll find something like this:
Namespace My
'NOTE: This file is auto-generated; do not modify it directly. To make changes,
' or if you encounter build errors in this file, go to the Project Designer
' (go to Project Properties or double-click the My Project node in
' Solution Explorer), and make changes on the Application tab.
'
Partial Friend Class MyApplication
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Public Sub New()
MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
Me.IsSingleInstance = false
Me.EnableVisualStyles = true
Me.SaveMySettingsOnExit = true
Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses
End Sub
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.AeonLabs.Layouts.Main.mainAppLayoutForm
End Sub
End Class
End Namespace
in the sub OnCreateMainForm() where it has OnCreateMainForm change it to something like this
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = New Global.AeonLabs.Layouts.Main.mainAppLayoutForm
End Sub
and you'll be good to go!!

Replace string if followed by any character visual basic

I am new to Visual Basic. I want to use the like operator in a textbox to change a character if it is followed by any other character. But it should be on the key-up event.
Anyone please help me: how I can make the following code work?
Public Class Form1
Dim myString As String
Dim sMatch As Boolean = myString Like "x?"
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If sMatch = True Then
TextBox1.Text = TextBox1.Text.Replace(myString, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myString = "x"
End Sub
End Class
Try this and ask. Your variable myString was never getting it's value from the TextBox. You just set it to x in the load event.
Public Class Form1
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If TextBox1.Text Like "x?" Then
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "x"
End Sub
End Class

Data substring not showing full value

My code should be showing 32 characters but it's only showing 7. This is the code I currently have:
Imports System.IO
Public Class Form1
Private Property sr As Object
Private Sub BrowseBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseBtn.Click
OpenFileDialog.ShowDialog()
FilePathLabel.Text = OpenFileDialog.FileName
End Sub
Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click
Dim sr As StreamReader = New StreamReader(OpenFileDialog.FileName)
Dim data = sr.ReadToEnd()
Dim pos = data.IndexOfAny("LASTSAVE")
If pos >= 0 Then
End If
CatiaVersionLabel.Text = data.Substring(pos, 32)
End Sub
End Class
Not too sure why it's doing this as the text that needs to be found is there when I open it independently.

"Version" is a type and cannot be resolved

Here is my code :
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.SelectedItem = "1.6.4 Vanilla Server" Then
Version = "164"
End If
If ComboBox1.SelectedItem = "1.6.2 Vanilla Server" Then
Version = "162"
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Version As Int16
End Sub
End Class
Then I get a blue line under Version saying : Version is a type and cannot be used as an expression
Thanks for any help :/
Aaak. here you go:
Public Class Form1
Private VersionNo As String
...
Private Sub Button1_Click...
VersionNo = "164"
....
End Sub
If you declare it in Form_Load it goes out of scope when the sub completes. you want a module level variable. When that happened, VB thought you were talking about the Version Type. If you want to use Version you may have to bracket it: [Version] to tel VB to use your var, not the NET Type.
Try changing the name of Version to VersionNo
Edit: declare the variable in the Form1 not the Form1_load
Use ME.VersionNo = "162", you have declared it as int16 and assigning string to it.

Error in programming

Imports SpeechLib
Public Class Form1
Public vox = CreateObject("sapi.spvoice")
Private Sub cmdSpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSpeak.Click
Dim t As String = "Hello , This is a Text"
Say(t)
End Sub
Public Sub Say(ByVal text As String)
vox.Speak(text,SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
vox.pause()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vox.AlertBoundary = SVEPhoneme
End Sub
End Class
I am getting an error
Name 'SVEPhoneme' is not declared.
How and where do I declare it ?
It is SpeechVoiceEvents.SVEPhoneme
This is all a lot easier if you make this code early bound:
Public vox as New SpVoice
Or better yet, use the .NET wrapper for the sapi, System.Speech assembly.
Imports System.Speech.Synthesis
Public Class Form1
Public vox As New SpeechSynthesizer
Public Sub Say(ByVal text As String)
vox.SpeakAsync(text)
End Sub
End Class
SVEPhoneme represents the Phoneme event, which occurs when the engine completes a phoneme while speaking.
Try setting SVEPhoneme to be the integer 64.
http://msdn.microsoft.com/en-us/library/ms720886(v=vs.85).asp