在我的程序中,我试图从系统中获取WiFi强度值。当我得到我需要的值时,我试图在窗体上的文本框中显示它。到目前为止,我无法在我需要的文本框中显示任何值。我需要了解如何实现这一目标。获取原始rssi值的函数的调用位于计时器循环内。
*请注意此网站上的贡献者提供了此强度的代码。这不是我自己的。
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”。 我想我有一些范围问题。
答案 0 :(得分: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
}
答案 1 :(得分:1)
您需要使用函数的返回值:
wifi.Text = GetSignalStrengthAsInt().ToString();