Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 9th, 2012, 10:48 AM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Using the "Enter" Event

Hi Rod,

I have another question. In my application, I have a button, a text box and TreeView Control where I will create a tree view list. With the button control I can successfully add a node to the Tree View Control. The contents of that node is what is entered in TextBox1. In addition to being able to using a button to add the node to the Tree View Control, I want to be able to just hit the "Enter" button and have it activate btnLevelOne. I am trying to do this using the Enter Event but it seems to have no affect. My code is below. Can you tell me where I am going wrong?

Thanks,


Gary


Code:
Public Class CreateIndex
        'Create a Root Level Node
        Private Sub btnLevelOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLevelOne.Click

            Dim NewNodes As String
            NewNodes = TextBox1.Text
            TreeView1.Nodes.Add(NewNodes)

            TextBox1.Clear()

        End Sub
        Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter

            btnLevelOne.Focus()
            SendKeys.Send("{Enter}")

        End Sub
    End Class
Reply With Quote
  #2 (permalink)  
Old March 9th, 2012, 11:40 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Gary,

I'm not sure why this isn't working. Possibly focus has not finished moving to the button before you send {Enter}. You could try to use Application.DoEvents to slow the program down enough so focus moves.

SendKeys seems to get harder to use with each new release. I suspect Microsoft isn't really trying to maintain it.

What I would do is making the button be the form's AcceptButton so the form fires it if you press Enter. Then you don't need the Enter event handler.

If that doesn't work, I would move the body of the button's event handler into a new subroutine. Then you can have the button and the TextBox's Enter command both call the new subroutine. That way they both call the code directly and you don't need to duplicate the code.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #3 (permalink)  
Old March 9th, 2012, 10:45 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Text Files

Hi Rod,

Yes, Thank you. Changing the button to an Accept button solved the problem.

I have another question though. I need to write certain words to a text file so that they are inserted in place within the textfile. The text file would not have any duplicate words. For example, if the list was:

car
truck
bicycle
boat

and I wanted to automatically insert the word "SUV" after "truck", how could I do it? I know I can loop down the list and find a particular word, but I am uncertain how to insert "SUV" directly after "truck".

Thank you for your ongoing help.


Gary
Reply With Quote
  #4 (permalink)  
Old March 12th, 2012, 11:12 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

(Please start a new thread for new topics so they're easier for others to find in the discussion.)

Sorry but you can't do that.

A text file is just a stream of bytes all adjacent on the disk so you can't insert or remove bytes in the middle.

That doesn't really hurt you here, though. Just read the file into an array (use ReadAllLines), add the new items where you want them, and rewrite the file (use WriteAllLines). It may sound inefficient but it should be very fast.

In this case, adding the SUV in the middle of the list is probably going to be the trickiest part. You may want to convert the array into a list, insert the new entry, and then use WriteAllLines to rewrite the file. WriteAllLines can take a list as a parameter instead of an array so you don't need to convert back.

If you want to ensure there are no duplicates, then you may want to look at LINQ.

I hope that helps.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
Difficulties with "web.config" and "ASPNETDB" CFRham BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 2 July 3rd, 2010 10:19 AM
ttwo or more events on a "mouseover" event ? liamfitz Javascript 0 January 14th, 2010 01:11 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM



All times are GMT -4. The time now is 10:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.