|
If you are using VB.NET, you can use "regular expressions" to validate your field matches a particular format... if you are using VB6 (why??), then you will need to have an event behind your form that passes the textbox's value to a function for parsing.
|
|
|
|
|
As Damian S said, you can use regular expressions in VB.Net. Try this function :
Private Function ValidatePhoneNumber(ByVal StringToTest As String) As Boolean
Dim expression As New Regex("(\(\d{3}\)|\d{3})[- ]\s?\d{3}[- ]\d{4}")
Dim match As Match = expression.Match(StringToTest)
If match.Success Then
Return True
Else
Return False
End If
End Function
Then you can call your function like this :
If TextBox1.Text <> String.Empty Then
If ValidatePhoneNumber(TextBox1.Text) Then
MsgBox("Correct phone number.")
Else
MsgBox("Not valid phone number")
End If
End If
If you want to learn more about regular expressions then I recommend you download a copy of Expresso (google it). It is an excellent tool for learning them.
Hope this helps.
|
|
|
|
|
Thank you Damian. I will try your example and look at regular expressions.
|
|
|
|
|
Regular expressions are the best way to go, as you can then embed some real phone number rules such as area code validation and so on.
Alternatively if you are only dealing with the one format allowed then I suggest you use a masked textbox so the end user can't even enter the wrong format.
|
|
|
|
|
Thanks Napolean Solo.
I will try regular expressions.
|
|
|
|
|
I'm using Visual Studio 2008 to develop a web site for my daughter's softball league. In one page I have two RadioButtonLists in the same panel in their own table cells. I programatically build the RBLs and determine the initial selected index values for each. The behavior I'm seeing is as follows:
1. I sucessfully change the first selected index from its cleared
state (-1) to the value I calculated. The second RBL remains in
its clear state (as it should).
2. I then set the second RBL's selected index. While its value is
correctly set, the first RBL's selected index is also altered
to the same value simulataneously.
I've stepped through with the VS debugger and watched these two steps occur as described.
Is this a known faux pas on my part? If so what have I done wrong?
Otherwise, has anyone else seen similar behavior?
I'm not sure what to include here so I'll probably provide too much of some detail and not enough of other, but I'll gladly provide the entire aspx file (imbedded VB script plus a common function VB scrip "include") and/or screen captures of the variable states as I stepped through the code, if any of these would be of use.
Please don't make fun of my choice of style!
PLEASE NOTE: Even though I'm including the related event handler routines, the erroneous behavior occurs on page load before any user selections are made.
Detail below.
Thanks in advance,
John
#########################################################################################
Page Content:
<tr>
<td> </td>
<td align="left">
<asp:RadioButtonList ID="hTeamNameRBL" runat="server" Enabled="False" OnSelectedIndexChanged="selectedHomeTeamChanged" AutoPostBack="True"></asp:RadioButtonList>
</td>
<td> </td>
<td align="left">
<asp:RadioButtonList ID="aTeamNameRBL" runat="server" Enabled="False" OnSelectedIndexChanged="selectedAwayTeamChanged" AutoPostBack="True"></asp:RadioButtonList>
</td>
<td> </td>
<td align="left" valign="top">
<asp:RadioButtonList ID="fieldRBL" runat="server" Width="200px" style="text-align:left;" Enabled="False" OnSelectedIndexChanged="selectedFieldChanged" AutoPostBack="True"></asp:RadioButtonList>
<asp:TextBox ID="otherField" runat="server" Enabled="false" OnTextChanged="otherFieldTextChanged" AutoPostBack="True"></asp:TextBox>
</td>
<td> </td>
</tr>
#########################################################################################
RadioButton Contructor Routine:
PLEASE NOTE:: The common function call...
myCommonVB.getTeamNamesByDiv(myYear, division, teamNames, teamNumbers, teamLetters, errEncountered, errDetail)
Takes (ByVal) integer "year" and "division" values and returns (ByRef) string arrays of teamNames, teamNumbers and teamLetters (pulled from a database), as well as error information if any.
Sub buildTeamRBL(ByVal division As Integer, Optional ByVal myYear As Integer = 2010)
Dim teamNames As String() = Nothing
Dim teamNumbers As String() = Nothing
Dim teamLetters As String() = Nothing
Dim lItem As New ListItem()
Dim tnLen As Integer = 0
Dim hTeamSelected As Integer = -1
Dim aTeamSelected As Integer = -1
hTeamNameRBL.Enabled = True
aTeamNameRBL.Enabled = True
hTeamNameRBL.Items.Clear()
aTeamNameRBL.Items.Clear()
hTeamNameRBL.ClearSelection()
aTeamNameRBL.ClearSelection()
myCommonVB.getTeamNamesByDiv(myYear, division, teamNames, teamNumbers, teamLetters, errEncountered, errDetail)
If Not errEncountered Then
tnLen = teamNames.GetUpperBound(0)
For i As Integer = 0 To tnLen - 1
lItem = New ListItem()
lItem.Text = teamNames.GetValue(i)
lItem.Value = lItem.Text
aTeamNameRBL.Items.Add(lItem)
If lItem.Text.Equals(aTeamName) Then
aTeamSelected = i
aTeamNumber = teamNumbers(i)
aTeamLetter = teamLetters(i)
End If
hTeamNameRBL.Items.Add(lItem)
If lItem.Text.Equals(hTeamName) Then
hTeamSelected = i
hTeamNumber = teamNumbers(i)
hTeamLetter = teamLetters(i)
End If
Next
aTeamNameRBL.SelectedIndex = aTeamSelected
hTeamNameRBL.SelectedIndex = hTeamSelected
Else
debug.Text += errDetail
End If
End Sub
#########################################################################################
OnSelectedIndexChanged routines:
FYI: The following two ensure the alternate RBL doesn't have the same selected value.
Sub selectedHomeTeamChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim orgAteamSelectedIndex As Integer = aTeamNameRBL.SelectedIndex
Dim newHteamSelectedIndex As Integer = hTeamNameRBL.SelectedIndex
For i As Integer = 0 To teamNames.GetUpperBound(0) - 1
aTeamNameRBL.SelectedIndex = i
aTeamNameRBL.SelectedItem.Enabled = True
Next
aTeamNameRBL.SelectedIndex = newHteamSelectedIndex
aTeamNameRBL.SelectedItem.Enabled = False
If Not orgAteamSelectedIndex = -1 Then
aTeamNameRBL.SelectedIndex = orgAteamSelectedIndex
Else
aTeamNameRBL.ClearSelection()
End If
seeIfEnablingSubmit()
End Sub
Sub selectedAwayTeamChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim orgHteamSelectedIndex As Integer = hTeamNameRBL.SelectedIndex
Dim newAteamSelectedIndex As Integer = aTeamNameRBL.SelectedIndex
For i As Integer = 0 To teamNames.GetUpperBound(0) - 1
hTeamNameRBL.SelectedIndex = i
hTeamNameRBL.SelectedItem.Enabled = True
Next
hTeamNameRBL.SelectedIndex = newAteamSelectedIndex
hTeamNameRBL.SelectedItem.Enabled = False
If Not orgHteamSelectedIndex = -1 Then
hTeamNameRBL.SelectedIndex = orgHteamSelectedIndex
Else
hTeamNameRBL.ClearSelection()
End If
seeIfEnablingSubmit()
End Sub
|
|
|
|
|
at the 1st line of the Procedure "selectedHomeTeamChanged"
Check for
If not page.isPostBack=true Then
'''''' write your code'''''''''
End If
Arindam Banerjee
Sr. Software Developer
Rance Computer Pvt Ltd.
Kolkata (India)
|
|
|
|
|
Hi Arindam.
Thanks very much for your reply.
I tried your suggestion w/o success. I did so even I saw (by stepping through with the debugger) the errant behavior occur on and between the two distinct "steps":
1. aTeamNameRBL.SelectedIndex = aTeamSelected
2. hTeamNameRBL.SelectedIndex = hTeamSelected
If it would be of use to anyone I have screen captures showing the fact that after step 1. is executed, the SelectedIndex of both RadioButtonList's are set to the "aTeamSelected" value, and then after step 2. both are set to the "hTeamSelected" value.
Also, I'd be happy to grab whatever additional debug detail you might suggest.
As I've seen Visueal Studio occasionally wrap itself around the axle and not register the fact changes have been made I also tried...
a. Deleting and recreating the RBLs
b. Deleting the RBs, saving the file, closing the project and then recreating them
c. Renaming them
None of these attemps made any difference. As soon as the first RBL was adjusted, the second was as well, and vice-versa.
HELP!!!!
|
|
|
|
|
Hello ! why i can't catch event keydown in a form that contain a datagridview. i have use KeyPreview= true in form load. but i still can't get any keydown value after pressing key. please give me some advise
|
|
|
|
|
See here[^].
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
I tried and works fine for me.
|
|
|
|
|
Hi all,
I have an issue with a datagrid in VB 2008 Express. I have loaded an Excel file into a datagrid as a preview; now I want to insert the datagrid content into a table in an Oracle DB (tha table have the same fields of the excel file). How ca I do it? I have tried with the following code but doesn't work
Using connection As New OleDbConnection(connectionString)
Dim adapter As New OleDbDataAdapter()
adapter.InsertCommand = New OleDbCommand(queryString, connection)
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)
connection.Open()
adapter.Fill(data)
adapter.Update(data, tableName)
Return data
End Using
When VB try to execute this command
adapter.Fill(data)
it crash with vshost error. If I Delete this row the functon works, but I haven't the data into the table.
Is there anyone can help me?
Many thanks
Filippo
|
|
|
|
|
What is the error message?
|
|
|
|
|
"vshost.exe has stopped working" and the application crash
|
|
|
|
|
Hi tried also to cicle into the rows/cols into the Datase with the following instruction
Dim iRow As Integer
Dim iCols As Integer
Dim sriga As String
For iRow = 0 To dgvPreview.RowCount
For iCols = 0 To dgvPreview.ColumnCount
If Len(sriga) = 0 Then
sriga = dgvPreview.Rows(iRow).Cells(iCols).Value.ToString
Else
sriga = sriga & ", " & dgvPreview.Rows(iRow).Cells(iCols).Value.ToString
End If
Next
TextBox2.Text = TextBox2.Text & vbCrLf & sriga
TextBox2.Refresh()
sriga = ""
Next
But the finel result is the same The Vb crash with the same vshost error
Please Help me! I'm going mad!
|
|
|
|
|
Try the following:
For iRow = 0 To dgvPreview.RowCount - 1
For iCols = 0 To dgvPreview.ColumnCount - 1
(Note the additional "- 1" at the end of the lines)
Maybe it is also helpful for your further devolpments to use try-catch-blocks, to handle exceptions. Normally the Exception itself contains several useful information, which could help you to identify the error's position and circumstances.
|
|
|
|
|
Well, first of all, I don't think you're understanding how ADO.Net works.
I suggest starting here: Overview of ADO.NET[^].
The InsertCommand that you are creating is used to run a SQL Insert...or in other words to insert a record into a database. And, the InsertCommand is only used when running an Update where records are marked as added or if you say InsertCommand.ExecuteNonQuery() .
If you are trying to fill a table, you need to change the way you are setting up the table. I suggest looking at: OleDBDataAdapter Class[^]
You want to change it to provide a SelectCommand. Generally, you do this like:
newDataAdapter = New OleDb.OleDbDataAdapter(sql, connectionString)
newDataAdapter.Fill(ProjectDS, "ProjectInfo")
I would say you clearly haven't looked at how to use ADO.NET and you need to start there.
|
|
|
|
|
One more thing...you need to make sure to close your connection before returning. It's bad practice to keep it open. I also hope you're creating the DataTable in your function because if it's a global variable, then why are you returning it? And if it's not accessible outside whatever class you're using, why are you making it a global variable?
|
|
|
|
|
Hi dear!,
I am trying to create a ruler for my project. The ruler is from right to left. I want to add an autowidth propert, which should automatically set the width of ruler when the form is resized.
at design time it works but at run time it give me error.
Following is the Ruler's code and it give error when I add it to an application and run it.
Public Class nytRuler
Private Sub nytRuler_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim p As Point = New Point(0, 0)
Dim s As Size = New Size(Me.Width, Me.Height)
Dim curRec As Rectangle = Me.ClientRectangle()
g.DrawRectangle(Pens.Black, curRec)
'Converting Pixels to Twips
Dim dpmm As Single = g.DpiX / 25.4F
Dim ptoTwip As Single = (curRec.Width * 15)
'Twips to Inches
Dim twipToInches As Single = CSng(ptoTwip / 1440)
Dim b As Single = curRec.Width
'Draw Numders 1 2 3 etc.
For a As Single = 0 To twipToInches
g.DrawString(a, New Font("Arial", 10), Brushes.Black, b, 10)
b = b - 96 '288 192 96
Next
b = curRec.Width
'Dim ptomm As Integer = Me.Width / dpmm
'Draw Lines between inches, and long line every 5th
Dim y As Single = 10.0F
Dim z As Integer = 0
For x As Single = b To 0 Step -9.6
g.DrawLine(Pens.Black, x, 0, x, y)
z += 1
If z = 5 Then
y = 15
z = 0
Else
y = 10
End If
Next
End Sub
Private wd As Boolean
Public Property autowidth() As Boolean
Get
Return wd
End Get
Set(ByVal value As Boolean)
If value Then
Me.Width = ParentForm.Width
End If
wd = value
Me.Invalidate()
End Set
End Property
End Class
|
|
|
|
|
nyt1972 wrote: at design time it works but at run time it give me error.
Maybe you should tell us what the error is, so we can maybe narrow down the problem. It does help us who haven't developed our telepathy yet.
|
|
|
|
|
I have copied and pasted your code into a new forms application and it generates a ruler when the project is run in Debug and in Release.
So without knowing your problem/error, can't help!
|
|
|
|
|
You are tying to create a control for ruler.
if you'r using control in your form it will work like same as run time.
I think you should use graphics to set in your control and do't use the paint method to crate image in the control.
If you can think then I Can.
|
|
|
|
|
Thanks for reply dear,
The problem is that When I build the Ruler and add it to a form then I set the rulers autowidth property to True and run the Application it shows me the following error.
System.InvalidOperationException was unhandled
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."
Source="WindowsApplication1"
StackTrace:
at WindowsApplication1.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at WindowsApplication1.My.MyProject.MyForms.get_Form1()
at WindowsApplication1.My.MyApplication.OnCreateMainForm() in C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.NullReferenceException
Message="Object reference not set to an instance of an object."
Source="WindowsApplication1"
StackTrace:
at WindowsApplication1.nytRuler.set_autowidth(Boolean value) in C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\nytRuler.vb:line 45
at WindowsApplication1.Form1.InitializeComponent() in C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.Designer.vb:line 31
at WindowsApplication1.Form1..ctor() in C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.Designer.vb:line 53
InnerException:
Please help, I will be thankful.
|
|
|
|
|
Pls pls pls help me, I am getting mad.
|
|
|
|
|
nytRuler is a class, I want to create a dll file and then Insert that ruler to an application where it causes this error when I set the ruler's autowidth property to true and run the application.
hope this will make u understand.
Please help.
Thanks in Advance.
|
|
|
|