Windows Forms - zdarzenie wciśnięcia przycisku nie uruchamia się

0

Witam.
Muszę zrobić prostą aplikację okienkową, jednak natrafiłem na problem.
Program nie reaguje na kliknięcie myszą na przycisku "Oblicz".
Nie mogę znaleźć błędu.
Proszę o pomoc.

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Reynolds2
{
	/// <summary>
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		               private void FormMain_Load(object sender, EventArgs e)
        {
            comboBoxPipeShape.SelectedIndex = 0;
            label8.Text = "";
            label9.Text = "";
        }
		void ComboBoxPipeShapeSelectedIndexChanged(object sender, EventArgs e)
		{
	 switch (comboBoxPipeShape.SelectedIndex)
            {
                case 0:
                    numericUpDown1.Enabled = true;
                    numericUpDown2.Enabled = false;
                    numericUpDown3.Enabled = false;
                    break;
                case 1:
                    numericUpDown1.Enabled = false;
                    numericUpDown2.Enabled = true;
                    numericUpDown3.Enabled = false;
                    break;
                case 2:
                    numericUpDown1.Enabled = false;
                    numericUpDown2.Enabled = true;
                    numericUpDown3.Enabled = true;
                    break;
            }
		}
		        private void button1_Click(object sender, MouseEventArgs e)
        {
            double srednica = 0;
            double Reynolds = 0;
            switch (comboBoxPipeShape.SelectedIndex)
            {
                case 0:
                    srednica = decimal.ToDouble(numericUpDown1.Value);
                    break;
                case 1:
                    srednica = decimal.ToDouble(numericUpDown2.Value);
                    break;
                case 2:
                    double height = decimal.ToDouble(numericUpDown3.Value);
                    double width = decimal.ToDouble(numericUpDown2.Value);
                    srednica = (2 * width * height) / (width + height);
                    break;
            }
		Reynolds=decimal.ToDouble(numericUpDown4.Value) * (decimal.ToDouble(numericUpDown5.Value) * (srednica) /
                                                                     (decimal.ToDouble(numericUpDown6.Value)));
            
            label8.Text=Reynolds.ToString("0.######");
            
            if (Reynolds < 2100)
                {
                    label9.Text = "laminarny.";
                }
            else
                {
                    if ((Reynolds > 2100) && (Reynolds < 4000))
                        {
                            label9.Text = "przejściowy.";
                        }   
                    else
                        {
                            label9.Text = "burzliwy.";
                        }
	}
}
}
}
0

Popraw formatowanie kodu. Rozumiem, że przycisk Oblicz nazywa się button1? Na pierwszy rzut oka nie widzę żadnych błędów. Do funkcji button1_Click(...) dodaj linijkę

MessageBox.Show("Wciśnięto przycisk");

teraz sprawdź czy po wciśnięciu przycisku wyskakuje okienko z komunikatem.

0

Program robię w SharpDevelop, nie bardzo rozumiem, czym jest XAML.

0

Po dodaniu linijki, nie wyświetla się żaden komunikat.

0
Początkujący2 napisał(a):

Program robię w SharpDevelop, nie bardzo rozumiem, czym jest XAML.

Utworzyłeś projekt Windows Forms czy WPF?

0

Windows Forms

0

Zmieniłem temat sam, ale następnym razem tak zatytułowany wątek poleci do kosza.

0

Brak tu kodu odpowiedzialnego za inicjalizację i deklarację komponetów (MainForm.Designer.cs), nie wiadomo nawet czy to button1 jest w ogóle podpięte to zdarzenie przez += i EventHandler, bez tego ciężko tu coś powiedzieć.

0

Oto kod MainForm.Designer.cs:

namespace Reynolds2
{
	partial class MainForm
	{
		/// <summary>
		/// Designer variable used to keep track of non-visual components.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.NumericUpDown numericUpDown6;
		private System.Windows.Forms.NumericUpDown numericUpDown5;
		private System.Windows.Forms.NumericUpDown numericUpDown4;
		private System.Windows.Forms.NumericUpDown numericUpDown3;
		private System.Windows.Forms.NumericUpDown numericUpDown2;
		private System.Windows.Forms.NumericUpDown numericUpDown1;
		private System.Windows.Forms.ComboBox comboBoxPipeShape;
		private System.Windows.Forms.Label label16;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Button button1;
		
		/// <summary>
		/// Disposes resources used by the form.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label16 = new System.Windows.Forms.Label();
			this.label15 = new System.Windows.Forms.Label();
			this.label14 = new System.Windows.Forms.Label();
			this.label13 = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.numericUpDown6 = new System.Windows.Forms.NumericUpDown();
			this.numericUpDown5 = new System.Windows.Forms.NumericUpDown();
			this.numericUpDown4 = new System.Windows.Forms.NumericUpDown();
			this.numericUpDown3 = new System.Windows.Forms.NumericUpDown();
			this.numericUpDown2 = new System.Windows.Forms.NumericUpDown();
			this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
			this.comboBoxPipeShape = new System.Windows.Forms.ComboBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label9 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown6)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown5)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown4)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown3)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label16);
			this.groupBox1.Controls.Add(this.label15);
			this.groupBox1.Controls.Add(this.label14);
			this.groupBox1.Controls.Add(this.label13);
			this.groupBox1.Controls.Add(this.label12);
			this.groupBox1.Controls.Add(this.label11);
			this.groupBox1.Controls.Add(this.numericUpDown6);
			this.groupBox1.Controls.Add(this.numericUpDown5);
			this.groupBox1.Controls.Add(this.numericUpDown4);
			this.groupBox1.Controls.Add(this.numericUpDown3);
			this.groupBox1.Controls.Add(this.numericUpDown2);
			this.groupBox1.Controls.Add(this.numericUpDown1);
			this.groupBox1.Controls.Add(this.comboBoxPipeShape);
			this.groupBox1.Controls.Add(this.label7);
			this.groupBox1.Controls.Add(this.label6);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(12, 12);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(431, 303);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Dane wejsciowe";
			// 
			// label16
			// 
			this.label16.Location = new System.Drawing.Point(356, 264);
			this.label16.Name = "label16";
			this.label16.Size = new System.Drawing.Size(100, 23);
			this.label16.TabIndex = 20;
			this.label16.Text = "N*s/m2";
			// 
			// label15
			// 
			this.label15.Location = new System.Drawing.Point(356, 227);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(100, 23);
			this.label15.TabIndex = 19;
			this.label15.Text = "kg/m3";
			// 
			// label14
			// 
			this.label14.Location = new System.Drawing.Point(356, 190);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(100, 23);
			this.label14.TabIndex = 18;
			this.label14.Text = "m/s";
			// 
			// label13
			// 
			this.label13.Location = new System.Drawing.Point(356, 137);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(100, 23);
			this.label13.TabIndex = 17;
			this.label13.Text = "m";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(356, 101);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(100, 23);
			this.label12.TabIndex = 16;
			this.label12.Text = "m";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(356, 68);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(100, 23);
			this.label11.TabIndex = 15;
			this.label11.Text = "m";
			// 
			// numericUpDown6
			// 
			this.numericUpDown6.Location = new System.Drawing.Point(230, 262);
			this.numericUpDown6.Maximum = new decimal(new int[] {
			100000,
			0,
			0,
			0});
			this.numericUpDown6.Name = "numericUpDown6";
			this.numericUpDown6.Size = new System.Drawing.Size(120, 22);
			this.numericUpDown6.TabIndex = 13;
			// 
			// numericUpDown5
			// 
			this.numericUpDown5.Location = new System.Drawing.Point(230, 225);
			this.numericUpDown5.Maximum = new decimal(new int[] {
			100000,
			0,
			0,
			0});
			this.numericUpDown5.Name = "numericUpDown5";
			this.numericUpDown5.Size = new System.Drawing.Size(120, 22);
			this.numericUpDown5.TabIndex = 12;
			// 
			// numericUpDown4
			// 
			this.numericUpDown4.Location = new System.Drawing.Point(230, 188);
			this.numericUpDown4.Maximum = new decimal(new int[] {
			100000,
			0,
			0,
			0});
			this.numericUpDown4.Name = "numericUpDown4";
			this.numericUpDown4.Size = new System.Drawing.Size(120, 22);
			this.numericUpDown4.TabIndex = 11;
			// 
			// numericUpDown3
			// 
			this.numericUpDown3.Location = new System.Drawing.Point(230, 135);
			this.numericUpDown3.Maximum = new decimal(new int[] {
			100000,
			0,
			0,
			0});
			this.numericUpDown3.Name = "numericUpDown3";
			this.numericUpDown3.Size = new System.Drawing.Size(120, 22);
			this.numericUpDown3.TabIndex = 10;
			// 
			// numericUpDown2
			// 
			this.numericUpDown2.Location = new System.Drawing.Point(230, 99);
			this.numericUpDown2.Maximum = new decimal(new int[] {
			100000,
			0,
			0,
			0});
			this.numericUpDown2.Name = "numericUpDown2";
			this.numericUpDown2.Size = new System.Drawing.Size(120, 22);
			this.numericUpDown2.TabIndex = 9;
			// 
			// numericUpDown1
			// 
			this.numericUpDown1.Location = new System.Drawing.Point(230, 66);
			this.numericUpDown1.Maximum = new decimal(new int[] {
			100000,
			0,
			0,
			0});
			this.numericUpDown1.Name = "numericUpDown1";
			this.numericUpDown1.Size = new System.Drawing.Size(120, 22);
			this.numericUpDown1.TabIndex = 8;
			// 
			// comboBoxPipeShape
			// 
			this.comboBoxPipeShape.FormattingEnabled = true;
			this.comboBoxPipeShape.Items.AddRange(new object[] {
			"Kołowy",
			"Kwadratowy",
			"Prostokątny"});
			this.comboBoxPipeShape.Location = new System.Drawing.Point(230, 27);
			this.comboBoxPipeShape.Name = "comboBoxPipeShape";
			this.comboBoxPipeShape.Size = new System.Drawing.Size(121, 24);
			this.comboBoxPipeShape.TabIndex = 7;
			this.comboBoxPipeShape.SelectedIndexChanged += new System.EventHandler(this.ComboBoxPipeShapeSelectedIndexChanged);
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(29, 264);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(197, 23);
			this.label7.TabIndex = 6;
			this.label7.Text = "Lepkosc dynamiczna cieczy:";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(29, 227);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(115, 23);
			this.label6.TabIndex = 5;
			this.label6.Text = "Gestosc cieczy:";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(29, 190);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(185, 23);
			this.label5.TabIndex = 4;
			this.label5.Text = "Predkosc przeplywu cieczy:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(29, 137);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(159, 23);
			this.label4.TabIndex = 3;
			this.label4.Text = "Wysokosc rurociagu:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(29, 101);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(159, 23);
			this.label3.TabIndex = 2;
			this.label3.Text = "Szerokosc rurociagu:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(29, 66);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(137, 23);
			this.label2.TabIndex = 1;
			this.label2.Text = "Srednica rurociagu:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(29, 30);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(137, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Ksztalt rurociagu:";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.label9);
			this.groupBox2.Controls.Add(this.label8);
			this.groupBox2.Location = new System.Drawing.Point(12, 321);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(431, 133);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Wyniki obliczen";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(29, 93);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(159, 23);
			this.label9.TabIndex = 1;
			this.label9.Text = "Przeplyw cieczy jest:";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(29, 46);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(100, 23);
			this.label8.TabIndex = 0;
			this.label8.Text = "Re =";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(449, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 2;
			this.button1.Text = "Oblicz";
			this.button1.UseVisualStyleBackColor = true;
			// 
			// MainForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(536, 466);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Name = "MainForm";
			this.Text = "Obliczanie liczby Reynolds\'a";
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown6)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown5)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown4)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown3)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
	}
}
 
0

Nie masz podpiętego zdarzenia. Nie znam tego programu, więc nie podpowiem jak to zrobić.

EDIT:
Zobacz tutaj. To powinno pomóc

0

Męczyłem się kilka godzin, a wystarczyło podpiąć zdarzenie trzema kliknięciami.
Wielkie dzięki za pomoc.

0

To co napisał @drorat1 : zdarzenie nie jest podpięte pod ten przycisk.

0

Bo brakowało linijki w MainForm.Designer.cs:

this.button1.Click += new System.EventHandler(this.button1_Click); 

1 użytkowników online, w tym zalogowanych: 0, gości: 1