none
Tab Control is not refreshing? RRS feed

  • Question

  • I am running into a weird issue with "values not displaying correctly on a Tab control".

    This is what I am getting at FormLoad :

    You see the value in the first textbox, "0.0362"? In the SQL database, this is indeed the correct value for that particular field but all the other textboxes's values are wrong. It is as though the control has "copied" or taken its source from the same database item.

    This is a code excerpt which shows how I load the value from the Data Table :

    If Me.SQLDataSet.Color.Rows(RecordNo)("nm380").Equals(DBNull.Value) Then  
                tb380.Text = "0.000"
    Else
                tb380.Text = Me.CIMSQLDataSet.Color.Rows(RecordNo)("nm380")
    End If
    If Me.
    SQLDataSet.Color.Rows(RecordNo)("nm390").Equals(DBNull.Value) Then
                tb390.Text = "0.000"
    Else
                tb390.Text = Me.
    SQLDataSet.Color.Rows(RecordNo)("nm390")
    End If
    If Me.
    SQLDataSet.Color.Rows(RecordNo)("nm400").Equals(DBNull.Value) Then
                tb400.Text = "0.000"
    Else
                tb400.Text = Me.
    SQLDataSet.Color.Rows(RecordNo)("nm400")
    End If

    As soon as I navigate to the next record, the data displays correctly!
    And when I navigate back to the first record, the data is also correct, all of a sudden :


    • Edited by roger.breton Tuesday, March 31, 2020 12:24 PM Made the code simpler to read
    Tuesday, March 31, 2020 12:20 PM

Answers

  • As I kept "debugging" I ran into additional but related issues, such as, when Updating rows, with data from the Textbox controls, as you can see below :

    I was getting "Empty" or "Nothing" or "Nulls" from the Textboxes -- ALL of them -- which did not make sense. Also, during another run, I got the following :

    Now, all the TextBoxes carried the "same" values, i.e. "0.0362" (tb380.Text, tb390.Text an so on).

    So, the solution which is far from elegant, for now, was to extract these Textboxes *out* of the Tab control. And that worked perfectly well? I decided later to delete the Tab control all together from the interface and stay away from that kind of control, sadly :(

    • Marked as answer by roger.breton Tuesday, March 31, 2020 2:04 PM
    Tuesday, March 31, 2020 2:04 PM

All replies

  • When displaying data from a DataTable (looks like from a DataSet) you should look at using a BindingSource with the DataSource set to the DataTable. In some cases in tangent with binding class using Format and Parse events.

    High level (without using the Binding class, just a BindingSource) DataBind each TextBox to a column in the DataTable with the BindingSource as the DataSource. Set the DataBinding before setting the BindingSource.DataSource.

    The way to format is with the Binding class format event.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 31, 2020 12:47 PM
    Moderator
  • Thank you Karen for the suggestion.
    A) I confess I'm not that familiar with "Binding" techniques?
    B) I was thinking that this might a possible solution BUT I don't know enough about the consequences of "Binding" Textbox controls to a DataSource to tell, for sure, whether this approach will work for my application:

    - The "data source" for these Textboxes is not *always* records from a data row;
    - Sometimes, the user will manually import data from the Clipboard;
    - At that time, my application parses the data and dispatches it to their respective "columns" or "fields" or "Textboxes". You see?

    That's what makes me hesitate. Once I "bind" each of those Textboxes to my SQL data, will I effectlvely "lose" manual access to change their values in code? 

    Tuesday, March 31, 2020 12:54 PM
  • I made a quick test... As shown below:

    I selected the TextBox called "tb510" and bound it to the existing column of my "ColorBindingSource" (hoping I'm doing this right). Then I launched the application, on FormLoad, the data was still incorrect. Then I created a "New" record, went into that particular Textbox, typed some value (e.g. 0.1245) and saved the record. When I navigated back to the record, the 0.1245 was still there!

    So it would appear VisualBasic does not "mind" my manual code intervention, even though the TextBox control is "bound" to a DataSource.

    Sorry for being such a newbie...

    Tuesday, March 31, 2020 1:05 PM
  • Hi

    Here is an example using a TabControl Page, DataTable, BindingSource, BindingNavigator and series of Labels to display data. This is a stand alone example.

    ' Form1 with TabControl1, Page2
    ' containing BindingNavigator1,
    ' Labels 1 to 6 (or match
    ' fields in DB) 
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("freddy")
      Dim BS As New BindingSource
      Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestData.xml")
    
      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ' save data on exit
        dt.WriteXml(path)
      End Sub
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim r As New Random
        With dt
          .Columns.Add("One", GetType(String))
          .Columns.Add("Two", GetType(String))
          .Columns.Add("Three", GetType(String))
          .Columns.Add("Four", GetType(String))
          .Columns.Add("Five", GetType(String))
          .Columns.Add("Six", GetType(String))
    
          If IO.File.Exists(path) Then
            ' test data file found
            ' so load it
            dt.ReadXml(path)
          Else
            ' no data file found
            ' so create some test
            ' data
            For i As Integer = 0 To 9
              .Rows.Add(r.NextDouble().ToString("0.0000"), r.NextDouble().ToString("0.0000"), r.NextDouble().ToString("0.0000"), r.NextDouble().ToString("0.0000"), r.NextDouble().ToString("0.0000"), r.NextDouble().ToString("0.0000"))
            Next
          End If
        End With
        BS.DataSource = dt
    
        Label1.DataBindings.Add("Text", BS, "One")
        Label2.DataBindings.Add("Text", BS, "Two")
        Label3.DataBindings.Add("Text", BS, "Three")
        Label4.DataBindings.Add("Text", BS, "Four")
        Label5.DataBindings.Add("Text", BS, "Five")
        Label6.DataBindings.Add("Text", BS, "Six")
        BindingNavigator1.BindingSource = BS
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Tuesday, March 31, 2020 1:25 PM
  • Thank you Lester :-)

    I must confess I started out by using a BindingNavigator and later switched to customs  buttons to which I attached custom "record navigation" code... I'm probably going wrong about this but, for the most part, it works. I may come back to a BindingNavigator later... Your code make it seem "simple" :-)

    Tuesday, March 31, 2020 1:29 PM
  • As I kept "debugging" I ran into additional but related issues, such as, when Updating rows, with data from the Textbox controls, as you can see below :

    I was getting "Empty" or "Nothing" or "Nulls" from the Textboxes -- ALL of them -- which did not make sense. Also, during another run, I got the following :

    Now, all the TextBoxes carried the "same" values, i.e. "0.0362" (tb380.Text, tb390.Text an so on).

    So, the solution which is far from elegant, for now, was to extract these Textboxes *out* of the Tab control. And that worked perfectly well? I decided later to delete the Tab control all together from the interface and stay away from that kind of control, sadly :(

    • Marked as answer by roger.breton Tuesday, March 31, 2020 2:04 PM
    Tuesday, March 31, 2020 2:04 PM
  • In regards to losing control, no, there are events for DataTable containers. In regards to the clipboard, there are ways to work with this data too.

    In short all you have concerns about there are ways to handle them. Not going to say it just a little code, it's not. simple, small code solutions sometimes are great while other times not the case. One of the things that's great about C# and the underlying Framework is freedom to expand past what developers usually don't see up front.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 31, 2020 2:14 PM
    Moderator
  • In regards to the exception shown in short use IsNothing or String.IsNullOrWhiteSpace for asserting there is data. When working with numeric types e.g. Single use Tryparse (with two overloads) which passing a string and a numeric variable will return true if the string can be converted to that type or not. An efficient way to do this is with a language extension method.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 31, 2020 2:18 PM
    Moderator
  • Karen,

    I have to say that this application is a "port" of a Microsoft Access "application". Importing the Access Tables into SQL was a little painful but it worked. I chose to continue using VisualBasic, as my programming language, as opposed to using c#,  because of all the VBA code I wrote over the last two years or so, inside that Access database, which add functionality beyond the base form controls and properties. 

    I gave serious thought to using c# as my development language for this project, at the onset, believe me, but I figured the opportunity to re-use all that VBA code was too good to pass.

    If I had the chance (probably never will?), I would be curious to rewrite the part of the code (may not be all that long?) that deal with that pesky Tab control using c#? In other words, just write a new WinForm project and throw a Tab control, with the same collection of TextBoxes to see what kind of havoc I would run into? I suspect there's something BIG I'm missing in my understanding of Tab controls which is responsible for the unfortunate behavior I ran into -- I can't know everything and I'm a victim of my programming ignorance, I know, although I try very hard, with my humble knowledge. 

    Thank you so much for taking the time to help :-)

    Tuesday, March 31, 2020 2:25 PM
  • Karen,

    I have to say that this application is a "port" of a Microsoft Access "application". Importing the Access Tables into SQL was a little painful but it worked. I chose to continue using VisualBasic, as my programming language, as opposed to using c#,  because of all the VBA code I wrote over the last two years or so, inside that Access database, which add functionality beyond the base form controls and properties. 

    I gave serious thought to using c# as my development language for this project, at the onset, believe me, but I figured the opportunity to re-use all that VBA code was too good to pass.

    If I had the chance (probably never will?), I would be curious to rewrite the part of the code (may not be all that long?) that deal with that pesky Tab control using c#? In other words, just write a new WinForm project and throw a Tab control, with the same collection of TextBoxes to see what kind of havoc I would run into? I suspect there's something BIG I'm missing in my understanding of Tab controls which is responsible for the unfortunate behavior I ran into -- I can't know everything and I'm a victim of my programming ignorance, I know, although I try very hard, with my humble knowledge. 

    Thank you so much for taking the time to help :-)

    In regards to C#, my bad, was thinking I was in the C# forum, I bounce between both forums so replace C# with VB.NET.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 31, 2020 2:42 PM
    Moderator
  • I regards to missing something about the tab control. When working with just code or a specific type of control and code it's always best to learn them aside from a project as there are no rules other than the ones you make for learning rather than business requirements

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 31, 2020 2:44 PM
    Moderator
  • I agree wholeheartedly.
    Tuesday, March 31, 2020 2:46 PM
  • Thank you Lester :-)

    I must confess I started out by using a BindingNavigator and later switched to customs  buttons to which I attached custom "record navigation" code... I'm probably going wrong about this but, for the most part, it works. I may come back to a BindingNavigator later... Your code make it seem "simple" :-)

    Hi

    OK dokey

    Just for completeness ................ as at the time, I didn't know you were using TextBoxes and I used Labels. Here is an amended version using TextBoxes. Any of the values can ve changed and will persist on saving/loading the data (mine in an XML file, yours in a DB), so a User changes any/all of the values then that is what would be saved/loaded.

    Another change in this version. The number of TextBoxes are created dynamically according to the DataTable columns and layout controlled with variables. Also shows NULLS and TEXT values which save/load OK.


    ' Form1 with TabControl1, Page2
    ' containing BindingNavigator1,
    ' Labels 1 to 6 (or match
    ' fields in DB) 
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("freddy")
      Dim BS As New BindingSource
      Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestData.xml")
      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ' save data on exit
        dt.WriteXml(path)
      End Sub
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim r As New Random
        MinimumSize = New Size(430, 200)
    
        BS.DataSource = dt
    
        With dt
          .Columns.Add("One", GetType(String))
          .Columns.Add("Two", GetType(String))
          .Columns.Add("Three", GetType(String))
          .Columns.Add("Four", GetType(String))
          .Columns.Add("Five", GetType(String))
          .Columns.Add("Six", GetType(String))
          .Columns.Add("Seven", GetType(String))
    
          If IO.File.Exists(path) Then
            ' test data file found
            ' so load it
            dt.ReadXml(path)
          Else
            ' no data file found
            ' so create some test
            ' data
            For i As Integer = 0 To 9
              .Rows.Add(r.NextDouble().ToString("0.0000"), r.NextDouble().ToString("0.0000"), r.NextDouble().ToString("0.0000 "), r.NextDouble().ToString("0.0000 "), r.NextDouble().ToString("0.0000 "), r.NextDouble().ToString("0.0000 "), r.NextDouble().ToString("0.0000 "))
            Next
          End If
        End With
    
    
        ' ========================
        ' to layout Textboxes in
        ' a defined array
    
        ' how many TB across
        Dim across As Integer = 4
    
        ' starting y pos (to skip
        ' the BindingNavigator)
        Dim sy As Integer = 36
    
        Dim g As Integer = 8
        Dim x As Integer = g
        Dim y As Integer = 8
        Dim w As Integer = 90
        For Each c As DataColumn In dt.Columns
          Dim textbox As New TextBox
          With textbox
            .Width = w
            .Font = New Font("Arial", 14)
            .Name = c.ColumnName
            .TextAlign = HorizontalAlignment.Right
            .DataBindings.Add("Text", BS, .Name)
            .Location = New Point(x, sy + y)
            x += w + g
            If x > across * (w + g) Then
              x = g
              y += textbox.Height + g
            End If
          End With
          TabControl1.TabPages(1).Controls.Add(textbox)
        Next
        ' ========================
        BindingNavigator1.BindingSource = BS
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, March 31, 2020 3:14 PM Removed duplicated pasted text
    Tuesday, March 31, 2020 3:13 PM
  • I may not be out of the woods yet.... 
    Looks like the 0.0362 Textbox value has caused "collateral damages".
    This Product Code item, for instance, used to have "valid" entries, in the Spectral Data section, but it's all 0.0362 values, now? I checked the database using SSMS and, indeed, it's too late, all the original data is gone. 

    I'll have to do additional testing to make triple sure I'm not losing any data anymore.

    Tuesday, March 31, 2020 4:04 PM
  • Les, when you get a minute, could you post what that XML file would look like? So I can test it with your code?
    Tuesday, March 31, 2020 5:22 PM
  • Les, when you get a minute, could you post what that XML file would look like? So I can test it with your code?

    Hi

    Of course I will. Posted below. However, the code doesb't actually need a file to run because it will create some random data if no file is found and save the file on exit, and on subsequent runs, will load that

    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
      <freddy>
        <One>0.6305</One>
        <Two>0.3282</Two>
        <Three>0.0114 </Three>
        <Four>0.6443 </Four>
        <Five>0.0941 </Five>
        <Six>0.7036 </Six>
        <Seven>0.4556 </Seven>
      </freddy>
      <freddy>
        <One>0.6188</One>
        <Two>0.5216</Two>
        <Three>0.6734 </Three>
        <Four>0.2162 </Four>
        <Five>0.8658 </Five>
        <Six>0.7858 </Six>
        <Seven>0.5408 </Seven>
      </freddy>
      <freddy>
        <One>0.3873</One>
        <Two>0.8362</Two>
        <Three>0.9918 </Three>
        <Four>0.9748 </Four>
        <Five>0.4942 </Five>
        <Six>0.3861 </Six>
        <Seven>0.0284 </Seven>
      </freddy>
      <freddy>
        <One>0.1992</One>
        <Two>0.1729</Two>
        <Three>0.7016 </Three>
        <Four>0.1412 </Four>
        <Five>0.5865 </Five>
        <Six>0.9893 </Six>
        <Seven>0.1096 </Seven>
      </freddy>
      <freddy>
        <One>0.5829</One>
        <Two>0.5371</Two>
        <Three>0.8573 </Three>
        <Four>0.7810 </Four>
        <Five>0.9834 </Five>
        <Six>0.8895 </Six>
        <Seven>0.2899 </Seven>
      </freddy>
      <freddy>
        <One>0.1949</One>
        <Two>0.0267</Two>
        <Three>0.3679 </Three>
        <Four>0.6165 </Four>
        <Five>0.4244 </Five>
        <Six>0.8693 </Six>
        <Seven>0.1687 </Seven>
      </freddy>
      <freddy>
        <One>0.3620</One>
        <Two>nothing</Two>
        <Three>0.2421 </Three>
        <Four>88.5507 </Four>
        <Five />
        <Six>-110.6024 </Six>
        <Seven>0.5471 </Seven>
      </freddy>
      <freddy>
        <One>0.8318</One>
        <Two>0.2535</Two>
        <Three>0.8181 </Three>
        <Four>0.4216 </Four>
        <Five>0.6827 </Five>
        <Six>0.5907 </Six>
        <Seven>0.4313 </Seven>
      </freddy>
      <freddy>
        <One>0.1552</One>
        <Two>0.3098</Two>
        <Three>0.5031 </Three>
        <Four>0.5076 </Four>
        <Five>0.7143 </Five>
        <Six>0.3460 </Six>
        <Seven>0.0359 </Seven>
      </freddy>
      <freddy>
        <One>0.9845</One>
        <Two>0.8161</Two>
        <Three>0.4352 </Three>
        <Four>0.8824 </Four>
        <Five>0.8962 </Five>
        <Six>0.2509 </Six>
        <Seven>0.1924 </Seven>
      </freddy>
    </DocumentElement>

    data.


    Regards Les, Livingston, Scotland

    Tuesday, March 31, 2020 6:40 PM