![](/img/trans.png)
[英]Windows Form won't show with DirectX.AudioVideoPlayback - C#
[英]Button classes that I've written with C# won't show up in Windows Form when running
剛開始學習 Visual Studio,我正在嘗試在 Windows 窗體中制作音板。 ButtonMaker(); 是我用來為目錄中的每個聲音文件制作按鈕的功能,因此我不必為每個聲音制作 70 個不同的按鈕,但是當我運行程序時,表單窗口中沒有任何顯示。 有人知道為什么嗎? 我試過在 Main() 和初始 Form1 類中調用該函數,但兩者都沒有發生。 表單類文件在這里:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MySoundBoard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
///Tried running it here
}
private void Form1_Load(object sender, EventArgs e)
{
///Tried running it here
}
private void ButtonMaker()
{
string[] files = Soundfiles.GetFile();
foreach (var item in files)
{
string btnName = item.ToUpper();
Button btNname = new Button();
btNname.Text = item;
int x = 40;
int y = 40;
btNname.Location = new Point(x, y);
x = x + 50;
if (x>900)
{
x = 40;
y = y + 30;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
這是 SoundFiles 類:
using System.IO;
using System;
using System.Text;
using System.Diagnostics;
using WMPLib;
namespace MySoundBoard
{
class Soundfiles {
WMPLib.WindowsMediaPlayer Player;
static public string[] GetFile() {
string txtPath = @"C:\Documents\path\to\sound effects";
string[] files =
Directory.GetFiles(txtPath, "*ProfileHandler.cs", SearchOption.TopDirectoryOnly);
return files;
}
public void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.URL = url;
Player.controls.play();
}
}
}
和主項目文件(尚未使用):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MySoundBoard
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
///Tried running it here
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
如前所述,我對這種語言很陌生,任何幫助都會得到幫助!
您創建按鈕但從不將它們添加到表單中。 只需添加
this.Controls.Add(btNname);
接下來的事情是,你的按鈕不會做任何事情。 您還需要添加一個事件處理程序。
btNname.Click += ...;
為了知道哪個按鈕播放哪個聲音,您需要找到一種方法來進行關聯。 一個hacky方法是
btNname.Tag = item;
然后稍后評估Tag
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.