显示标签中字符串的值

 别他妈给脸不要脸别动老子东西 发布于 2023-02-09 13:58

我的表单上有一个标签,我想要填充字符串的值,但我无法使其正常工作.这是我的代码;

在函数Exec​​ute中,我调用函数ShowText((string)result[3]),result是我从数据库查询中获取的对象.这是ShowText函数;

public void ShowText(string message)
{
    label4.Text = message;
}

我收到NullReferenceException错误,我做错了什么?

结果[3]; 在此输入图像描述 label4 在此输入图像描述

码;

    Execute();
    InitializeComponent();
    var aTimer = new System.Timers.Timer();
    aTimer.Elapsed += new ElapsedEventHandler((sender, e) => Execute());
    aTimer.Interval = 300000;
    aTimer.Enabled = true;

}
public void Execute()
{
    int Tijd;
    int Videolengte;
    string resultaat;
    string URL = "";

    Database db = new Database(CONNECTION_STRING);
    object[] result = db.GetFirstRecord();

    if (result == null)
    {
        return;
    }
    else
    {
        //laat app steeds 50ms slapen zodat overgang van muziek mooi is
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 90);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 80);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 70);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 60);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 50);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 40);
        //start youtube video op
        URL = (string)result[1];
        Process browser = Process.Start(@"chrome.exe", "http:\\www.youtube.com/watch?v=" + URL);
        ShowText((string)result[3]);
        //laat app steeds 50ms slapen zodat overgang van muziek mooi is
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 30);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 20);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 10);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 0);
        //bepaald tijd die applicatie moet slapen
        resultaat = (string)result[2];
        Videolengte = Convert.ToInt32(resultaat);
        Tijd = Videolengte * 1000;
        //laat app slapen tot lengte van liedje
        System.Threading.Thread.Sleep(Tijd);
        //laat app steeds 50ms slapen zodat overgang van muziek mooi is
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 10);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 20);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 30);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 40);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 50);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 60);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 70);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 80);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 90);
        System.Threading.Thread.Sleep(500);
        Audio.SetApplicationVolume(APP, 100);
        //Kill alle google extensies
        Process[] localByName = Process.GetProcessesByName("chrome");
        foreach (Process p in localByName)
        {
            p.Kill();
        }
        //verwijder record uit database
        //deleteRow();

    }
}
public void ShowText(string message)
{
    label5.Text = message;
}
private void deleteRow()
{
    try
    {
        using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
        {
            conn.Open();
            using (SqlCommand cmd1 = new SqlCommand("SELECT * FROM Tracks ORDER BY Tijdstip", conn))
            using (SqlCommand cmd = new SqlCommand("DELETE TOP(1) FROM Tracks", conn))
            {
               cmd.ExecuteNonQuery();
               cmd1.ExecuteNonQuery();
            }
        }
    }
    catch (SqlException ex)
    {
        //Het is niet gelukt om het record uit de database te verwijderen
    }
}

private void FormMain_Load(object sender, EventArgs e)
{
    //Execute();
}

private Process GetWindowProcess(string windowTitle)
{
    foreach (var proc in Process.GetProcessesByName("chrome"))
    {
        if (proc.MainWindowTitle.IndexOf(windowTitle, StringComparison.OrdinalIgnoreCase) > 0)
        {
            return proc;
        }
    }

    return null;
}

Mike Perreno.. 5

你要么在这里得到它:

ShowText((string)result[3])

因为resultnull,或者你在这里得到它:

label4.Text = message;

因为label4null.

最有可能的resultnull; 特别是如果它来自数据库.


更新:既然已经添加了所有代码,只需在调用Execute 后将调用移到InitializeComponent.你看,InitializeComponent在WYSIWYG设计器中创建你在表单上删除的所有东西.

1 个回答
  • 你要么在这里得到它:

    ShowText((string)result[3])
    

    因为resultnull,或者你在这里得到它:

    label4.Text = message;
    

    因为label4null.

    最有可能的resultnull; 特别是如果它来自数据库.


    更新:既然已经添加了所有代码,只需在调用Execute 后将调用移到InitializeComponent.你看,InitializeComponent在WYSIWYG设计器中创建你在表单上删除的所有东西.

    2023-02-09 14:00 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有