Create a status bar?

I am moving rows from a table from one server to another another using a C# script. Please don't tell me to use SSIS components for that. I am looping the incoming dataset using a for loop and I want to see the current iteration, ie for loop index in a GUI box. I can use MessageBox.Show("Text"), but I need to press ok/cancel to allow the code to continue. So, I thought of using a status bar instead. I tried an example I got online

The line this.Controls.Add(mainStatusBar); in the example (below) causes the error -

csproj.ScriptMain' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument of type '.csproj.ScriptMain' could be found (are you missing a using directive or an assembly reference?)

This happens despite adding the reference - System.Windows.Forms.dll and doing a save all (ie Ctrl+Shift+S). The script has a import using System.Windows.Forms; already.

Why am I getting this error and how do I fix it ?

Code -

protected StatusBar mainStatusBar = new StatusBar();
protected StatusBarPanel statusPanel = new StatusBarPanel();
protected StatusBarPanel datetimePanel = new StatusBarPanel();

private void CreateStatusBar()
{
    // Set first panel properties and add to StatusBar
    statusPanel.BorderStyle = StatusBarPanelBorderStyle.Sunken;
    statusPanel.Text = "Application started. No action yet.";
    statusPanel.ToolTipText = "Last Activity";
    statusPanel.AutoSize = StatusBarPanelAutoSize.Spring;
    mainStatusBar.Panels.Add(statusPanel);

    // Set second panel properties and add to StatusBar
    datetimePanel.BorderStyle = StatusBarPanelBorderStyle.Raised;
    datetimePanel.ToolTipText = "DateTime: " + 
    System.DateTime.Today.ToString();
    datetimePanel.Text = System.DateTime.Today.ToLongDateString();
    datetimePanel.AutoSize = StatusBarPanelAutoSize.Contents;
    mainStatusBar.Panels.Add(datetimePanel);

    mainStatusBar.ShowPanels = true;
    // Add StatusBar to Form controls
    this.Controls.Add(mainStatusBar);

}

private void button1_Click(object sender, EventArgs e)
{
    statusPanel.Text = "Button is clicked.";
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    statusPanel.Text = "CheckBox is checked.";
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    statusPanel.Text = "TextBox edited.";
}

Answers


The program doesn't know that this is supposed to refer to a form in this statement this.Controls.Add(mainStatusBar);

You would have to do it the way Percentage has suggested.

The proper way to use this is like so :

For Instance

public partial class someForm : Form
    {
        public someForm()
        {
            InitializeComponent();
        }
    }

partial class someForm
    {
        private void InitializeComponent()
            {
                this.mainStatusBar = new StatusBar();
            }
    }

Also, take a look at this article :

When do you use the "this" keyword?

If you are runing script on SQL Server you can't add control to your script because you don't have a window there.

What you can do is to create a standalone GUI aplication that will comunicate with your script (by TCP for example).

Or you can create a file and add new text to it when you perform one iteration.

Use mTail to see what is happening inside your file in realtime.

mTail - http://ophilipp.free.fr/op_tail.htm


The class that you are using does not have Controls because it is not a Form.

You may create a Form the following way and add a status bar to it:

Replace

this.Controls.Add(mainStatusBar);

With

Form window = new Form();
window.Controls.Add(mainStatusBar);
window.ShowDialog();

The last line will display the window with your status bar in it.


Need Your Help

Open VSTFS Link in Visual Studio by PowerShell Script

visual-studio powershell tfs

I try to open a detail page of a successfull build in VisualStudio by a PowerShell script. I am able to connect to the collection of my test project and a certain Builddefinition. I got a vstfs-Lin...

How can I create a mock back-end in AngularJS?

angularjs testing mocking prototyping

How can I implement a mock back-end to do quick prototyping with AngularJS?