|
hello everyone!!
can v add validations for textboxes (insertions by user) in a windows application as in a web application
my problem is wit the registration form wherein i need to check tat a person enters a valid phone-no. etc..
|
|
|
|
|
Sure. This[^] is the beginning of a 3 part series of articles covering that very topic. Be sure to find the other two articles in the navigation pane on the left.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
what type of validation do you want exactly...?
like in that textbox of phone no u dont want that user could enter any character, dots space etc...?????
is it like that...?
specify in some detail..
|
|
|
|
|
yes, like in the textbox to enter a number, text should not b added and in a box for user's name, no numbers
|
|
|
|
|
like if u want to enter only digits and no alphabets make a keypress event like this
Private Sub Text1box_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress<br />
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)<br />
Select Case KeyAscii<br />
Case 48 To 57 '0 To 9<br />
Case 8 'Back Space<br />
Case 13 'Enter<br />
Case Else 'Other Keys<br />
KeyAscii = 0<br />
End Select<br />
If KeyAscii = 0 Then<br />
eventArgs.Handled = True<br />
End If<br />
End Sub
if you want only alphabets no digits then in case just enter the ascii of alphebets...
and also if u want to validate more than one textboxes then in keypress event after handles textbox1.keypress put textbox2.keypress, textbox3.keypress...and so on..
i had the same problem and got solution on this forum only around a week ago...check out that post wid heading 'validating textboxes'
|
|
|
|
|
You can handle the keypress event of your textbox, and set handled to true if it's a character you don't want. You can handle the leave event, and set focus back on the control if the input is not valid ( use a regex, probably )
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I want to program a checkbook register in VB.Net 2.0 (on XP, not Vista). I have a couple of ideas on how to store the data. But I'm stuck at even how to begin representing the register on the screen. Can anyone give me some hints, tips, ideas in that regard?
Thanks,
Owen
|
|
|
|
|
Worry about how you're going to represent the data first. Answer the basic questions first. What transactions are you going to handle? What is the information needed to perform each transaction? Stuff like that. It'll make your data layer much easier to write if you lay everything out ahead of time. Basically, you want this data layer to be able to do anything you want to the data, without any kind of a user interface.
How the transactions are setup and executed is the domain of the business layer. It sits between your UI and data layers and enforces all the rules for each transaction. Data is validated and massaged on this layer before any kind of transaction is built and executed by your data layer.
Now, after those two are done, the user interface practically writes itself. You start with a simple interface and build it up from there. Maybe using some textboxes and buttons. Simple. Then when your ready to move up to more complex, custom drawn interface elements, like filling in an actual image of a check, you just have to modify the UI layer.
I've got an "application" written entirely in SQL Server, a small football pool. It doesn't have any user interface at all but runs entirely inside SQL Server. The business logic (transactions) and data layers have been combined enitrely into SQL stored procedures, Views, and Triggers. The entire data model is defined inside the SQL database.
You can run the app entirely using nothing but simple SQL statements, or, if you wanted just opening the tables and start editing them directly, like adding players, games, changing the scoring model, whatever... You can view current standings or any game details by just opening the View you want. You can't screw up the database with invalid data because the validation rules in the business logic inside the SP's and Triggers won't let you. The database defends itself from bad data entirely on its own. It doesn't need a seperate business layer written in .NET code to keep the data safe.
Now, all I have to do is put together a number of user interfaces for the app, like an ASP.NET application and/or a Windows Forms app.
Why did I say all this?? Because if you need to change how the application looks, you don't have to touch the business and data layers at all. The application can still run, even if you chose to use a simple console app as the user interface!
Want to run this in a web browser? No problem! Just add an ASP.NET project to the solution and you can start building a web-based UI for the exact same app.
Really, this is a great little project to teach yourself how to write n-tier applications.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Exactly. The data representation has very little, if any, effect on the UI. For a start, I'd like to figure out how to display a column of numbers with their descriptions in something that looks like either a checkbook register or a ledger book page -- the UI will not be adding or subtracting, just showing the data. (When I say "looks like", I don't mean it looks like someone wrote in the entries by hand: I mean that the display has columns and rows and the background color is a light green.).
Thanks,
Owen
|
|
|
|
|
DataGridView would do this easily. If you wanted colored alternating rows, that'd take a bit more work.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
OK. Thanks. I'll check out DataGridView.
|
|
|
|
|
My platform: VB.NET Windows Form
My case:
DOS program mon.exe runs on command window. This program enables you to monitor a flash ram. To monitor ipno 10.0.0.90 from 4000 port you just type:
C:\>mon 10.0.0.90 4000
Which can also be performed by VB.NET with command line:
Shell("mon " & ipno & " 4000", AppWinStyle.MaximizedFocus, True)
After mon.exe executes you are monitoring the ipno and a command line occurs.
10.0.0.90\>
To change the ipno you type SET IP newIP:
10.0.0.90\> SET IP 10.0.0.44
At this point, I am not able to execute SET IP command line. I tried to create batch file but it didn't work. I tried :
System.Console.Write("SET IP" & newIP) 'but it didn't work.
I am trying to develop a Windows Form to change the ip of a special system. Can anyone help me? Should I work on batch files? Is there a way to type on command window while shell command is executing?
I will be really appriciated for an answer.
B.B.
|
|
|
|
|
You can't type into a Shell'd process from VB.NET. You have to create your own process using the Process class. You can then redirect the StandardInput stream so you can have your code type in the command window. You can't do this using Shell.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Thanks. I will try. It will take some time. But will post the result.;)
B.B.
|
|
|
|
|
Is it right below? To start monitoring? It does not seem to work properly.
Dim objProcess As New Process
' Start the Command and redirect the output
objProcess.StartInfo.UseShellExecute = False
objProcess.StartInfo.RedirectStandardOutput = True
objProcess.StartInfo.CreateNoWindow = False
objProcess.StartInfo.RedirectStandardError = True
objProcess.StartInfo.WorkingDirectory = CurDir()
objProcess.StartInfo.FileName() = "mon"
objProcess.StartInfo.Arguments = " 10.0.0.99 4000"
objProcess.Start()
|
|
|
|
|
Well, you got most of it right. The only stream you didn't redirect was StandardInput. That's the keyboard stream that let's you "type" something into that console session.
Dim objProcess As New Process
With objProcess.StartInfo
.UseShellExecute = False
.CreateNoWindow = False
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.FileName = "mon"
.Arguments = " 10.0.0.99 4000"
End With
objProcess.Start()
Then to write to the console keyboard stream:
Dim kb As StreamWriter = objProcess.StandardInput
kb.WriteLine("This is being typed in the console session...")
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
When I make the StandardInput = True
objProcess.StartInfo.RedirectStandardInput = True
it opens the command window but does not give any result of mon.exe. It is not enabled also.
If StandardInput = False then mon.exe works fine but command window only lets input from keyboard.
Should I Import any dll?
B.B.
|
|
|
|
|
I have no idea. I've never sen anything like that. You might want to try it with a different executable. When I did something like this, it took some playing around to get all the redirect settings to work properly. You've got everything that I've used before working.
If your app isn't worried about what's comming back from MON, turn off the redirection of StandardError and StandardOutput.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
You are right about the code. It is working for cmd.exe or notepad.exe.. I also tested the code below tried a trick to work on cmd but second writeline command executes for cmd.exe only. I also tried SendKey.SendWait(). Do you think there is something to do with security permissions? Because when I make RedirectStandardInput = True, command window is opened but not responding(blank). That's why I am saving the debug info into a txt file.
Dim objProcess As New Process
With objProcess.StartInfo
.UseShellExecute = False
.CreateNoWindow = False
.FileName = "cmd"
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = False
End With
objProcess.Start()
Dim param(2) As String
param(0) = Chr(34) & CurDir() & "/mon.exe" & Chr(34) & " 10.0.0.99 4000 > C:\a.txt"
param(1) = "SET IP 10.0.0.98 > C:\b.txt"
kb.WriteLine(param)
kb.WriteLine(param(1))
B.B.
|
|
|
|
|
Sorry for the confusion. I couldn't make it run on notepad also. Could you post me a sample code to print on notepad other than the one below.
Dim objProcess As New Process
With objProcess.StartInfo
.UseShellExecute = False
.CreateNoWindow = True
.FileName = "notepad"
.RedirectStandardInput = True
.RedirectStandardOutput = False
.RedirectStandardError = False
End With
objProcess.Start()
Dim kb As StreamWriter = objProcess.StandardInput
kb.WriteLine("A1233 ")
I assume StreamWriter acts as keyboard but it is not. Maybe there is something small I am missing. I have to leave now. Appriciated for your help. Regards
B.B.
|
|
|
|
|
Actually, there's something large you're missing. You can't redirect Notepad because there are no streams in a Windows app to redirect. You can only do this with DOS applications that use the standard console and keyboard devices themselves, like the FTP app (FTP.EXE) that comes with Windows.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
By adding System.Environment.NewLine into writeline command, it is working now. Thanks for your help and support.
' The code below enables you to run a command
' window application (exe), and send special input
' commands to that exe application.
'Execute mon.exe file by creating a new process
Dim objProcess As New Process
With objProcess.StartInfo
.UseShellExecute = False
.CreateNoWindow = True
.WorkingDirectory = CurDir()
.FileName = "mon.exe"
.Arguments = " " & ipno & " 4000"
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = False
End With
objProcess.Start()
Dim sIn As StreamWriter = objProcess.StandardInput
Dim sOut As StreamReader = objProcess.StandardOutput
Dim s as String
sIn.AutoFlush = True
sIn.Write("SET IP " & ipnoNEW & System.Environment.NewLine)
s = sOut.ReadToEnd()
objProcess.WaitForExit(5)
If Not objProcess.HasExited Then
objProcess.Kill()
End If
MsgBox(s)
B.B.
|
|
|
|
|
barkanb wrote: sIn.Write("SET IP " & ipnoNEW & System.Environment.NewLine)
Or, you could have just used sIn.WriteLine("SET IP" & ipnoNEW) .
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I have an issue where the NumericUpDown box is diplaying too many values.
I have a simple NumericUpDownBox with the minimum set to 1 and the maximum set to 4, but can enter multiple digits on the control and I need to prevent this.
By using the KeyDown event, I am able to prevent the user from entering anything but 1, 2, 3, 4, and a few special keys like backspace, left-right arrows and delete. However, I can still press 4444444 in the display box.
I tried to coerce the code to over-write the digit with any valid key pressed. For example if "1" is pressed, then the value gets set to 1. But since it's not a .text display, the display is not getting changed.
Here is the code I am using:
Private Sub nudNUMC_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles nudNUMC.KeyDown, nudNUMD.KeyDown
Dim nud As NumericUpDown = sender
'Allowed Keycodes:
' KeyCode 49 = 1 : KeyCode 97 = NumKey 1
' KeyCode 50 = 2 : KeyCode 98 = NumKey 2
' KeyCode 51 = 3 : KeyCode 99 = NumKey 3
' KeyCode 52 = 4 : KeyCode 100 = NumKey 4
If (e.KeyCode >= 49 And e.KeyCode <= 52) Or (e.KeyCode >= 97 And e.KeyCode <= 100) Then
Select Case e.KeyCode
Case 49, 97
nud.Value = 1
Case 50, 98
nud.Value = 2
Case 51, 99
nud.Value = 3
Case 52, 100
nud.Value = 4
End Select
nud.Update()
ElseIf e.KeyCode = 8 Or e.KeyCode = 37 Or e.KeyCode = 39 Or e.KeyCode = 46 Then
'Ok
' KeyCode 8 = Backspace
' KeyCode 37 = Left Arrow
' KeyCode 39 = Right Arrow
' KeyCode 46 = Delete
Else
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
I was able to create a new project, add two NumericUpDown boxes named nudNUMC and nudNUMD with their MIN set to 1 and MAX set to 4 to isolate this scenario.
Thank you for any help and advice, Chris.
|
|
|
|
|
All you have to do is check the length of the string in the Text property of the NumericUpDown control. If it's greater than or equal to 1, don't allow the keystroke to be processed.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|