c# - 如何将 BackColore 颜色存储为一种方法,并在 C# 中以不同的方法使用它
问题描述
我构建了一个为面板保存 BackColor 的方法,如果我在新方法中调用该方法,颜色将不会显示
这是我的代码'''''''''''''''''''''''''''''''''''''''''' ''''''
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace WindowsFormsApplication24
{
class theme
{
public static void dark()
{
Form1 f = new Form1();
Color c= f.panel1.BackColor = Color.Black;
}
}
}
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
theme.dark();
}
}
}
''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''
解决方案
new Form1()
您实例化的是该类的一个实例,Form1
但许多Form1
实例可以同时存在。你想传递Form1
你试图改变的实例。尝试:
private void Form1_Load(object sender, EventArgs e)
{
theme.dark(this);
}
和:
public static void dark(Form1 f)
{
f.panel1.BackColor = Color.Black;
}
推荐阅读
- json - 使用代码外的 json 模式文件创建数据帧读取 json 文件
- javascript - javascript中的条件字符串检查
- apache-nifi - Nifi:MergeRecord 不会等待并将 json 文件分组为一批
- linq - 在使用 Linq 选择的匿名对象中等待任务
- maven - 将 Azure Devops 工件发布到 Maven Central
- php - 如何为 Xdebug 3.0 版设置 VSCode launch.json?
- r - 如何从 renv.lock 更改 R 存储库 CRAN 以从内部/企业存储库获取包?
- ruby-on-rails - 尝试测试调用 AWS S3 Bucket copy_to 的 (minitest) 方法。如何模拟或存根?
- r - 当我们有依赖于输入的动态 UI 时,如何在 R 中获得输出?
- list - 试图输出一个列表,但得到一个值