-
Sep 25th, 2022, 05:45 AM
#1
Thread Starter
Addicted Member
[RESOLVED] TabControl add controls from text file
Hello,
Can someone help me out cause i cannot find good information on google about this.
How can i create this stuff:
When form loads to create in TabControl tabs names from the text file.
Example:
Inside Text.txt i have information like:
Name1|Line1
Name2|Line2
Name3|Line3
so it will create in tabcontrol 3 tabs with names "Name1","Name2","Name3"
and for each control created to create inside datagridview
Can someone help me out?
Last edited by luckydead; Sep 25th, 2022 at 06:10 AM.
-
Sep 25th, 2022, 06:29 AM
#2
Re: TabControl add controls from text file
What have you done and where are you stuck? Surely you can get a list of files from a folder, but I don't see any evidence of that. Can you read the contents of a file? You've asked multiple questions in one here, which tell sme that you haven't actually given the problem much thought. ALWAYS break the problem down into the smallest parts you can and then address each part individually. Implement all the parts you can for yourself and then ask a specific question about a specific part, showing us what you already have and how the question fits into the whole. The fact that beginners try to solve multiple problems as though they are one is the main reason they can't solve any. When you divide and conquer, you'll be able to do some, much or even all of the work yourself. You then only need to ask us about the stuff you genuinely can't do and your questions will be far more focused.
-
Sep 25th, 2022, 06:41 AM
#3
Thread Starter
Addicted Member
Re: TabControl add controls from text file
i have started with this
Code:
Dim tab As New TabPage() With {
.Text = IO.Path.GetFileName(My.Application.Info.DirectoryPath & "\Lines.txt")
}
Guna2TabControl1.TabPages.Add(tab)
Guna2TabControl1.SelectedTab = tab
Dim rich As New RichTextBox With {
.Parent = tab,
.Dock = DockStyle.Fill
}
rich.LoadFile(My.Application.Info.DirectoryPath & "\Lines.txt", RichTextBoxStreamType.PlainText)
but its not working correctly
simply say to read Lines.txt information and create tabs in tabcontrol with that names, then add datagridview for each tab inside
-
Sep 25th, 2022, 06:55 AM
#4
Re: TabControl add controls from text file
Let's think about the logic first and then write code to implement that logic, rather than writing code with no real idea what it's supposed to do.
You have a file that contains multiple lines and for each line in that file you want to add a TabPage. Hey look! That's almost paseudo-code because it contains the words "for each" and VB has a For Each loop that lets you do something for each item in a list. Clue?
-
Sep 25th, 2022, 07:01 AM
#5
Lively Member
Re: TabControl add controls from text file
I don't know anything about using datagridview so there I can't help you. But instead of a textfile I would use excel. It makes it a lot easier to locate the right text. Also with a divider like | you need to check each character to see if it is the | where is located. With excel it is already seperated.
But if you want to keep using a textfile I think it would be easier to everything on a different line.
Name1
Line1
Name2
Line2
And if the names and lines doesn't repeat in the same order (like 0/2 or more lines) I would add a | in front of the name. That way you only need to check 1 character to see if it is a name or line.
Here is a link for reading text files. https://www.homeandlearn.co.uk/NET/nets8p3.html
-
Sep 25th, 2022, 07:05 AM
#6
Re: TabControl add controls from text file
The Form has a Controls collection, as does a TabPage, a Panel, and a GroupBox. The method for adding dynamic Controls to these containers is the same…
Code:
[container].Controls.Add([dynamic Control])
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 25th, 2022, 07:22 AM
#7
Thread Starter
Addicted Member
Re: TabControl add controls from text file
something like that?
Code:
Dim Lines() As String = IO.File.ReadAllLines(My.Application.Info.DirectoryPath & "\Lines.txt")
For Each line As String In Lines
Dim LineParts() As String = Strings.Split(line, "|", 2) 'Split the current line into two chunks at the first =
If LineParts.Count < 2 Then
Continue For 'No = in the line, so skip it
Else
Dim Key As String = LineParts(0)
Dim Value As String = LineParts(1) 'This contains the part after the =
'Do whatever you want with the value here. e.g.
Guna2TabControl1.TabPages.Add(Key)
End If
Next
Now i got stuck how to add the datagridview to each one of them
Last edited by luckydead; Sep 25th, 2022 at 08:12 AM.
-
Sep 25th, 2022, 08:18 AM
#8
Re: TabControl add controls from text file
You can’t reference a Control by a String. At least, not that way.
If you’re interested, Google - Reflection
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 25th, 2022, 08:19 AM
#9
Thread Starter
Addicted Member
Re: TabControl add controls from text file
the code above works fine it creates tabs with names from the txt file, but i got stuck on how to create the datagridview to each one tab created (mostly nobody give even 1 code example or support me a little - maybe too lazy people here to support people? ) - come one give a hand and support me a little
-
Sep 25th, 2022, 08:30 AM
#10
Re: TabControl add controls from text file
You just need to loop through your strings, then use a select case statement to identify the type of Control to add. I see now what you did with the tabpage, but that won’t work with the controls collection
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 25th, 2022, 08:46 AM
#11
Thread Starter
Addicted Member
Re: TabControl add controls from text file
Originally Posted by .paul.
You just need to loop through your strings, then use a select case statement to identify the type of Control to add. I see now what you did with the tabpage, but that won’t work with the controls collection
Example??
-
Sep 25th, 2022, 09:37 AM
#12
Re: TabControl add controls from text file
You first… Example of your text file.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 25th, 2022, 09:44 AM
#13
Re: TabControl add controls from text file
Code:
Select Case aString
Case “DataGridView”
Dim dgv As New DataGridView
‘ set properties
tab.Controls.Add(dgv)
Case “TextBox”
Dim tb As New TextBox
‘ set properties
tab.Controls.Add(tb)
End Select
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 25th, 2022, 09:54 AM
#14
Thread Starter
Addicted Member
Re: TabControl add controls from text file
Code:
Dim Lines() As String = IO.File.ReadAllLines(My.Application.Info.DirectoryPath & "\Lines.txt")
For Each line As String In Lines
Dim LineParts() As String = Strings.Split(line, "|", 2) 'Split the current line into two chunks at the first =
If LineParts.Count < 2 Then
Continue For 'No = in the line, so skip it
Else
Dim Key As String = LineParts(0)
Dim Value As String = LineParts(1) 'This contains the part after the =
'Do whatever you want with the value here. e.g.
Dim dynamicTab As New TabPage(Key)
Dim btn As New DataGridView With {
.Name = "btnButton",
.Text = "Dynamic Button",
.BackColor = Color.SeaGreen
}
btn.Columns.Add("Header1", "Header2")
btn.Rows.Add("Name Line1", "Line1")
dynamicTab.Controls.Add(btn)
Me.Guna2TabControl1.TabPages.Add(dynamicTab)
End If
Next
now it creates datagridview to all tabs created, but i think someone can provide better code solution?
-
Sep 26th, 2022, 10:47 AM
#15
Re: TabControl add controls from text file
Change this:
Code:
If LineParts.Count < 2 Then
Continue For 'No = in the line, so skip it
Else
Dim Key As String = LineParts(0)
To this:
Code:
If LineParts.Count >= 2 Then
Dim Key As String = LineParts(0)
-tg
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|