Results 1 to 15 of 15

Thread: [RESOLVED] TabControl add controls from text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    191

    Resolved [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.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,078

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    191

    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

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,078

    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?

  5. #5
    Lively Member
    Join Date
    Aug 2014
    Posts
    65

    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,003

    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])

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    191

    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.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,003

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    191

    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

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,003

    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

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    191

    Re: TabControl add controls from text file

    Quote Originally Posted by .paul. View Post
    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??

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,003

    Re: TabControl add controls from text file

    You first… Example of your text file.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,003

    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

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    191

    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?

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,000

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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
  •  



Click Here to Expand Forum to Full Width