Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > .NET Frameworks (точка нет фреймворки) > Общие вопросы .NET
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 01.06.2010, 17:26   #1
Tmaxxx
 
Аватар для Tmaxxx
 
Регистрация: 12.05.2010
Сообщений: 6
Сообщение Исправите ошибку, C#

Код:
private void button1_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            int i = 0;
            int fnd = 0;
            if ((convert == true)) 
            {
                foreach (item in listBox2.Items) 
                {
                    if ((item.ToString == textBox1.Text)) 
                    {
                        textBox2.Text = listBox1.Items.item[i].ToString;
                        fnd = 1;
                    }
                i++;
            }
            
                if ((fnd == 1)) 
                {
                    label4.Text = "Translation Complete!";
                }
                else 
                {
                    label4.Text = "Error: Unable to translate!";
                }
            }
            else
            {
                foreach (item in listBox1.Items) 
                {
                    if ((item.ToString == textBox1.Text)) 
                    {
                        textBox2.Text = listBox2.Items.item[i].ToString;
                        fnd = 1;
                    }
                i++;
                }

                if ((fnd == 1)) 
                {
                    label4.Text = "Translation Complete!";
                }
                else 
                {
                    label4.Text = "Error: Unable to translate!";
                }
            }
       }
Ошибка в переменной item! Как ее исправить, подскажите, пожалуста)
I'ma firin' ma lazer!!!
Tmaxxx вне форума Ответить с цитированием
Старый 01.06.2010, 17:39   #2
Stilet
Белик Виталий :)
Старожил
 
Аватар для Stilet
 
Регистрация: 23.07.2007
Сообщений: 57,097
По умолчанию

Цитата:
foreach (ListItem item in listBox2.Items)
Или
Цитата:
foreach (var item in listBox2.Items)
Понятно?
I'm learning to live...
Stilet вне форума Ответить с цитированием
Старый 01.06.2010, 19:02   #3
Tmaxxx
 
Аватар для Tmaxxx
 
Регистрация: 12.05.2010
Сообщений: 6
По умолчанию

а как насчет:
Цитата:
textBox2.Text = listBox2.Items.item[i].ToString;
там перечисление элементов с индексом i. Там тоже есть ошибка.

а вот здесь не понятно какой тип ставить! скорее всего должен быть object. Но если стоит object, выделенный метод не работает r.Peek()
Цитата:
/*object*/StreamReader r = new StreamReader((Directory.GetCurrentD irectory() + "@eng.txt"));
while (r.Peek() > -1)
{
r2 = r.ReadLine();
listBox1.Items.Add(r2);
}

а в общем программа выглядит так:
Код:
public partial class Form1 : Form
    {
        private System.IO.StreamReader r;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string r2;
            comboBox1.SelectedIndex = 0;
            label4.Text = "";
            if (File.Exists((Directory.GetCurrentDirectory() + "@eng.txt")))
            {
                /*object*/StreamReader r = new StreamReader((Directory.GetCurrentDirectory() + "@eng.txt"));
                while (r.Peek() > -1)
                {
                    r2 = r.ReadLine();
                    listBox1.Items.Add(r2);
                }
                r.Close();
            }
            if (File.Exists((Directory.GetCurrentDirectory() + "@rus.txt")))
            {
                /*object*/StreamReader r = new StreamReader((Directory.GetCurrentDirectory() + "@rus.txt"));
                while (r.Peek() > -1)
                {
                    r2 = r.ReadLine();
                    listBox2.Items.Add(r2);
                }
                r.Close();
            }
        }

        private bool convert;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            if ((comboBox1.SelectedIndex == 0))
            {
                label1.Text = "English:";
                label2.Text = "Russian:";
                convert = false;
            }
            else
            {
                label1.Text = "Russian:";
                label2.Text = "English:";
                convert = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            int i = 0;
            int fnd = 0;
            if ((convert == true)) 
            {
                foreach (item in listBox2.Items) 
                {
                    if ((item.ToString == textBox1.Text)) 
                    {
                        textBox2.Text = listBox1.Items.item[i].ToString;
                        fnd = 1;
                    }
                i++;
            }
            
                if ((fnd == 1)) 
                {
                    label4.Text = "Translation Complete!";
                }
                else 
                {
                    label4.Text = "Error: Unable to translate!";
                }
            }
            else
            {
                foreach (item in listBox1.Items) 
                {
                    if ((item.ToString == textBox1.Text)) 
                    {
                        textBox2.Text = listBox2.Items.Item[i].ToString;
                        fnd = 1;
                    }
                i++;
                }

                if ((fnd == 1)) 
                {
                    label4.Text = "Translation Complete!";
                }
                else 
                {
                    label4.Text = "Error: Unable to translate!";
                }
            }
       }
    }
I'ma firin' ma lazer!!!
Tmaxxx вне форума Ответить с цитированием
Старый 01.06.2010, 20:47   #4
Tmaxxx
 
Аватар для Tmaxxx
 
Регистрация: 12.05.2010
Сообщений: 6
По умолчанию

Цитата:
Сообщение от Stilet Посмотреть сообщение
Или

Понятно?
И не один из вариантов не пашет!((((
I'ma firin' ma lazer!!!
Tmaxxx вне форума Ответить с цитированием
Старый 02.06.2010, 11:55   #5
Casper-SC
Форумчанин
 
Регистрация: 04.07.2007
Сообщений: 549
По умолчанию

Твой код:
Код:
textBox2.Text = listBox2.Items.Item[i].ToString;
Надо как минимум так:
Код:
textBox2.Text = listBox2.Items.ToString();
() - значит, что это метод, а не переменная

Воссоздал по коду проект и всё равно не совсем понятно, что он делает, а гадать, разбираться, честно говоря лень и нет времени. Скриншот программы не помешал бы и объяснение краткое, что она делает.

Код:
foreach (item in listBox1.Items) 
                {
                    if ((item.ToString == textBox1.Text)) 
                    {
                        textBox2.Text = listBox2.Items.item[i].ToString;
                        fnd = 1;
                    }
                i++;
                }
Так же:
Код:
foreach (var item in listBox1.Items) 
           {
               if ((item.ToString() == textBox1.Text)) 
               {
                    textBox2.Text = listBox2.Items[i].ToString();
                    fnd = 1;
               }
          i++;
                }
Твой код:
Код:
 if (File.Exists((Directory.GetCurrentDirectory() + "@eng.txt")))
Так:
Код:
if (File.Exists((Directory.GetCurrentDirectory() + @"\eng.txt")))
Или так:
Код:
if (File.Exists((Directory.GetCurrentDirectory() + "\\eng.txt")))
А вообще выложи проект, проще будет.

И кстати, чтобы русский текст из текстового файла грузился в листбокс и нормально отображался там, сохраняй текстовый файл в UTF8, а не как там стоит по умолчанию ASCII

==========================
В принципе уже не надо ничего:
Код:
...
namespace Project2
{
    public partial class Form1 : Form
    {
        private System.IO.StreamReader r;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string r2;
            comboBox1.SelectedIndex = 0;
            label4.Text = "";
            if (File.Exists((Directory.GetCurrentDirectory() + "\\eng.txt")))
            {
                /*object*/
                StreamReader r = new StreamReader((Directory.GetCurrentDirectory() + "\\eng.txt"));
                while (r.Peek() > -1)
                {
                    r2 = r.ReadLine();
                    listBox1.Items.Add(r2);
                }
                r.Close();
            }
            if (File.Exists((Directory.GetCurrentDirectory() + "\\rus.txt")))
            {
                /*object*/
                StreamReader r = new StreamReader((Directory.GetCurrentDirectory() + "\\rus.txt"));
                while (r.Peek() > -1)
                {
                    r2 = r.ReadLine();
                    listBox2.Items.Add(r2);
                }
                r.Close();
            }
        }

        private bool convert;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            if ((comboBox1.SelectedIndex == 0))
            {
                label1.Text = "English:";
                label2.Text = "Russian:";
                convert = false;
            }
            else
            {
                label1.Text = "Russian:";
                label2.Text = "English:";
                convert = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            int i = 0;
            Boolean fnd = false;
            if (convert) 
            {
                foreach (var item in listBox2.Items) 
                {
                    if ((item.ToString() == textBox1.Text)) 
                    {
                        textBox2.Text = listBox1.Items[i].ToString();
                        fnd = true;
                    }
                i++;
            }
            
                if (fnd) 
                {
                    label4.Text = "Translation Complete!";
                }
                else 
                {
                    label4.Text = "Error: Unable to translate!";
                }
            }
            else
            {
                foreach (var item in listBox1.Items) 
                {
                    if ((item.ToString() == textBox1.Text)) 
                    {
                        textBox2.Text = listBox2.Items[i].ToString();
                        fnd = true;
                    }
                i++;
                }

                if (fnd) 
                {
                    label4.Text = "Translation Complete!";
                }
                else 
                {
                    label4.Text = "Ошибка: Unable to translate!";
                }
            }
       }
    }
}

Последний раз редактировалось Casper-SC; 02.06.2010 в 12:30. Причина: Проблема решена
Casper-SC вне форума Ответить с цитированием
Старый 02.06.2010, 20:27   #6
Tmaxxx
 
Аватар для Tmaxxx
 
Регистрация: 12.05.2010
Сообщений: 6
По умолчанию

Спасибо, Casper-SC, вроде работает так как надо!
I'ma firin' ma lazer!!!
Tmaxxx вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Выдаёт ошибку Stanislava7 Общие вопросы C/C++ 1 05.05.2010 17:55
Укажите ошибку kerbeross PHP 7 25.08.2009 21:47
Выдаёт ошибку: acos DOMAIN error,полсе нажатия окей,ещё одну ошибку pow OWERFLAW ERROR prikolist Общие вопросы C/C++ 4 10.04.2009 20:27
Указать на ошибку valerij Microsoft Office Excel 15 26.10.2008 13:31
Исправить ошибку LLIypLLIyH Помощь студентам 5 16.06.2008 16:50