0

在我的程序中,我试图从系统中获取 WiFi 强度值。当我得到我需要的值时,我试图在 Windows 窗体的文本框中显示它。到目前为止,我无法在我需要的文本框中显示任何类型的值。我需要一些关于如何实现这一点的见解。获取原始 rssi 值的函数调用在计时器循环内。

*请注意,此 wifi 强度代码由本网站的贡献者提供。它不是我自己的。

 public static int GetSignalStrengthAsInt()
 {
     Int32 returnStrength = 0;
     ManagementObjectSearcher searcher = null;
     try
     {
         searcher = new ManagementObjectSearcher( 
              @"root\WMI", 
              @"select Ndis80211ReceivedSignalStrength  
                from  MSNdis_80211_ReceivedSignalStrength 
                where active=true" );

         // Call the get in order to populate the collection
         ManagementObjectCollection adapterObjects = searcher.Get();

         // Loop though the management object and pull out the signal strength
         foreach ( ManagementObject mo in adapterObjects )
         {
             returnStrength = Convert.ToInt32( 
                 mo["Ndis80211ReceivedSignalStrength"].ToString());
             break;
         }
      }
      catch (Exception)
      {
      }
      finally
      {
          if ( searcher != null )
          {
              searcher.Dispose();
          }
      }
      return returnStrength;            
 }

 //**********************PROBLEM AREA BELOW************************************
 void timer_Tick(object sender, EventArgs e) //not working
 {
     GetSignalStrengthAsInt();
     wifi.Text = returnStrength;  // unclear how to get returnStrength in wifi box
     ...
 }

文本框的名称是“wifi”。我想我遇到了某种范围界定问题。

4

2 回答 2

2

您应该从函数中读取值。

void timer_Tick(object sender, EventArgs e) //not working 
{ 
    int returnStrength = GetSignalStrengthAsInt(); 
    wifi.Text = returnStrength.ToString();  // unclear how to get returnStrength in wifi box 

     //stuff 

}

于 2012-09-04T17:40:07.853 回答
1

您需要使用函数的返回值:

wifi.Text = GetSignalStrengthAsInt().ToString();
于 2012-09-04T17:40:46.053 回答