Hey All!

I'm working on a project at the moment, where there are a variety of sections. Each section has its own window and WPF controls. Each window has a tab control, with tabs to 'View', 'Add', 'Edit' and 'Delete' records. The 'View' tab has a ListView which is populated with records from a database (using LINQ to SQL).

From the 'View' tab, users must have the abilitye to select a record from the ListView to edit or delete. By selecting the record and clicking on the relevant button ('Edit' or 'Delete') the relevant tab will be selected and the controls in that tab will be populated with the details of the selected record.

I know how to change the tabs by changing the TabControl selected index property, but I'm not sure how to pass the information across to the relevant tab item. In VB I used a procedure in the first form to pass the variables to the other windows form:

Using f As New FrmDetails(action)
                ' display the form
                f.ShowDialog()
            End Using

And then the following procedure would be in the code of the form receiving the information

' recieve passed through variable
    Sub New(ByVal var As String)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        action = var ' var is the variable passed from the previous form
    End Sub

I assume something similar could be done in C# but I'm not sure how to proceed because I don't see how this would work with the TabControl.

The only way I could see to do it would be to have a gloabl variable to store the object that must be passed, with a default value of null and then each time the tabControl index is changed I could fill the form based on this variable but that doesn't seem like a very efficient method.

Any ideas?
Regards
Laura

P.S. Sorry about the VB code snippets in C# syntax. I wasn't sure how to change it to VB.Net. If anyone can tell me, I'd appreciate that as well :D

Dani AI

Generated

For the scenario described by (a TabControl with View/Add/Edit/Delete and a ListView populated via LINQ-to-SQL), the cleanest approach is to avoid globals and use data binding: expose a single shared ViewModel for the window, bind the ListView.SelectedItem to a property on that ViewModel, and have the Edit/Delete tab controls bind to that same property (or to an editable copy). That keeps UI state explicit, works nicely with WPF, and removes the need to "pass" variables manually between tabs.

A minimal pattern (conceptual):

<ListView ItemsSource="{Binding Records}"
          SelectedItem="{Binding SelectedRecord, Mode=TwoWay}" />

<!-- inside the Edit tab -->
<TextBox Text="{Binding SelectedRecord.Name, Mode=TwoWay}" />
public class MainViewModel : INotifyPropertyChanged
{
    private MyEntity _selectedRecord;
    public MyEntity SelectedRecord
    {
        get { return _selectedRecord; }
        set { _selectedRecord = value; OnPropertyChanged("SelectedRecord"); }
    }

    // INotifyPropertyChanged implementation omitted for brevity
}

Switching tabs can be done by a command or a small code-behind handler that sets the TabControl.SelectedIndex (the OP already knows how to change the index). For edit workflows, prefer copying the selected entity into an editable DTO or EditViewModel and bind the Edit tab to that copy—this prevents accidental, immediate changes to a LINQ-to-SQL tracked object and makes Cancel/Safe Save semantics straightforward. On Save, copy the edited values back or apply them to the data context and call SubmitChanges.

For larger apps use an MVVM framework or a messenger/event aggregator to decouple UI parts; for small windows a shared ViewModel plus simple commands is usually simplest and robust. This pattern answers the data-passing need without globals and keeps the UI and persistence concerns clear.

Thanks for the information, I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.