|
Hi Luc,
I will digest that. In the meantime, as of a minute ago and after making sure everything came out in American date format, the service is running without errors.
It has been a salutory lesson that running under Local System is a different dimension to running as a logged in user.
My most sincere thanks to you Luc as I would not have sussed this one for ages without you pointing me in the right direction. Unfortunately there is no :hail: smiley so I can't use it, but :hail: anyway.
|
|
|
|
|
you're welcome.
Getting it to work properly is part of the goal; making sure it works reliably is a part as well. And IMO striving for excellent readability is a big factor in both.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Hi Luc,
"your message and code feels very slithery to me;" Eww! I hate snakes.
"I'll pick a simple one here
strPartLine = astrData(QUAKE_ID) & COMMA"
Mm, sorry Luc that one was confusing as it is a testing line.
"would the app be about earthquakes?" Yup - Importing USGS data using a service
"The same goes for your problem description:
This is a data line being imported
nc,71401286,0,"Thursday, May 20, 2010 01:00:54 UTC",38.8335,-122.8778,1.0,1.80,12,"Northern California"
which may, or may not, mean: the initial content of strLine is ..."
I put that in there to show what the date part looked like on the original so you could perhaps make head or tail of my code that was chopping it up and turning it into numbers only. Unfortunately as my DB says, I am not the best at explaining things.
"and then
Message="Conversion from string "19/05/2010 02:12:54" to type 'Date' is not valid."
Are you saying the above input (May 20, 2010 01:00:54) is converted into 19/05/2010 02:12:54 and then it throws? " Yes basically.
"That is a mysterious conversion, I have no clue what functionality you are trying to get; or did you just throw some non-matching examples together to make things more obscure?" ??? Not quite sure what you mean there. Can I just put the text in - May 20, 2010 01:00:54 ??. I think possible when logged in but not when running as a service maybe?
"So my problem overall was you were not offering much of a grip on the problem. I had to assume too many things at once to come up with a reasonable hypothesis, apart from the impression you had a date problem, probably caused by an implicit format assumption."
Nevertheless you did manage to come up with an hypothesis! Again my thanks.
Re your last post:
"Getting it to work properly is part of the goal; making sure it works reliably is a part as well. And IMO striving for excellent readability is a big factor in both." I will try to be clearer in future!!
Over and out
|
|
|
|
|
I know the issue has been resolved; but, I just read the post. Here's another take on a solution. This code uses the Date Wizard class library to come up with the converted date variable. (Click here for the article on the Date Wizard.)
Imports _2gs_datewizard
'** Split source string on spaces (source string is in Textbox1)
Dim ss() As String
ss = TextBox1.Text.Split(" ")
'** Convert the month in ss(1) from a name to a number
Select Case ss(1).Substring(0, 3).ToLower
Case "jan" : ss(1) = "1"
Case "feb" : ss(1) = "2"
Case "mar" : ss(1) = "3"
Case "apr" : ss(1) = "4"
Case "may" : ss(1) = "5"
Case "jun" : ss(1) = "6"
Case "jul" : ss(1) = "7"
Case "aug" : ss(1) = "8"
Case "sep" : ss(1) = "9"
Case "oct" : ss(1) = "10"
Case "nov" : ss(1) = "11"
Case "dec" : ss(1) = "12"
End Select
'** Remove trailing comma from the day in ss(2)
Dim dayString As String = ss(2).ToString
ss(2) = dayString.Substring(0, dayString.Length - 1)
'** Convert to date (ss(3) contains the year)
Dim df As New C_DateFunctions
Dim sourceDate As Date = df.BuildDate(ss(1), ss(2), ss(3))
df = Nothing
'** Test the procedure by displaying the resulting date
MsgBox(sourceDate.ToShortDateString)
The test at the end displays the converted date as 5/20/2010.
|
|
|
|
|
i use mscomm in vb.net.
the problem is i cant detect the comport is open or close by other software,i can do this only if the comport is open by my own vb.net program.
when i use hyper terminal to connect by comport1,i open my vb.net program and check the comport.portopen it always give me false.so when i use the following code:
if comport.portopen=true then
comport.portopen=false
end if
i cant close the port as the vb program not even know it is open by other software.
after that if i continue use port.portopen=true i will give me exception as the port is alredi open.
i had surf through web and found many ppl having this problem also,but nobody provide a good solution.
is there a way to force all the comport of my computer to close on my formload?
|
|
|
|
|
newbievbnet wrote: is there a way to force all the comport of my computer to close on my formload?
I hope there isn't. When an app opens something (a file, a serial port, whatever) and got promised exclusive access, then no other app should have the power to undo that.
A process can ask for (a fraction of) available CPU cycles, memory, access to files, ports, etc, to the extent that those are free. And Windows will provide or not provide as it sees fit, taking into account what has already been set aside for other running processes. No process can demand resources.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I am sort of new to multithreadings (doing it about 9 months now) and I ran into an unusual error that I can't figure out. First, how the code is set up:
I have my delegate Delegate Sub RecommendReportsDoneDelegate()
Before I start the calling of the new thread:
DetermineRecommendations = New clsReportRecommendations
AddHandler DetermineRecommendations.StatusChange, AddressOf StatusChange
AddHandler DetermineRecommendations.RecommendationsDone, AddressOf RecommendationsDone
DetermineRecommendations.Start(DataFolder)
The "Start" method actually creates the new thread in the DetermineRecommendations class. Finally, the sub that handles the RecommendationsDone event is:
Private Sub RecommendationsDone()
Try
If Me.InvokeRequired Then
Me.Invoke(New RecommendReportsDoneDelegate(AddressOf RecommendationsDone))
Else
Me.Enabled = True
CopyData()
SetUpUnitSheet()
ShowRecommendations()
SetUpSamplingSheet()
DetermineReportType()
Status.Text = "Finished"
End If
Catch ex As Exception
End Try
However, on the Me.Invoke I get the following error:
Index and length must refer to a location within the string. Parameter name: length
The error occurs in mscorlib. Any ideas on why there seems to be some string function occurring with the Me.Invoke ? By the way, the
|
|
|
|
|
Hi,
I don't see anything wrong right away.
However I feel some essential information must be missing here. I would like to know:
- what is "Me" in the code shown, i.e. in which class is all this? and what type does it derive/inherit from?
- is any of the relevant objects created by code running on a secondary thread? are you trying to create a Form, a Control, ... by a timer tick handler, by a BackgroundWorker.DoWork handler, ...?
I also would suggest you put the whole else block (Me.Enabled...Status.Text=...) in one try-catch, where you catch an Exception and show Exception.ToString() in its entirety. I expect an exception will throw, and offer exact class name, method name, line number information (look for the first line number it spits out).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
OK, you can probably tell from my subject that I am a newbie to VB.net. This may be simple, but I haven't seen (or recognized) a method yet. My problem: An existing program needs to pass information to, and get data from another independent executable module (or DLL) whose name must be defined at run-time. I want to be able to vary the "slave" module as well as it's call name without changing the host. Ideas would be very welcome. Thanks.
Troy
|
|
|
|
|
are you talking about two EXE files, hence interprocess communication?
or different parts of a single process, maybe an application with plug-ins?
and the existing program is a .NET one, you have the code, and will be able and allowed to modify it once?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc,
I'm open to whatever would work, two .EXE's or whatever. A friend suggested using DLL's for the "slave" routines, but I don't know if there is a method for run-time setting the call name of a DLL at run time in the supervisory program that once established, I don't want to change.
Yes, the existing program is .NET and I would modify it to call sending/receiving arrays of data in a standardized format.
Thanks,
|
|
|
|
|
OK,
I find having only one EXE simpler, so I would suggest a plog-in scheme. This is how it could be made to work:
- you take the existing code, add some, and create the EXE; that is one project.
- you define an Interface, which basically is a contract describing the methods and parameters all the plug-ins must offer.
- for one or more plug-ins, you create a DLL project, implementing the required functionality.
- in the main program, you add detect logic, select logic, and call logic.
The detect logic could be: all DLL files in a particular subfolder, say "plugins".
The select logic is up to you, it could be a Menu, a ComboBox, or whatever, populated with the names of the DLL files (assuming those are relevant); a more complex scheme would require you load those "assemblies", execute some code to get the types they implement, and call some method/property on them to get a display name, with which you populate the GUI.
The call logic would require some "reflection" code, look at Activator.CreateInstance()
I did not immediately find a relevant article that is based on VB.NET; however there are several with C#, here[^] is one (haven't read it though); the principles should be exactly the same, basically the only difference is syntactic.
I hope this gets you started.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
OK, Luc, thanks a lot. This is pretty much what I was looking for. I'll work on getting a test case going.
|
|
|
|
|
Hi,
I need to limit the user to only entering integers (positive and negative) into a textbox.
Now I know the trick with the KeyPress event and e.KeyChar , to limit the user to entering only numbers into a textbox, and with CChar("-") I can check for the minus sign.
The problem is, how do I make sure that when entered, the minus sign is always in first place?
Is it possible to check for the index position of the caret ?
Or am I going about this in an overly complicated fashion ?
My advice is free, and you may get what you paid for.
|
|
|
|
|
You can check the position of a specific character in the text box by calling indexOf on the control. Then check it is only at position 1. You will also want to check that it doesn't already have one by calling contains .
You will also need to do the same thing for a decimal point. (if it is the first character entered, then good practice is to prefix the textbox with a 0, so the dp is more visible.
And as you say you already know about the the keypress and keychar, I tend to use the keydown event, but it upto you how you want to do things.
|
|
|
|
|
I had thought of this solution as well, but I had a silent hope that maybe I could do the checking In Flagrante Delicto so to speak, instead of after the minus sign has been entered already.
I mean, during the KeyPress (or indeed KeyDown) event the key has not actually been entered yet, so I can't check for its index yet (right?).
My advice is free, and you may get what you paid for.
|
|
|
|
|
In that situation you can check for the length of myTextBox.Text , if it is greater than zero, don't allow the minus.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
Tried that (and it works, thanks), but yet another but: I would like for the user to be able to enter a number and then add the minus sign afterward (but only at the beginning of the text of course).
So I suppose I am back to finding the index where the new character is going to be entered.
My advice is free, and you may get what you paid for.
|
|
|
|
|
IMO you can't check validity of user input by only watching which keys get presses; unless you are willing to not support pasting. A good user interface IMO will always support copy and paste, and the clipboard could contain arbitrary text.
So you could:
- save the content before the modification
- read the content after modification
- validate it (int.TryParse could be appropriate)
- when not valid, restore the last saved content that was valid
Warning: this gets tricky when intermediate values don't have a valid syntax, e.g. when you want to accept floating-point numbers in engineering format.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Generally speaking, and in terms of best practices and such, you are of course entirely right. And I suppose I will be doing it your way in the end.
But I don't have to like it
In this case I am handling a form with a bunch of textboxes, each representing a setting. Each setting is simply a value between -999 and 999, and won't be changed much.
I suppose I am trusting the users to be lazy enough to not go through the trouble of copying and pasting such a short value.
So I limited the max length to 3 (which brings to mind another problem), and I handle their KeyPress events to limit input to numbers.
Anyway, now it is more a matter of interest than necessity. In other words can it be done?
My advice is free, and you may get what you paid for.
|
|
|
|
|
of course it can be done.
Assuming SelectionLength is zero, you would need TextBox.SelectionStart to know where the caret is, so you can perform the insert/append yourself, validate and then allow/disallow the keypress to be handled.
And when SelectionLength isn't zero, the new character will replace all the ones selected; and you could handle that too.
But is it all worth it?
The popular approach is:
- to only allow characters that could appear somewhere in the input
- to validate afterwards
So yes a minus sign is allowed, so KeyPressed accepts it; and no, it is not acceptable but as the first character, so int.TryParse() will make sure of that in the end.
And then some people will advise to use a NumericUpDown thingy; I'm not one of them, as I find those controls horribly user-unfriendly.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
You can always use a MaskedTextBox or a NumericUpDown, and use those controls internal validation.
|
|
|
|
|
This thread must rate as one of the most asked questions on CodeProject, and Dave's[^] was the best answer.
What is it that ties people to text boxes, when the NumericUpDown is designed out of the box to handle numbers only, and ranges for that matter? [Sorry: mini rant there, having seen the same question to many times]
If the control you are using doesn't do the job, maybe you are using the wrong control.
|
|
|
|
|
Don't worry, by all means, rant away.
Just checked the numeric updown control, and although it does what Luc suggested, about going back to the last valid value, it doesn't do what I was after.
In other words, indeed it efficiently corrects bad input, but it doesn't prevent it right from the bat, which was what I was interested in solving. I must admit I haven't really investigated the masked textbox though.
My advice is free, and you may get what you paid for.
|
|
|
|
|
Don't know how your getting on, but i have just been playing this afternoon trying to write a function to see if it can be done
This appears to work, and caters for various, minus and 0 entry perms.
e.g. if you enter 0 then 1, the leading 0 is binned.
e.g. if you enter 111 then a -, it will change it to -111
it won't allow 0000
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Dim tb As TextBox = CType(sender, TextBox)
Select Case e.KeyCode
Case Keys.Decimal, Keys.OemPeriod
If tb.Text.Contains(".") Then
e.SuppressKeyPress = True
Else
If tb.TextLength = 0 Then
tb.Text = "0" & tb.Text
tb.SelectionStart = tb.TextLength
ElseIf tb.TextLength = 1 And tb.Text.StartsWith("-") Then
tb.Text = "-0"
tb.SelectionStart = tb.TextLength
End If
End If
Case Keys.Subtract, Keys.OemMinus
If tb.Text.Contains("-") Then
e.SuppressKeyPress = True
Else
If tb.TextLength >= 1 Then
tb.Text = "-" & tb.Text
tb.SelectionStart = tb.TextLength
e.SuppressKeyPress = True
End If
End If
Case Keys.Back, Keys.Delete, Keys.Left, Keys.Right
e.SuppressKeyPress = False
Case Else
If Not IsNumeric(Chr(e.KeyValue)) Then
e.SuppressKeyPress = True
Else
If (e.KeyCode = Keys.D0 Or e.KeyCode = Keys.NumPad0) Then
If tb.Text.Contains("0") And Not tb.Text.Contains(".") Then
If Val(tb.Text) = 0 Then e.SuppressKeyPress = True
End If
Else
If tb.Text.Contains(".") = False And tb.Text.Contains("0") Then
If Val(tb.Text) = 0 Then
tb.Text = tb.Text.Remove(tb.Text.IndexOf("0"), 1)
tb.SelectionStart = tb.TextLength
End If
End If
End If
End If
End Select
End Sub
|
|
|
|
|