如何:向 StatusBar 控件添加面板

更新:2007 年 11 月

重要说明:

StatusStripToolStripStatusLabel 控件替换了 StatusBarStatusBarPanel 控件并添加了功能;但是也可选择保留 StatusBarStatusBarPanel 控件以备向后兼容和将来使用。

StatusBar 控件(Windows 窗体) 控件中的可编程区域由 StatusBarPanel 类的实例组成。这些实例是通过添加到 StatusBar.StatusBarPanelCollection 类而添加的。

向状态栏添加面板

  • 在过程中,通过向 StatusBar.StatusBarPanelCollection 添加状态栏面板来创建状态栏面板。使用通过 Panels 属性传递的面板索引指定单个面板的属性设置。

    在下面的代码示例中,图标位置的路径设置是 My Documents 文件夹。使用此位置是因为可假定大多数运行 Windows 操作系统的计算机都包含该文件夹。选择此位置还允许具有最低系统访问级别的用户安全地运行应用程序。下面的示例需要一个已添加了 StatusBar 控件的窗体。

    说明:

    StatusBar.StatusBarPanelCollection 是一个从零开始的集合,因此编写代码时应使用相应的初始值。

    Public Sub CreateStatusBarPanels()
    ' Create panels and set text property.
       StatusBar1.Panels.Add("One")
       StatusBar1.Panels.Add("Two")
       StatusBar1.Panels.Add("Three")
    ' Set properties of StatusBar panels.
    ' Set AutoSize property of panels.
       StatusBar1.Panels(0).AutoSize = StatusBarPanelAutoSize.Spring
       StatusBar1.Panels(1).AutoSize = StatusBarPanelAutoSize.Contents
       StatusBar1.Panels(2).AutoSize = StatusBarPanelAutoSize.Contents
    ' Set BorderStyle property of panels.
       StatusBar1.Panels(0).BorderStyle = StatusBarPanelBorderStyle.Raised
       StatusBar1.Panels(1).BorderStyle = StatusBarPanelBorderStyle.Sunken
       StatusBar1.Panels(2).BorderStyle = StatusBarPanelBorderStyle.Raised
    ' Set Icon property of third panel. You should replace the bolded
    ' icon in the sample below with an icon of your own choosing.
       StatusBar1.Panels(2).Icon = New _ 
       System.Drawing.Icon(System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Icon.ico")
       StatusBar1.ShowPanels = True
    End Sub
    
    public void CreateStatusBarPanels()
    {
       // Create panels and set text property.
       statusBar1.Panels.Add("One");
       statusBar1.Panels.Add("Two");
       statusBar1.Panels.Add("Three");
       // Set properties of StatusBar panels.
       // Set AutoSize property of panels.
       statusBar1.Panels[0].AutoSize = StatusBarPanelAutoSize.Spring;
       statusBar1.Panels[1].AutoSize = StatusBarPanelAutoSize.Contents;
       statusBar1.Panels[2].AutoSize = StatusBarPanelAutoSize.Contents;
       // Set BorderStyle property of panels.
       statusBar1.Panels[0].BorderStyle =
          StatusBarPanelBorderStyle.Raised;
       statusBar1.Panels[1].BorderStyle = StatusBarPanelBorderStyle.Sunken;
       statusBar1.Panels[2].BorderStyle = StatusBarPanelBorderStyle.Raised;
       // Set Icon property of third panel. You should replace the bolded
       // icon in the sample below with an icon of your own choosing.
       // Note the escape character used (@) when specifying the path.
       statusBar1.Panels[2].Icon = 
          new System.Drawing.Icon (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       + @"\Icon.ico");
       statusBar1.ShowPanels = true;
    }
    
    public:
       void CreateStatusBarPanels()
       {
          // Create panels and set text property.
          statusBar1->Panels->Add("One");
          statusBar1->Panels->Add("Two");
          statusBar1->Panels->Add("Three");
          // Set properties of StatusBar panels.
          // Set AutoSize property of panels.
          statusBar1->Panels[0]->AutoSize =
             StatusBarPanelAutoSize::Spring;
          statusBar1->Panels[1]->AutoSize =
             StatusBarPanelAutoSize::Contents;
          statusBar1->Panels[2]->AutoSize =
             StatusBarPanelAutoSize::Contents;
          // Set BorderStyle property of panels.
          statusBar1->Panels[0]->BorderStyle =
             StatusBarPanelBorderStyle::Raised;
          statusBar1->Panels[1]->BorderStyle =
             StatusBarPanelBorderStyle::Sunken;
          statusBar1->Panels[2]->BorderStyle =
             StatusBarPanelBorderStyle::Raised;
          // Set Icon property of third panel.
          // You should replace the bolded image 
          // in the sample below with an icon of your own choosing.
          statusBar1->Panels[2]->Icon =
             gcnew System::Drawing::Icon(String::Concat(
             System::Environment::GetFolderPath(
             System::Environment::SpecialFolder::Personal),
             "\\Icon.ico"));
          statusBar1->ShowPanels = true;
       }
    

请参见

任务

如何:设置状态栏面板的大小

演练:在运行时更新状态栏信息

如何:确定 Windows 窗体 StatusBar 控件中被单击的面板

参考

“集合编辑器”对话框

StatusBar 控件概述(Windows 窗体)

StatusBar

ToolStripStatusLabel