Search code examples
c#.net-3.5statusbar

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.";
}

Solution

  • 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?