Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações. |
Tradução
Original
|
Classe PowerStatus
Indica o sistema corrente potência informações de status.
Assembly: System.Windows.Forms (em System.Windows.Forms.dll)
The PowerStatus classe representa informações sobre o status corrente de potência de linha CA, status de carregamento da bateria e status de carga da bateria.
Essa classe é usada pelo PowerStatus propriedade das SystemInformation classe para indicar informações corrente de potência do sistema.
O exemplo a seguir lista todas as propriedades do PowerStatus classe em um ListBox controlar e exibe o valor corrente da propriedade associada em um TextBox controle quando uma lista de item selecionada.
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Reflection; using System.Windows.Forms; namespace PowerStatusBrowser { publicclass PowerStatusBrowserForm : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.TextBox textBox1; public PowerStatusBrowserForm() { this.SuspendLayout(); InitForm(); //Add each property of the PowerStatus class to the list box. Type t = typeof(System.Windows.Forms.PowerStatus); PropertyInfo[] pi = t.GetProperties(); for( int i=0; i<pi.Length; i++ ) listBox1.Items.Add( pi[i].Name ); textBox1.Text = "The PowerStatus class has "+pi.Length.ToString()+" properties.\r\n"; // Configure the list item selected handler for the list box to invoke a // method that displays the value of each property. listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged); this.ResumeLayout(false); } privatevoid listBox1_SelectedIndexChanged(object sender, EventArgs e) { // Return if no item is selected.if( listBox1.SelectedIndex == -1 ) return; // Get the property name from the list item string propname = listBox1.Text; // Display the value of the selected property of the PowerStatus type. Type t = typeof(System.Windows.Forms.PowerStatus); PropertyInfo[] pi = t.GetProperties(); PropertyInfo prop = null; for( int i=0; i<pi.Length; i++ ) if( pi[i].Name == propname ) { prop = pi[i]; break; } object propval = prop.GetValue(SystemInformation.PowerStatus, null); textBox1.Text += "\r\nThe value of the "+propname+" property is: "+propval.ToString(); } privatevoid InitForm() { // Initialize the form settingsthis.listBox1 = new System.Windows.Forms.ListBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.listBox1.Location = new System.Drawing.Point(8, 16); this.listBox1.Size = new System.Drawing.Size(172, 496); this.listBox1.TabIndex = 0; this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.textBox1.Location = new System.Drawing.Point(188, 16); this.textBox1.Multiline = true; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(420, 496); this.textBox1.TabIndex = 1; this.ClientSize = new System.Drawing.Size(616, 525); this.Controls.Add(this.textBox1); this.Controls.Add(this.listBox1); this.Text = "Select a PowerStatus property to get the value of"; } [STAThread] staticvoid Main() { Application.Run(new PowerStatusBrowserForm()); } } }
package PowerStatusBrowser;
import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Drawing.*;
import System.Reflection.*;
import System.Windows.Forms.*;
public class PowerStatusBrowserForm extends System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
public PowerStatusBrowserForm()
{
this.SuspendLayout();
InitForm();
//Add each property of the PowerStatus class to the list box.
Type t = System.Windows.Forms.PowerStatus.class.ToType();
PropertyInfo pi[] = t.GetProperties();
for (int i = 0; i < pi.get_Length(); i++) {
listBox1.get_Items().Add(pi[i].get_Name());
}
textBox1.set_Text("The PowerStatus class has "
+ System.Convert.ToString(pi.get_Length()) + " properties.\r\n");
// Configure the list item selected handler for the list box to invoke
// a method that displays the value of each property.
listBox1.add_SelectedIndexChanged(
new EventHandler(listBox1_SelectedIndexChanged));
this.ResumeLayout(false);
} //PowerStatusBrowserForm
private void listBox1_SelectedIndexChanged(Object sender, EventArgs e)
{
// Return if no item is selected.
if (listBox1.get_SelectedIndex() == -1) {
return;
}
// Get the property name from the list item
String propName = listBox1.get_Text();
// Display the value of the selected property of the PowerStatus type.
Type t = System.Windows.Forms.PowerStatus.class.ToType();
PropertyInfo pi[] = t.GetProperties();
PropertyInfo prop = null;
for (int i = 0; i < pi.get_Length(); i++) {
if (pi[i].get_Name().Equals(propName)) {
prop = pi[i];
break;
}
}
Object propVal = prop.GetValue(SystemInformation.get_PowerStatus(),
null);
textBox1.set_Text(textBox1.get_Text() + "\r\nThe value of the "
+ propName + " property is: " + propVal.ToString());
} //listBox1_SelectedIndexChanged
private void InitForm()
{
// Initialize the form settings
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1.set_Anchor((System.Windows.Forms.AnchorStyles)(
System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.
AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.
Left | System.Windows.Forms.AnchorStyles.Right));
this.listBox1.set_Location(new System.Drawing.Point(8, 16));
this.listBox1.set_Size(new System.Drawing.Size(172, 496));
this.listBox1.set_TabIndex(0);
this.textBox1.set_Anchor((System.Windows.Forms.AnchorStyles)(
System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.
AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right));
this.textBox1.set_Location(new System.Drawing.Point(188, 16));
this.textBox1.set_Multiline(true);
this.textBox1.set_ScrollBars(System.Windows.Forms.ScrollBars.Vertical);
this.textBox1.set_Size(new System.Drawing.Size(420, 496));
this.textBox1.set_TabIndex(1);
this.set_ClientSize(new System.Drawing.Size(616, 525));
this.get_Controls().Add(this.textBox1);
this.get_Controls().Add(this.listBox1);
this.set_Text("Select a PowerStatus property to get the value of");
} //InitForm
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new PowerStatusBrowserForm());
} //main
} //PowerStatusBrowserForm
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.