lütfen yardım C#

7tepe_cse
13-03-2012, 01:27   |  #1  
OP Taze Üye
Teşekkür Sayısı: 0
11 mesaj
Kayıt Tarihi:Kayıt: Şub 2012

windows form'da 4 tane textbox ve 1 tane de button oluşturdum, 3 textbox a string bir tanesinede integer alıyorum (int.Parse(textbox1.Text)) şeklinde.

button a tıkladığım zamn :


this.TextBox2.Clear();

this.TextBox3.Clear();

this.TextBox4.Clear();
bunlar oluyor ama textBox1.Clear için exceptionhandling error veriyor..Ne yapmalıyım nasıl temizleyecem textbox'ı???

Kapalı Hesap (153535)
13-03-2012, 01:29   |  #2  
Kapalı Hesap
Teşekkür Sayısı: 27
2,297 mesaj
Kayıt Tarihi:Kayıt: Eki 2009

Kodun tamamını paylaşabilir misiniz?

Kapalı Hesap (153535)
13-03-2012, 01:37   |  #3  
Kapalı Hesap
Teşekkür Sayısı: 27
2,297 mesaj
Kayıt Tarihi:Kayıt: Eki 2009

Kod:

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 Forum_Soru_10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       

        private void button2_Click(object sender, EventArgs e)
        {
            string a = textBox1.Text;
            string b = textBox2.Text;
            string c = textBox3.Text;
            int d = Convert.ToInt32(textBox4.Text);
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }
    }
}
Bu şekilde yazdım herhangi bir problem yok.

7tepe_cse
14-03-2012, 05:02   |  #4  
OP Taze Üye
Teşekkür Sayısı: 0
11 mesaj
Kayıt Tarihi:Kayıt: Şub 2012
Alıntı: cs87  
Kodun tamamını paylaşabilir misiniz?
ben textbox'ı direk pars ederek okuduğum için hata veriyordu sanırım (hata veriyordu derken Clear için değilde kullandığım bi foreach döngüsü içinde hata alıordum ama clear ı silince döngü çalışıordu), bende çözüm olarak textbox ı bir stringe alıp onu parse ettim böylece clear sorunu çözülmüş oldu ama başka bi buttonda aynı mantıkla yazdım ama gene foreach döngüsü hata veriyor, kod uzun olduğu için hepsini atmıorum ama lütfen şu kısma bakabilirmisin burdaki hata ne verem olacam artık:  


 private void button4_Click(object sender, EventArgs e)
        {
            bool delete_success = false;
            String s;
            s = this.textBox9.Text;

            if (this.textBox9.Text.Length == 0)
                MessageBox.Show("Enter the ID of patient!" + " Error");
            else
            {
                foreach (Patient p in Patlist)
                {
                    if (p.ID == int.Parse(s))
                    {
                        Patlist.Remove(p);
                        delete_success = true;
                    }
                }
                if (delete_success)
                    MessageBox.Show("Patient has deleted succesfully!");
                else
                    MessageBox.Show("The ID, you have entered is invalid!" + "Error");
                this.textBox9.Clear();
            }
        }

burdaki "foreach" için InvalidOperationException was unhandled dior???..Parantez içindeki "Patient p" Patient Clasına ait bir obje oluyor ve Patlist de bu objeleri tutan List.. benim burda yabmak istediğim kullanıcının textbox a girmiş olduğu ID (integer) yi alıp List içindeki hastalardan hangisine aitse bu id e sahip kişiyi silmek..Lütfen yardımcı olun çok teşekkür ediyorum..

xenamorph
25-03-2012, 17:35   |  #5  
Taze Üye
Teşekkür Sayısı: 0
5 mesaj
Kayıt Tarihi:Kayıt: Oca 2012

foreach ile bir kolleksiyonu döngüye aldığında eğer kolleksiyonun yapısını değiştirirsen foreach bozulur. Programında Patlist.Remove(p) kolleksiyon yapısını bozuyor buda bir sonraki adımda hataya neden oluyor. Eğer silmek istediğin id sadece 1 tane ise, istediğin Patient  nesnesini kolleksiyondan attıktan sonra döngüyü kırabilirsin.
                foreach (Patient p in Patlist)
                {
                    if (p.ID == int.Parse(s))
                    {
                        Patlist.Remove(p);
                        delete_success = true;
                        break;
                    }
                }
Bu hızlı bir çözüm olabilir fakat bu tarz işler için LinQ kullanmak daha doğrudur. İstediğin Patient nesnesini aşağıdaki gibi listeden atabilirsin:
int _id= int.Parse(s);
Patlist.Remove( Patlist.Where(x => x.ID  ==  _id).FirstOrDefault());

Sorunu yukardaki yöntemlerden birisi ile çözebileceğini düşünüyorum.