【问题标题】:Create a status bar?创建状态栏?
【发布时间】:2014-03-10 20:06:31
【问题描述】:

我正在使用 C# 脚本将表中的行从一台服务器移动到另一台服务器。请不要告诉 我为此使用 SSIS 组件。我正在使用循环传入的数据集 一个 for 循环,我想查看当前的迭代,即 GUI 中的 for 循环索引 盒子。我可以使用MessageBox.Show("Text"),但我需要按确定/取消以允许 代码继续。所以,我想改用状态栏。我试过an example I got online

示例(下)中的this.Controls.Add(mainStatusBar); 行导致错误 -

csproj.ScriptMain' 不包含“控件”的定义 并且没有扩展方法“控件”接受类型的第一个参数 可以找到“.csproj.ScriptMain”(您是否缺少 using 指令 还是程序集参考?)

尽管添加了引用 - System.Windows.Forms.dll 并执行了 全部保存(即Ctrl+Shift+S)。该脚本使用System.Windows.Forms; 导入 已经。

为什么会出现此错误,我该如何解决?

代码 -

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

【问题讨论】:

  • 您知道使用MessageBox 可以获得更多好处,对吧?
  • @billinkc - 我不知道。我不想为 10K 迭代单击确定/取消。
  • 不确定是否可能,但this.Controls.Add 中的this 不是Windows 窗体,而是部分类ScriptMain(如错误所述)。它的 API 不包含 Controls 属性。参考:technet.microsoft.com/en-us/library/…
  • 表在同一个数据库吗?会一直这样吗?如果是这样,您可以通过运行INSERT INTO Table1 (COl1, COl2) SELECT Col3,Col4 FROM Table2 让事情变得更简单、更快捷,别担心,这不是 SSIS。
  • “C# 脚本”是什么意思?数据复制代码是大型应用程序(Winforms、WPF、ASP.Net、控制台应用程序)的一部分吗?还是这个数据复制是“一次性”操作,之后你的代码会被扔掉?

标签: c# .net-3.5 statusbar


【解决方案1】:

程序不知道this 应该引用此语句this.Controls.Add(mainStatusBar); 中的一个表单

您必须按照百分比建议的方式进行操作。

正确的使用方法是这样的:

举例

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

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

另外,看看这篇文章:

When do you use the "this" keyword?

【讨论】:

    【解决方案2】:

    如果您在 SQL Server 上运行脚本,则无法向脚本添加控制,因为那里没有窗口。

    您可以做的是创建一个独立的 GUI 应用程序,该应用程序将与您的脚本通信(例如通过 TCP)。

    或者您可以创建一个文件并在执行一次迭代时向其中添加新文本。

    使用 mTail 实时查看文件中发生的情况。

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

    【讨论】:

      【解决方案3】:

      您正在使用的类没有Controls,因为它不是Form

      您可以通过以下方式创建Form 并为其添加状态栏:

      替换

      this.Controls.Add(mainStatusBar);
      

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

      最后一行将显示带有您的状态栏的窗口。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-11
        • 1970-01-01
        • 2017-07-18
        相关资源
        最近更新 更多