C# 如何将变量发送回Form1?
我使用此函数创建了一个新类:C# 如何将变量发送回Form1?,c#,winforms,C#,Winforms,我使用此函数创建了一个新类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Management; namespace ScreenVideoRecorder { class GetMemory { private static void
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Management;
namespace ScreenVideoRecorder
{
class GetMemory
{
private static void DisplayTotalRam()
{
string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
foreach (ManagementObject WniPART in searcher.Get())
{
UInt32 SizeinKB = Convert.ToUInt32(WniPART.Properties["MaxCapacity"].Value);
UInt32 SizeinMB = SizeinKB / 1024;
UInt32 SizeinGB = SizeinMB / 1024;
//Console.WriteLine("Size in KB: {0}, Size in MB: {1}, Size in GB: {2}", SizeinKB, SizeinMB, SizeinGB);
}
}
}
}
我想在Form1中显示标签上的大小链接MB和GB。编辑
由于从KB到MB/GB的转换是标准的,因此可以将其移出此函数,因此我只返回UINT32的列表,因为您没有显示任何其他信息来区分数字:
private static void DisplayTotalRam()
{
string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
List<Uint32> sizes = new List<UInt32>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
foreach (ManagementObject WniPART in searcher.Get())
{
UInt32 SizeinKB = Convert.ToUInt32(WniPART.Properties["MaxCapacity"].Value);
sizes.Add(SizeinKB);
}
return sizes;
}
private static void display totalram()
{
string Query=“从Win32\U PhysicalMemoryArray中选择最大容量”;
列表大小=新列表();
ManagementObjectSearcher search=新的ManagementObjectSearcher(查询);
foreach(searcher.Get()中的ManagementObject WniPART)
{
UInt32 SizeinKB=Convert.ToUInt32(WniPART.Properties[“MaxCapacity”].Value);
尺寸。添加(SizeinKB);
}
返回大小;
}
然后只需按照以下表格进行计算:
List<UInt32> sizes = GetMeMory.DisplayTotalRam();
foreach(UInt32 sizeInKB in sizes)
{
// show sizeInKB on label
UInt32 sizeInMB = sizeInKB / 1024;
// show sizeInMB on label
// ..etc.
}
List size=GetMeMory.DisplayTotalRam();
foreach(尺寸为UInt32 sizeInKB)
{
//在标签上显示sizeInKB
UInt32 sizeInMB=sizeInKB/1024;
//在标签上显示sizeInMB
//……等等。
}
有几种方法可以做到这一点;其中两种更简单的方法是:
Int32s
数组(简单,不干净)private static void DisplayTotalRam()
{
string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
List<Uint32> sizes = new List<UInt32>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
foreach (ManagementObject WniPART in searcher.Get())
{
UInt32 SizeinKB = Convert.ToUInt32(WniPART.Properties["MaxCapacity"].Value);
sizes.Add(SizeinKB);
}
return sizes;
}
private static void display totalram()
{
string Query=“从Win32\U PhysicalMemoryArray中选择最大容量”;
列表大小=新列表();
ManagementObjectSearcher search=新的ManagementObjectSearcher(查询);
foreach(searcher.Get()中的ManagementObject WniPART)
{
UInt32 SizeinKB=Convert.ToUInt32(WniPART.Properties[“MaxCapacity”].Value);
尺寸。添加(SizeinKB);
}
返回大小;
}
然后只需按照以下表格进行计算:
List<UInt32> sizes = GetMeMory.DisplayTotalRam();
foreach(UInt32 sizeInKB in sizes)
{
// show sizeInKB on label
UInt32 sizeInMB = sizeInKB / 1024;
// show sizeInMB on label
// ..etc.
}
List size=GetMeMory.DisplayTotalRam();
foreach(尺寸为UInt32 sizeInKB)
{
//在标签上显示sizeInKB
UInt32 sizeInMB=sizeInKB/1024;
//在标签上显示sizeInMB
//……等等。
}
有几种方法可以做到这一点;其中两种更简单的方法是:
Int32s
数组(简单,不干净)可以向方法中添加字符串参数:
DisplayTotalRam(ref String one, ref String two)
并在方法中使用它们。
因此,如果您有两个要设置文本的标签,请编写:
DisplayTotalRam(ref label1.Text, ref label2.Text);
可以向方法中添加字符串参数:
DisplayTotalRam(ref String one, ref String two)
并在方法中使用它们。
因此,如果您有两个要设置文本的标签,请编写:
DisplayTotalRam(ref label1.Text, ref label2.Text);
D Stanley请给我举一个数字1的例子。D Stanley请给我举一个数字1的例子。是的,他可以,但这不能解决他的问题。提问者想得到一些整数值。给该方法提供两个字符串不会起作用
label1.Text
和label2.Text
在以这种方式调用该方法后,不会在原始位置发生更改。这只是一个示例。您也可以使用“ref”关键字传递对整数的引用。我知道这一点,但您的答案仍然不正确。您是对的。无论如何我都会让它保持下去,这样未来的用户可以看一看。是的,他可以,但这并不能解决他的问题。提问者想要得到一些整数值。给该方法提供两个字符串不会起作用label1.Text
和label2.Text
在以这种方式调用该方法后,不会在原始位置发生更改。这只是一个示例。您也可以使用“ref”关键字传递对整数的引用。我知道这一点,但您的答案仍然不正确。您是对的。不管怎样,我都会让它保持下去,这样未来的用户可以看一看。你的C#book或教程系列关于使用对象和返回数据的内容可能重复?你的C#book或教程系列关于使用对象和返回数据的内容可能重复?