J.C.Morris
J.C.Morris

Reputation: 803

Displaying contents of a function to a textbox on a windows form

In my program, I am attempting to get the WiFi strength value from the system. When I get the value I need, I am trying to display it in a text box on a windows form. So far, I cannot get any sort of value to appear in the text box that I need. I need some insight on how I can achieve this. The call of the function that gets the raw rssi value is inside a timer loop.

*note this code for wifi strength was given by a contributor on this site. it is NOT my own.

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

The name of the textbox is 'wifi'. I think I am having some sort of scoping issue.

Upvotes: 0

Views: 1198

Answers (2)

itsme86
itsme86

Reputation: 19496

You need to use the return value of the function:

wifi.Text = GetSignalStrengthAsInt().ToString();

Upvotes: 1

Casperah
Casperah

Reputation: 4554

You should read the value from the function.

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 

}

Upvotes: 2

Related Questions