Szpilona 18 Light Poster

Can you be a little bit more specific? What kind of system have you created? Where the database is located (f. ex. on a remote server or you simply have the file with the database)? etc.

Szpilona 18 Light Poster

as InterestedCompany is a class in the student class you have to create the instance of it... preferably create constructor for student class:

public Student()
{
   this.x = new InterestedCompany();
}

or create an instance like that:

InterestedCompany x = new InterestedCompany();

inside of Student class...
I'm not sure which solution is better... I personally usually for such things create constructor...

Szpilona 18 Light Poster

well... I'm not good in that but:
1. create query more or less like that:

string query = "update tableName set money=" + textbox1.Text.Trim() + "where id=2";

2. construct connection string... if you do not know how to do that use wizard... to do that select from main menu of VS: Data -> Add new data source -> Choose a datasource tylpe (database) -> click New connection and copy the connection string constructed by the wizard...
3. connect to a database and execute query:

using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(query, connection);
                connection.Open();
                SqlDataReader sqlReader = command.ExecuteReader();

                 while (sqlReader.Read())
                 {
                     // check the result
                 }
            }
Szpilona 18 Light Poster

set enable property to false....

Szpilona 18 Light Poster

if it comes to combo box there is no ReadOnly property... so depending on what you want to do you can f.ex. use DropDownStyle property (if you select DropDownList there's no way to put values other than from list) or Enable..

Szpilona 18 Light Poster

answers to questions from private message:
yeap... there's a little error in the code... in the:

private void button1_Click(object sender, EventArgs e)

line 18 - it was:

for (int i = textFromRTB2.Length + 1; i < textFromRTB1.Length; i++)

it should be:

for (int i = textFromRTB2.Length; i < textFromRTB1.Length; i++)

and similarly, line 29... it was:

for (int i = textFromRTB1.Length + 1; i < textFromRTB2.Length; i++)

it should be:

for (int i = textFromRTB1.Length; i < textFromRTB2.Length; i++)

if it comes to other questions there will be NO answer... from my point of view you show no effort - you only try to find somebody to do your job for you... finding that little mistake was very, very easy even for somebody completely new to C#...

Szpilona 18 Light Poster

TextBox class has the property ReadOnly... set it to true... you can do that in the properties window or in the code...

Szpilona 18 Light Poster

I did not have any time to take a closer look to that problem (a lot of work... ;()... so that's probably not the best way to accomplish that, but it works...
enjoy the code:

private void compareLines(int linesCount, string[] textFromRTB1, string[] textFromRTB2)
        {
            bool checker = true;
            for (int i = 0; i < linesCount; i++)
            {
                checker = String.Equals(textFromRTB1[i], textFromRTB2[i]);

                if (!checker)
                {
                    if (textFromRTB1[i] == String.Empty)
                        richTextBox1.SelectionBackColor = Color.Green;
                    else
                        richTextBox1.SelectionBackColor = Color.Red;
                    if (textFromRTB2[i] == String.Empty)
                        richTextBox2.SelectionBackColor = Color.Green;
                    else
                        richTextBox2.SelectionBackColor = Color.Red;
                    richTextBox1.SelectedText = (i + 1).ToString() + ". " + textFromRTB1[i] + "\n";
                    richTextBox2.SelectedText = (i + 1).ToString() + ". " + textFromRTB2[i] + "\n";
                }
                else
                {
                    if (textFromRTB1[i] == String.Empty)
                    {
                        richTextBox1.SelectionBackColor = Color.Green;
                        richTextBox2.SelectionBackColor = Color.Green;
                    }
                    else
                    {
                        richTextBox1.SelectionBackColor = Color.Yellow;
                        richTextBox2.SelectionBackColor = Color.Yellow;
                    }
                    richTextBox1.SelectedText = (i + 1).ToString() + ". " + textFromRTB1[i] + "\n";
                    richTextBox2.SelectedText = (i + 1).ToString() + ". " + textFromRTB2[i] + "\n";
                }
            }
        }

and then:

private void button1_Click(object sender, EventArgs e)
      {
          string[] textFromRTB1 = richTextBox1.Lines;
          string[] textFromRTB2 = richTextBox2.Lines;
          richTextBox1.Clear();
          richTextBox2.Clear();
          int linesCount = 0;

          if (textFromRTB1.Length == textFromRTB2.Length)
              compareLines(textFromRTB1.Length, textFromRTB1, textFromRTB2);
          else
          {
              linesCount = Math.Min(textFromRTB1.Length, textFromRTB2.Length);
              compareLines(linesCount, textFromRTB1, textFromRTB2);

              if (textFromRTB1.Length > textFromRTB2.Length)
              {
                  for (int i = textFromRTB2.Length + 1; i < textFromRTB1.Length; i++)
                  {
                      if (textFromRTB1[i] == String.Empty)
                          richTextBox1.SelectionBackColor = Color.Green;
                      else
                          richTextBox1.SelectionBackColor = Color.Red;
                      richTextBox1.SelectedText = (i + 1).ToString() + ". " + textFromRTB1[i] + "\n";
                  }
              }
              else
              {
                  for (int …
Szpilona 18 Light Poster

So there will be a problem... as far as I know richtextbox does not support selecting and coloring a line - I mean you cannot select a blank line... so any line consisting of "\r", "\n" or "\r\n" is a problem... the easiest workaround for that problem is filling empty line from left to right margin with spaces... but as in your app a user can edit the files using the same richtextboxes it would be very difficult to differentiate between empty lines and actual spaces... so basically in that case you can create your own control which inherit from richtextbox or find good workaround for your app... you can for example provide user with checkbox and one more richtextbox for editing selected file...
btw if you will always leave the lines' numbers visible the problem will be very easily solved - for empty lines only the line numbers will be highlighted...

Szpilona 18 Light Poster

I feel a little bit like doing your homework but OK...
Some questions:
1. what exactly do you want to do? you just want to compare 2 files and show the differences? or you give users the possibility to change the text in the boxes and compare the text?
2. do you need some fancy text formating like changing the font color, shape or size?
3. if it comes to empty line highlighting - you have to highlight the whole line or it not really important - you can highlight half of the line as well?
I need more details... but if you really need all of sophisticated features of richtextbox it can be difficult to achieve...

Szpilona 18 Light Poster

You do not know the proper definition of that operator.
The syntax:
boolean_expression ? expression1 : expression2
Expression is any operation which returns a value. Void is not a value. Void specifies that no value is returned. If your get this message, at least one of your methods, must return void value. What is more you must in that context assign the value returned by the "?:" operator.
Proper usage of that operator:

private int m1(int a)
        {
            return a;
        }
        private int m2(int a)
        {
            return a + a;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < 10; i++)
            {
                j = (i > 5) ? m1(i) : m2(i);
            }
        }

If you don't want to use a value returned by the operator - you just want to decide which method to execute, use if - else or switch - case expressions.

ddanbe commented: Well explained. +6
Szpilona 18 Light Poster

That's very easy... All you have to do is to add a boolean variable which will remember the user's choice (you can use for example a combobox to give a user the chance to choice). Then according to that variable you add the line number or not.
Sample code:

private void button1_Click(object sender, EventArgs e)
        {
            string[] textFromRTB1 = richTextBox1.Lines;
            string[] textFromRTB2 = richTextBox2.Lines;
            richTextBox1.Clear();
            richTextBox2.Clear();
            int linesCount = 0;
            bool lineNumVisible;
            if (comboBox1.SelectedIndex == 0)
                lineNumVisible = true;
            else
                lineNumVisible = false;

            if (textFromRTB1.Length != textFromRTB2.Length)
            {
                linesCount = Math.Min(textFromRTB1.Length, textFromRTB2.Length);
                MessageBox.Show("Boxes have different number of lines.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                linesCount = textFromRTB1.Length;

            bool checker = true;
            for (int i = 0; i < linesCount; i++)
            {
                checker = String.Equals(textFromRTB1[i], textFromRTB2[i]);

                if (!checker)
                {
                    richTextBox1.SelectionBackColor = Color.Red;
                    richTextBox2.SelectionBackColor = Color.Red;
                    if (lineNumVisible)
                    {
                        richTextBox1.SelectedText = (i + 1).ToString() + ".  " + textFromRTB1[i] + "\n";
                        richTextBox2.SelectedText = (i + 1).ToString() + ".  " + textFromRTB2[i] + "\n";
                    }
                    else
                    {
                        richTextBox1.SelectedText = textFromRTB1[i] + "\n";
                        richTextBox2.SelectedText = textFromRTB2[i] + "\n";
                    }
                }
                else
                {
                    richTextBox1.SelectionBackColor = Color.Green;
                    richTextBox2.SelectionBackColor = Color.Green;
                    if (lineNumVisible)
                    {
                        richTextBox1.SelectedText = (i + 1).ToString() + ".  " + textFromRTB1[i] + "\n";
                        richTextBox2.SelectedText = (i + 1).ToString() + ".  " + textFromRTB2[i] + "\n";
                    }
                    else
                    {
                        richTextBox1.SelectedText = textFromRTB1[i] + "\n";
                        richTextBox2.SelectedText = textFromRTB2[i] + "\n";
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // set the default value of the combobox
            comboBox1.SelectedIndex = 0;
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            richTextBox2.Clear();
            richTextBox1.SelectionBackColor = Color.White;
            richTextBox2.SelectionBackColor = Color.White;
        }
Szpilona 18 Light Poster

ListBox class does not support editing items in runtime (at least I do not know anything about it ;-P). So you have to handle yourself... As you want to give the user opportunity to change data after double clicking use DoubleClick event of the ListBox class and add in there the code. Depending on how you want a user to provide data to the app it will look differently.
Probably the simplest way to achieve what you want is to add TextBox control to the form. Then the DoubleClick event can look somehow like this:

private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            int selectedElement = listBox1.SelectedIndex;
            listBox1.Items[selectedElement] = editText.Text;
        }

To be more sophisticated you can hide the textbox and show it only when it will be needed.
Sample code:

public partial class Form1 : Form
    {
        private TextBox editText;
        private int selectedElement;

        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            selectedElement = listBox1.SelectedIndex;
            editText.Show();
            editText.Focus();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            editText = new TextBox();
            editText.Location = new Point(10, 0);
            editText.Size = new Size(100, 20);
            editText.Text = "";
            editText.Hide();
            listBox1.Controls.Add(this.editText);
            editText.LostFocus += new System.EventHandler(this.FocusLost);
        }

        private void FocusLost(object sender, System.EventArgs e)
        {
            listBox1.Items[selectedElement] = editText.Text;
            editText.Text = "";
            editText.Hide();
        }
    }

Having that you have to just work on the specific location of the textbox when it shows and other less important details...

Szpilona 18 Light Poster

I've got no time to give you more elaborate example but the following code should give you the idea what to do:

private void button1_Click(object sender, EventArgs e)
        {
            string[] textFromRTB1 = richTextBox1.Lines;
            string[] textFromRTB2 = richTextBox2.Lines;
            richTextBox1.Clear();
            richTextBox2.Clear();
            int linesCount = 0;

            if (textFromRTB1.Length != textFromRTB2.Length)
            {
                linesCount = Math.Min(textFromRTB1.Length, textFromRTB2.Length);
                MessageBox.Show("Boxes have different number of lines.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                linesCount = textFromRTB1.Length;

            bool checker = true;
            for (int i = 0; i < linesCount; i++)
            {
                checker = String.Equals(textFromRTB1[i], textFromRTB2[i]);

                if (!checker)
                {
                    richTextBox1.SelectionBackColor = Color.Red;
                    richTextBox2.SelectionBackColor = Color.Red;
                    richTextBox1.SelectedText = textFromRTB1[i] + "\n";
                    richTextBox2.SelectedText = textFromRTB2[i] + "\n";
                }
                else
                {
                    richTextBox1.SelectionBackColor = Color.Green;
                    richTextBox2.SelectionBackColor = Color.Green;
                    richTextBox1.SelectedText = textFromRTB1[i] + "\n";
                    richTextBox2.SelectedText = textFromRTB2[i] + "\n";
                }
            }
        }

To do more sophisticated comparisons use regular expressions (class Regex and e.x. Match).
If you want to parse the text when it is written you can use TextChanged Event...