-2

I am trying to making table in Windows Form app C# which will contains Labels, text boxes, buttons & combo boxes as shown in image below:

enter image description here

I also tried DataGrid View but it does not provide such arrangement like in above image. How I can make such a table programmatically?

James Z
  • 12,209
  • 10
  • 24
  • 44
Adil Ahmed
  • 37
  • 4
  • Datagridview does not provide a way to merge cells, you must customize the paint method to build the table you need. Here are some solutions I hope will help you. - [Merge datagridview row and column cells](https://social.msdn.microsoft.com/Forums/en-US/bbda3e63-30f2-47f1-ae00-2f022796c21e/merge-datagridview-row-and-column-cells?forum=winformsdatacontrols) - [How to Merge DataGridView Cell in Winforms](https://stackoverflow.com/q/16774966/16764901) - [Merge cells in datagridview](https://stackoverflow.com/q/2063951/16764901) – Jiale Xue - MSFT Mar 10 '23 at 03:08

1 Answers1

1

How you do this is a matter of programming style, but what I see here are three good candidates for custom UserControl, header, group and effect that each contain TableLayoutPanel. The header and the added groups are inserted into a FlowLayoutPanel on the main form. The single effects are inserted into the table layout panel on the effects group user control.

This example is going to need some work, but should give you the general idea.

main

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        flowLayoutPanel.Controls.Add(
            new ScalableHeaderUserControl
            {
                Width= flowLayoutPanel.Width - SystemInformation.VerticalScrollBarWidth,
            }
        );
        flowLayoutPanel.SizeChanged += (sender, e) =>
        { 
            foreach ( Control control in flowLayoutPanel.Controls )
            {
                control.Width = flowLayoutPanel.Width - SystemInformation.VerticalScrollBarWidth;
            }
        };
    }
}

Header

header

public partial class ScalableHeaderUserControl : UserControl
{
    public ScalableHeaderUserControl()
    {
        InitializeComponent();
        labelAdd.Click += onClickAdd;
    }
    private void onClickAdd(object sender, EventArgs e)
    {
        if(Parent is FlowLayoutPanel flowLayoutPanel) 
        { 
            flowLayoutPanel.Controls.Add(new EffectsGroupUserControl
            {
                Width= flowLayoutPanel.Width - SystemInformation.VerticalScrollBarWidth,
            });
        }
    }
}

Effect Group

group

public partial class EffectsGroupUserControl : UserControl
{
    public EffectsGroupUserControl()
    {
        InitializeComponent();
        labelAdd.Click += onClickAdd;
        labelTiming.Click += onClickTiming;
        Controls.Add(_timeEditorControl);
        _timeEditorControl.KeyDown += onKeyDownTimeEditor;
    }
    private void onClickAdd(object sender, EventArgs e)
    {
        SingleEffectUserControl effect = new SingleEffectUserControl
        {
            Anchor = (AnchorStyles)0xF,
        };            
        tableLayoutPanel.Controls.Add(effect, 3, _effects.Count);
        tableLayoutPanel.SetColumnSpan(effect, 9);
        _effects.Add(effect);
    }
    private List<SingleEffectUserControl> _effects = new List<SingleEffectUserControl>();

    MaskedTextBox _timeEditorControl = new MaskedTextBox()
    {
        Mask = "00:00:000",
        Font = new Font("Segoe UI", 12),
    };
    private void onClickTiming(object sender, EventArgs e)
    {
        if (sender is Control control)
        {
            _timeEditorControl.Tag = control;
            _timeEditorControl.Location = control.Location;
            _timeEditorControl.Show();
            _timeEditorControl.BringToFront();
            _timeEditorControl.Focus();
        }
    }
    private void onKeyDownTimeEditor(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (_timeEditorControl.Tag is Control control)
            {
                control.Text = _timeEditorControl.Text;
            }
            _timeEditorControl.Hide();
        }
    }
}

Effect

single

public partial class SingleEffectUserControl : UserControl
{
    public SingleEffectUserControl()
    {
        InitializeComponent();
        comboBoxDevices.SelectedIndex = 0;
        comboBoxEffects.SelectedIndex = 0;
    }
}
IVSoftware
  • 5,732
  • 2
  • 12
  • 23
  • Thank you very much, you create the same thing which I need, but I can't understand and link the code to the outputs as in pictures, can you please clear the concept? I have to save & reload this table from text file. – Adil Ahmed Mar 11 '23 at 12:42
  • Thx for letting me know! Feel free to [clone](https://github.com/IVSoftware/controller-panel.git) this working example. Running the code and experimenting with it should make it clearer. Let me know if you have any trouble doing this. – IVSoftware Mar 11 '23 at 23:47
  • Dear IVSoftware, When I try to Build the project or when i execute the .exe file from debug folder it come with error "To run this application, you must install .NET Core, Would you like to Download it Now?" why I am seeing this error? – Adil Ahmed Mar 12 '23 at 20:45
  • I am using Visual Studio 2022 Community .NET framework >4.0, I just install .NET SDK v7.0 from .NET website, but still I am facing the same error. Do I have to download any further requirements? – Adil Ahmed Mar 12 '23 at 21:48
  • In the Windows search, run **Visual Studio Installer** and click "Modify" for VS 2022 I believe that **.NET desktop development** is the important workload here. As a point of reference I have three additional workloads checked. In decreasing importance: **,NET Multi-pllatform App UI development**, **Universal Windows Platform development**, **Desktop development with C++**. – IVSoftware Mar 12 '23 at 21:58
  • 1
    I am again re-installing visual studio community with my D: Drive (not in C: due to low space), & I have check three additional workloads as described by you, and then will update you, please stay in touch. Thanks – Adil Ahmed Mar 13 '23 at 15:06
  • Dear IVSoftware, How can I access each text boxes and combo boxes in "SingleEffectUserControl", and How can I delete the one "SingleEffectUserControl" from list by clicking cross button? Thank You – Adil Ahmed Mar 29 '23 at 10:56
  • Kindly post these new questions on a separate thread. For the first, explain _why_ you want to access these controls. For example, are you trying to reload from the text file now? Your new question might be **Save and Reload UserControls from a Text File** but search for similar answers first. Post a _small_ sample of code showing what you have tried and where you're stuck. Make sure you have read [How to Ask](https://stackoverflow.com/help/how-to-ask) because it's a guide on how to get most helpful answers. – IVSoftware Mar 29 '23 at 12:46
  • For the second question, you can handle the cross button click by using the `Parent` property to remove the user control from the `Parent.Controls` collection. You can post a new question if you get stuck. Maybe **Button to Remove UserControl from Parent Collection**. Show the click handler you tried for the cross button and say what is/isn't happening or what errors you're getting. – IVSoftware Mar 29 '23 at 12:46
  • I need to send each combo box & text box value to Serial Port, yes I also need to load it from text file, I am also posting new question about this, the form with controls are taking time to resizing, In Original software which I am following does not take time while resizing, why this is happening and idea? – Adil Ahmed Mar 30 '23 at 11:13
  • Dear IVSoftware, I have created the table as described by you, all things get done but the problem is I can't add `Windows Media Player` in .NET Core application, did you know any solution about this or I have to go back to .NET Framework? – Adil Ahmed Apr 22 '23 at 19:51
  • I tested this [answer](https://stackoverflow.com/a/70634593/5438626) and added it to the [repo](https://github.com/IVSoftware/controller-panel.git) to play a 3-second MP3 file on startup. – IVSoftware Apr 22 '23 at 21:51
  • The Controls Flickers when resizing or minimizing the form, is there is any solution for this? – Adil Ahmed Apr 23 '23 at 14:46
  • Dear IVSoftware, I need to stop scrolling of `ScalableHeaderUserControl` whenever user scroll download the `flowLayoutPanel`, Because it contains the information related to below table, Is it possible? – Adil Ahmed Apr 27 '23 at 16:50