select method:I don't understand what is .select(1,0) 1? 0? select?

Private Sub mnuOpenItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpenItem.Click

Dim AllText, LineOfText As String

OpenFileDialog1.Filter = "Text files (*.TXT)|*.TXT"

OpenFileDialog1.ShowDialog() 'display Open dialog box

If OpenFileDialog1.FileName <> "" Then

Try 'open file and trap any errors using handler

FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)

Do Until EOF(1) 'read lines from file

LineOfText = LineInput(1)

'add each line to the AllText variable

AllText = AllText & LineOfText & vbCrLf

Loop

lblNote.Text = OpenFileDialog1.FileName 'update label

txtNote.Text = AllText 'display file

txtNote.Select(1, 0) 'remove text selection:WHAT DOES IT MEAN Whwat is the selection and what does mean 1,0

txtNote.Enabled = True 'allow text cursor

mnuCloseItem.Enabled = True 'enable Close command

mnuOpenItem.Enabled = False 'disable Open command

Catch

MsgBox("Error opening file.")

Finally

FileClose(1) 'close file

End Try

End If

End Sub



Answer this question

select method:I don't understand what is .select(1,0) 1? 0? select?

  • Shahzad Godil

    From help:

    "You can programmatically move the caret within the text box by setting the start parameter to the position within the text box where you want the caret to move to and set the length parameter to a value of zero (0). The text box must have focus in order for the caret to be moved."

    So what it does is to set start position of caret (text cursor) to 1 (beginning) and select 0 characters (no selection). However it is unnecessary there (there are other unnecessary code like a loop). Instead you could do something like this:

    OpenFileDialog1.Filter = "Text files (*.TXT)|*.TXT"
    OpenFileDialog1.ShowDialog() 'display Open dialog box
    If OpenFileDialog1.FileName <> "" Then
    lblNote.Text = OpenFileDialog1.FileName 'update label
    Try
    txtNote.Text = System.IO.File.ReadAllText(OpenFileDialog1.FileName)
    Catch
    MsgBox("Error opening file.")
    End Try
    End If


  • Rahul Singla

    I'm not sure where you got this code from but this will not work and the select statement will have no effect.

    What it looks like is that you load the contents of the file into the textbox and the idea would be to enable the textbox with the carat in the textbox at the start. However this would not work with the select statement here as the textbox control (txtNote) does not specifically have the focus and that is required for the select statement to work.

    That said, you could simply put the following statement into you code to ensure the control gets focus as the select statement implies that it requires focus to show any effect.

    TxtNote.Focus

    But I'd personally eliminate the two lines altogether, or replace with a single line

    TxtNote.SelectionStart = 0

    When this will come into play is if you carry out the action and tab through the controls to give the textbox focus - which will result in the (Carat) being at the start of the textbox. Otherwise if you click on the textbox the carat would be around the position you click in the textbox.

    If you dont have any lines in then the carat will be at the end of the textbox contents.

    Either way, it really only comes into play when your textbox gets the focus (either programmatically or through setting by tabbing)



  • Kyle Key

    Thank you all for your help,i understood the select method and the code is not a good code.i found this code on Step by step Visual Basic.Net book by microsoft press.
  • select method:I don't understand what is .select(1,0) 1? 0? select?