C# "button_click" 2 kere çalışıyor

emirhan31ke

Centipat
Katılım
27 Aralık 2022
Mesajlar
83
Çözümler
1
Daha fazla  
Cinsiyet
Erkek
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Streamdeck_deneme
{
public partial class Form1 : Form
{
OpenFileDialog openFileDialog1 = new OpenFileDialog(); // OpenFileDialog nesnesini formun dışında tanımlıyoruz

public Form1()
{
InitializeComponent();
this.KeyPreview = true; // Formun tuş vuruşlarını algılamasını sağlar
this.KeyDown += Form1_KeyDown; // KeyDown olayına olay işleyicisi ekler
}
private void xdosya()
{
// Dosya seçme penceresinden seçilen dosyanın yolu boş değilse
if (!string.IsNullOrEmpty(openFileDialog1.FileName))
{
// Dosyayı çalıştır
Process.Start(openFileDialog1.FileName);
}
else
{
// Kullanıcı henüz dosya seçmediği için bir uyarı mesajı göster
MessageBox.Show("önce bir dosya seçin.");
}
}

private void button1_Click(object sender, EventArgs e)
{
// Dosya seçim penceresini göster ve kullanıcının seçimini al
DialogResult result = openFileDialog1.ShowDialog();

// Kullanıcı bir dosya seçtiyse ve "Tamam" düğmesine bastıysa
if (result == DialogResult.OK)
{
// Seçilen dosyanın yolunu al
string filePath = openFileDialog1.FileName;

// Dosya yolunu ekrana yazdır (opsiyonel)
Console.WriteLine("Seçilen dosya: " + filePath);
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// Eğer basılan tuş "X" ise
if (e.KeyCode == Keys.X)
{
xdosya();

}
}
}
}


Kod olması gerektiği gibi çalışıyor dosyayı seçiyorum X e basıyorum seçtiğim dosyayı 2 kere açıyor. Nasıl çözebilirim?
 
Son düzenleyen: Moderatör:
this.KeyDown += Form1_KeyDown; satırı ile keydown olayı iki defa listeye ekleniyor olabilir. Bu işlemi form üzerinde manuel olarak yapamıyor musun? Veya += operatörü yerine sadece = operatörü kullanınca olmuyor mu?
 
Son düzenleme:
Şunu dener misiniz:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Streamdeck_deneme {
    public partial class Form1 : Form {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        public Form1() {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyDown += Form1_KeyDown;
        }

        private void xdosya() {
            if (!string.IsNullOrEmpty(openFileDialog1.FileName)) {
                Process.Start(openFileDialog1.FileName);
            } else {
                MessageBox.Show("Önce bir dosya seçin.");
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK) {
                string filePath = openFileDialog1.FileName;
                Console.WriteLine("Seçilen dosya: " + filePath);
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.X) {
                xdosya();
            }
        }
    }
}
 
Şunu dener misiniz:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Streamdeck_deneme {
 public partial class Form1 : Form {
 OpenFileDialog openFileDialog1 = new OpenFileDialog();

 public Form1() {
 InitializeComponent();
 this.KeyPreview = true;
 this.KeyDown += Form1_KeyDown;
 }

 private void xdosya() {
 if (!string.IsNullOrEmpty(openFileDialog1.FileName)) {
 Process.Start(openFileDialog1.FileName);
 } else {
 MessageBox.Show("Önce bir dosya seçin.");
 }
 }

 private void button1_Click(object sender, EventArgs e) {
 DialogResult result = openFileDialog1.ShowDialog();

 if (result == DialogResult.OK) {
 string filePath = openFileDialog1.FileName;
 Console.WriteLine("Seçilen dosya: " + filePath);
 }
 }

 private void Form1_KeyDown(object sender, KeyEventArgs e) {
 if (e.KeyCode == Keys.X) {
 xdosya();
 }
 }
 }
}

Form açılmadı :/?
 
Form açılmadı :/?
Bunu dener misiniz:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Streamdeck_deneme
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public partial class Form1 : Form
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyDown += Form1_KeyDown;
        }

        private void xdosya()
        {
            if (!string.IsNullOrEmpty(openFileDialog1.FileName))
            {
                System.Diagnostics.Process.Start(openFileDialog1.FileName);
            }
            else
            {
                MessageBox.Show("Önce bir dosya seçin.");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                Console.WriteLine("Seçilen dosya: " + filePath);
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.X)
            {
                xdosya();
            }
        }
    }
}
 
@Canefşan
Private Void form1_keypress(object sender, keypresseventargs e)
{
İf (e. Keycode == keys.X)
{
// Dosya seçme penceresinden seçilen dosyanın yolu boş değilse.
İf (!string. Isnullorempty(openfiledialog1.filename))
{
// Dosyayı çalıştır.
Process. Start(openfiledialog1.filename);
}
Else.
{
// Kullanıcı henüz dosya seçmediği için bir uyarı mesajı göster.
Messagebox. Show("lütfen önce bir dosya seçin.");
}
}
}
"İf (e. Keycode == keys.X)" yazan yerde keycode hatalı.

1711642613339.png


Bunu dener misiniz:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Streamdeck_deneme.
{
 static class Program.
 {
 [STAThread]
 static void Main()
 {
 Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new Form1());
 }
 }

 public partial class Form1 : Form.
 {
 OpenFileDialog openFileDialog1 = new OpenFileDialog();

 public Form1()
 {
 InitializeComponent();
 this.KeyPreview = true;
 this.KeyDown += Form1_KeyDown;
 }

 private void xdosya()
 {
 if (!string.IsNullOrEmpty(openFileDialog1.FileName))
 {
 System.Diagnostics.Process.Start(openFileDialog1.FileName);
 }
 else.
 {
 MessageBox.Show("Önce bir dosya seçin.");
 }
 }

 private void button1_Click(object sender, EventArgs e)
 {
 DialogResult result = openFileDialog1.ShowDialog();

 if (result == DialogResult.OK)
 {
 string filePath = openFileDialog1.FileName;
 Console.WriteLine("Seçilen dosya: " + filePath);
 }
 }

 private void Form1_KeyDown(object sender, KeyEventArgs e)
 {
 if (e.KeyCode == Keys.X)
 {
 xdosya();
 }
 }
 }
}

Çalışmıyor.
 
Son düzenleme:

Yeni konular

Geri
Yukarı