如何处理方法必须在C#windowsforms中有返回类型 [英] How to handle method must have return type in C# windowsforms

查看:59
本文介绍了如何处理方法必须在C#windowsforms中有返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在Form1.cs页面中的代码。在下面的代码附近

Below is my code in Form1.cs page.In the below code near

// Run the app

    public static void Main( )
    {
       Application.Run(new TestForm( ));
    }

在构造函数的最后和附近

in the last and near the constructor

// Constructor

    public TestForm()
    {

我收到错误因为方法在编译时必须有返回类型,而我的

program.cs中的代码是



i am getting error as method must have return type while compling and the code in my
program.cs is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace DssToWav
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}







Form1 .cs






Form1.cs

<pre lang="C#">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;
using System;
using System.IO;
using Arbingersys.Audio.Aumplib;

namespace DssToWav
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         // Declarations hence


		private string inputFile = "Not set";
		private string outputFile = "Not set";


		private Label label1;

		private ComboBox comboBox1;
		private Label comboBox1Label;
		private Button convertButton;
		private static ProgressBar progressBar1;

		// Source file open button and label:

		private Button sourceFileButton;
		private Label sourceFileLabel;	  


		// Destination file save button and label:

		private Button destFileButton;
		private Label destFileLabel;	  	  


		string[] convertFormat = new string[] {"WAV", "MP3", "AU", "AIFF"};

		private Aumpel audioConverter = new Aumpel();
		private Aumpel.soundFormat inputFileFormat;
		private Aumpel.soundFormat outputFileFormat;


		// Constructor

		public TestForm()
		{
		  
		  // Create the objects
		  
		  this.label1 = new Label();
		  this.convertButton = new Button();
		  
		  progressBar1 = new ProgressBar();		  
		  
		  this.sourceFileButton = new Button();
		  this.sourceFileLabel = new Label();

		  this.destFileButton = new Button();
		  this.destFileLabel = new Label();	

		  this.comboBox1 = new ComboBox();
		  this.comboBox1Label = new Label();
	
		 

		  // Set the form's title
		  
		  this.Text = "TestForm";


		  // Set up the output label
		  
		  label1.Location = new System.Drawing.Point (10, 10);
		  label1.Text = "Select a file to convert and a destination.";
		  label1.Size = new System.Drawing.Size (216, 16);


		  // Set convert button location
		  
		  convertButton.Location = new System.Drawing.Point (300,80);
		  convertButton.Size = new System.Drawing.Size (65, 22);
		  convertButton.Text = "&Convert";
		  

		  // Set source file button location
		  
		  sourceFileButton.Location = new System.Drawing.Point (10,30);
		  sourceFileButton.Size = new System.Drawing.Size (100, 22);
		  sourceFileButton.Text = "&File to convert:";
		  

		  // Set source file label location

		  sourceFileLabel.Location = new System.Drawing.Point (10, 55);
		  sourceFileLabel.Size = new System.Drawing.Size (375, 22);
		  sourceFileLabel.Text = inputFile;		  


		  // Set destination file button location
		  
		  destFileButton.Location = new System.Drawing.Point (10,80);
		  destFileButton.Size = new System.Drawing.Size (100, 22);
		  destFileButton.Text = "&Save to:";
		  

		  // Set destination file label location

		  destFileLabel.Location = new System.Drawing.Point (10, 105);
		  destFileLabel.Size = new System.Drawing.Size (375, 22);
		  destFileLabel.Text = outputFile;	


		  // Conversion file types
		  
		  comboBox1Label.Location = new System.Drawing.Point (120,80);
		  comboBox1Label.Size = new System.Drawing.Size(90,16);
		  comboBox1Label.Text = "Convert to:";
		  
		  comboBox1.Location = new System.Drawing.Point (216,80);
		  comboBox1.Size = new System.Drawing.Size(75,10);
		  
		  this.comboBox1.Items.AddRange(convertFormat);		  



		  // Progress Bar

		  progressBar1.Location = new System.Drawing.Point(10, 145);
		  progressBar1.Size = new System.Drawing.Size(375, 22);
		  progressBar1.Minimum = 0;
		  progressBar1.Maximum = 100;		  

		  
		  
		  // Set up the event handlers
		  
		  sourceFileButton.Click += 
			  new System.EventHandler (this.sourceFileButton_Click);		 

		  destFileButton.Click += 
			  new System.EventHandler (this.destFileButton_Click);			  

		  convertButton.Click += 
			  new System.EventHandler (this.convertButton_Click);		  


		  
		  // Add the controls and set the client area
		  
		  this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
		  this.ClientSize = new System.Drawing.Size (400, 200);
		  this.Controls.Add (this.convertButton);		  
		  this.Controls.Add (this.sourceFileButton);
		  this.Controls.Add (this.sourceFileLabel);
		  this.Controls.Add (this.destFileButton);
		  this.Controls.Add (this.destFileLabel);		  
		  this.Controls.Add (this.label1);
		  this.Controls.Add (this.comboBox1);
		  this.Controls.Add (this.comboBox1Label);
		  this.Controls.Add (progressBar1);
	  

      }
		


		// Delegates for decoding

		public static int soundFileSize = 0;
		

		// Conversion callback (lame,libsndfile)
		
		private static void
		ReportStatus(int totalBytes, 
			int processedBytes, Aumpel aumpelObj)
		{
			progressBar1.Value = 
				(int)(((float)processedBytes/(float)totalBytes)*100);
		}


		// Decoding callback (madlldlib)
		
		private static bool 
		ReportStatusMad(uint frameCount, uint byteCount, 
				ref MadlldlibWrapper.mad_header mh) 
		{

			progressBar1.Value = 
				(int)(((float)byteCount/(float)soundFileSize)*100);

			return true;
		}


		  
		private void
		ShowExceptionMsg(Exception e)
		{
			MessageBox.Show("Exception: " + e.Message, 
				"Exception!", MessageBoxButtons.OK);
		}


	  // Event handlers hence
	  
	  protected void
	  sourceFileButton_Click (object sender, System.EventArgs e)
	  {
		    
			// Show Open File dialog

			OpenFileDialog openFile = new OpenFileDialog();
			openFile.Filter  = "MP3 (*.mp3)|*.mp3|WAV (*.wav)|" + 
				"*.wav|All Files (*.*)|*.*";

			openFile.FileName = "" ;
			openFile.CheckFileExists = true;
			openFile.CheckPathExists = true;


			if ( openFile.ShowDialog() != DialogResult.OK )
			return;


			// Set input file formation 

			try
			{
				inputFileFormat = 
					audioConverter.CheckSoundFormat(openFile.FileName);
			}
			catch(Exception ex)
			{
				ShowExceptionMsg(ex);
				return;
			}

			sourceFileLabel.Text = inputFile = openFile.FileName;
			
	  }	


	  protected void
	  destFileButton_Click (object sender, System.EventArgs e)
	  {
		    
			// Show Open File dialog

			SaveFileDialog saveFile = new SaveFileDialog();
			saveFile.Filter  = "MP3 (*.mp3)|*.mp3|" +
				"WAV (*.wav)|*.wav|" +
				"AIFF (*.aiff)|*.aiff|" +
				"AU (*.au)|*.au|" +
				"All Files (*.*)|*.*";


			if ( saveFile.ShowDialog() != DialogResult.OK )
				return;


			// Set input file formation 

			destFileLabel.Text = outputFile = saveFile.FileName;
			
	  }


	  protected void
	  convertButton_Click (object sender, System.EventArgs e)
	  {	  

		  // Set conversion type

		  switch((string)comboBox1.SelectedItem)
		  {
			  case "WAV":
				  outputFileFormat = Aumpel.soundFormat.WAV;
			      break;
			  case "MP3":
				  outputFileFormat = Aumpel.soundFormat.MP3;
			      break;
			  case "AU":
				  outputFileFormat = Aumpel.soundFormat.AU;
			      break;
			  case "AIFF":
				  outputFileFormat = Aumpel.soundFormat.AIFF;
			      break;
			  default:
				  MessageBox.Show("You must select a type to convert to.", 
						  "Error", MessageBoxButtons.OK);
				  return;
		  }


		  // Convert to MP3

		  if ( (int)outputFileFormat == (int)Aumpel.soundFormat.MP3 )
		  {

			  try
			  {
				  
				  Aumpel.Reporter defaultCallback = 
					  new Aumpel.Reporter(ReportStatus);
				  
				  audioConverter.Convert(inputFile, 
						  (int)inputFileFormat, outputFile, 
						  (int)outputFileFormat, defaultCallback);
				  
				  progressBar1.Value = 0;
				  
				  destFileLabel.Text = outputFile = "";
				  sourceFileLabel.Text = inputFile = "";
				  
				  MessageBox.Show("Conversion finished.", 
						  "Done.", MessageBoxButtons.OK);

			  }
			  catch (Exception ex)
			  {
				  ShowExceptionMsg(ex);
				  return;
			  }
	  
		  }

		  // From MP3 (using named pipe):

		  else if ( (int)inputFileFormat == (int)Aumpel.soundFormat.MP3 )
		  {

			  try
			  {
				  
				  MadlldlibWrapper.Callback defaultCallback = 
					  new MadlldlibWrapper.Callback(ReportStatusMad);

				  // Determine file size
				  FileInfo fi = new FileInfo(inputFile);		
				  soundFileSize = (int)fi.Length;

				  audioConverter.Convert(inputFile, 
						  outputFile, outputFileFormat, defaultCallback);				  
				  progressBar1.Value = 0;
				  
				  destFileLabel.Text = outputFile = "";
				  sourceFileLabel.Text = inputFile = "";
				  
				  MessageBox.Show("Conversion finished.", 
						  "Done.", MessageBoxButtons.OK);

			  }
			  catch (Exception ex)
			  {
				  ShowExceptionMsg(ex);
				  return;
			  }			  
			  
		  }

		  // Non-MP3 soundfile conversion:

		  else
		  {
			  
			  try		  
			  {

				  Aumpel.Reporter defaultCallback = 
					  new Aumpel.Reporter(ReportStatus);
			  
				  audioConverter.Convert(inputFile, 
						  (int)inputFileFormat, 
						  outputFile, 
						  (int)(outputFileFormat | Aumpel.soundFormat.PCM_16), 
						  defaultCallback);
				  
				  progressBar1.Value = 0;
				  
				  destFileLabel.Text = outputFile = "";
				  sourceFileLabel.Text = inputFile = "";
				  
				  MessageBox.Show("Conversion finished.", 
						  "Done.", MessageBoxButtons.OK);
				  
			  }
			  catch (Exception ex)
			  {
				  ShowExceptionMsg(ex);
				  return;
			  }			  

		  }
		  		
		  
		
	  }	  
	  
		
      // Run the app

      public static void Main( ) 
      {
         Application.Run(new TestForm( ));
      }

    }
}





我尝试过:



有谁可以帮我解决这个问题



What I have tried:

Can anybody help me out how to handle this

推荐答案

有一个号码你需要做的事情:



There are a number of things you need to do here:



  1. 重命名 Form1 TestForm
public partial class Form1 TestForm : Form
{



  • InitializeComponents 行从当前Form1构造函数复制到TestForm构造函数


  • Copy the InitializeComponents line from the current Form1 constructor into your TestForm constructor

    public TestForm()
    {
        InitializeComponnents();		  
        // Create the objects

  • 删除当前的Form1构造函数

  • Delete the current Form1 constructor

    public Form1()
    {
       InitializeComponent();
    }

  • 确保 Main 调用 TestForm constructor

  • Make sure Main calls the TestForm constructor

    Application.Run(new Form1TestForm());


  • 你的问题就在这一行,它需要有空。 .net中的构造函数与类相同。



    Your problem is on this line, it needs to have void. Constructors in .net are called the same as the class.

    // Constructor
    public TestForm() // needs to have void as return type
    {


    这篇关于如何处理方法必须在C#windowsforms中有返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆