Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any method to display only single line of textfile which is loaded in textbox when cursor/key up&down is done and hide other lines while current line is highlighted.

Example:
Two lines of textfile in textbox are:

abcdef
1234

when cursor is pointed on line containing "abcdef" then line containing "1234" which is second line of textfile should be hide and when cursor is on second line then first and other lines of textbox should be hide

What I have tried:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ReadSpciFile As Array
Dim line As String
ReadSpciFile = File.ReadAllLines("C:\Users\OM\Desktop\1 (3).txt")
line = ReadSpciFile(0)
RichTextBox1.Text = line

End Sub

above code is now showing first line of textfile due to this "line = ReadSpciFile(0)"
but i am unable to load/see other lines when i keydown/keyup event
Posted
Updated 18-Jun-18 1:03am
v6
Comments
Maciej Los 11-Jun-18 4:44am    
Wrong idea!

Think of it... If the second line is hidden how cursor can be moved over this line?
shaileshshinde 11-Jun-18 4:48am    
if not hidden then the second line can be of such forecolor which is invisible.
and when cursor pointed on second line then the second line cursor should get highlighted with visible forecolor

or any other idea which you suggest to achieve this kind of behaviour?
Maciej Los 11-Jun-18 5:17am    
You already answered your question yourself ;)
shaileshshinde 11-Jun-18 5:36am    
Yes right. But for coding this method I am not getting coding logic.
Ralf Meier 15-Jun-18 8:42am    
You have the wrong approach.
This Behaviour is not the Behaviour which could (or should) be realized with a (single) TextBox - better you create a customized Control, for example a UserControl which consists of 2 TextBoxes - now you are able to really switch between 2 both lines.

1 solution

Refering to the discussion until now I would suggest the Following :
- with Loading your Form (for example) you load the Data of your Textfile into an Array (like allready posted from you).
Also assign the 1st Array-Element to your Textbox
Build an Index-Variable (Integer) which points to the line which is allready to be seen
- now hook the KeyUp-Event from the Textbox to a method where you decrease the Index-Variable (and check it against the Array-Bounds).
Assign the Array-Element which is indexed by this Variable to your Textbox
- now hook the KeyDown-Event from the Textbox to a method where you increase the Index-Variable (and check it against the Array-Bounds).
Assign the Array-Element which is indexed by this Variable to your Textbox

VB
Dim ReadSpciFile As array
Dim Index as integer = 0

Private Sub Form1_Load (sender As Object, e As EventArgs) Handles MyBase.Load
Dim line As String
ReadSpciFile = File.ReadAllLines("C:\Users\OM\Desktop\1 (3).txt")
line = ReadSpciFile(0)
RichTextBox1.Text = line
End Sub

private sub DecreaseLine (sender As Object, e As EventArgs) Handles RichTextBox1.KeyUp
Index -= 1
if Index < 0 then Index = 0
line = ReadSpciFile(Index)
RichTextBox1.Text = line
end sub

private sub IncreaseLine (sender As Object, e As EventArgs) Handles RichTextBox1.KeyDown
Index += 1
if Index >= ReadSpciFile.length then Index = ReadSpciFile.length
line = ReadSpciFile(Index)
RichTextBox1.Text = line
end sub


Extended suggestion :
VB
Dim ReadSpciFile As array
Dim Index1 as integer = 0
Dim Index2 as integer = 0

Private Sub Form1_Load (sender As Object, e As EventArgs) Handles MyBase.Load
Dim line As String
ReadSpciFile = File.ReadAllLines("C:\Users\OM\Desktop\1 (3).txt")
line = ReadSpciFile(0)
RichTextBox1.Text = line
End Sub

private sub DecreaseLine1 (sender As Object, e As EventArgs) Handles RichTextBox1.KeyUp
Index1 -= 1
if Index1 < 0 then Index1 = 0
line = ReadSpciFile(Index1)
RichTextBox1.Text = line
end sub

private sub IncreaseLine1 (sender As Object, e As EventArgs) Handles RichTextBox1.KeyDown
Index1 += 1
if Index1 >= ReadSpciFile.length then Index1 = ReadSpciFile.length
line = ReadSpciFile(Index1)
RichTextBox1.Text = line
end sub

private sub DecreaseLine2 (sender As Object, e As EventArgs) Handles RichTextBox2.KeyUp
Index2 -= 1
if Index2 < 0 then Index2 = 0
line = ReadSpciFile(Index2)
RichTextBox2.Text = line
end sub

private sub IncreaseLine2 (sender As Object, e As EventArgs) Handles RichTextBox2.KeyDown
Index2 += 1
if Index2 >= ReadSpciFile.length then Index2 = ReadSpciFile.length
line = ReadSpciFile(Index2)
RichTextBox2.Text = line
end sub
 
Share this answer
 
v2
Comments
shaileshshinde 18-Jun-18 7:24am    
It works .But has three issues:

1) It shows only first two lines of textfile
2)When once keydown is done and second line is pointed and if we want to see again the first line then following behaviour occurs:
Immediately second line gets popped up as if it is default line to be showed
3)the lines are showed only in same textboxes. I want to show it in different textboxes with same hide and show behaviour as in same textbox as per your above code.
Ralf Meier 18-Jun-18 10:06am    
OK ... most of your Comments must be solved by yourself - using the Debugger.

to 1) How many Lines has your Textfile ?
At the end of Form1_Load (set a Breakpoint there) - what is the content and the Size of the Array 'ReadSpciFile' ?
to 2) Here you must look with the Debugger what happens
to 3) I don't understand what you want to do here. Please explain !!!
Basicly you need to have as much different Index-Variables as you want to use Textboxes with this Behaviour (each Textbox must have it's own Index-Pointer).
Also you have to hook the Events from each Textbox and guide it to methods as posted above. But notice : if those different Textboxes should act sperately you must have different methods for each Textbox and each Event. As a suggestion for this I add a sample to my code above ...
shaileshshinde 4-Jul-19 1:15am    
textbox contains "n" number of lines.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900