Object reference error on merge sort application via GUI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shammishetty
    New Member
    • Oct 2011
    • 1

    Object reference error on merge sort application via GUI

    My errors are:
    Error 2 An object reference is required for the non-static field, method, or property 'home.Form9.Two TextBox(int, int)'


    Error 1 An object reference is required for the non-static field, method, or property 'System.Windows .Forms.Control. Invoke(System.D elegate)'


    Code:
    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;
    using System.Threading;
    namespace home
    {
        public partial class Form9 : Form
        {
            public Form9()
            {
                InitializeComponent();
            }
    
            private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
            {
    
            }
            Thread th;
            private void button1_Click(object sender, EventArgs e)
            {
              
             Invoke((MethodInvoker)delegate { test1(); });
              Thread.Sleep(600);
    
             
    
            }
              string str;
            string[] strArr;
            private void test1()
            {
                TableLayoutPanel tlp = new TableLayoutPanel();
                tlp.Name = "tlp";
                tlp.AutoSize = true;
                
    
                Label lb = new Label();
             
           
                int count = 0;
                str = textBox1.Text;
                
                char[] splitchar = { ' ' };
    
                strArr = str.Split(splitchar);
                for (count = 0; count <= strArr.Length - 1; count++)
                {
                    Label lbl = new Label();
                    lb.Name = "lab1" + count.ToString();
                    lbl.Width = 20;
                    lbl.Text = count.ToString();
    
                    tlp.Controls.Add(lbl, count, 0);
    
                    TextBox tb = new TextBox();
                    tb.Name = "txt" + count.ToString();
                    tb.BorderStyle = BorderStyle.FixedSingle;
    
    
                    tb.Width = 35;
                    //ar[i] = i;
                    //tb.Text = ar[i].ToString();
                    //   tb.Text = textBox1.Text;
                    tb.Text = strArr[count];
    
                    tlp.Controls.Add(tb, count, 1);
    
                    Label lbb = new Label();
                    lbb.Name = "lab" + count.ToString();
                    lbb.Width = 20;
    
                    tlp.Controls.Add(lbb, count, 2);
    
                    flowLayoutPanel1.Controls.Add(tlp);
                }
              
                Sort(0, strArr.Length - 1);
    
            }
            public static void Sort( int left, int right)
            {
    
         
                if (left < right)
                {
                    Invoke((MethodInvoker)delegate { TwoTextBox(left, right); });
    
                    
                    int middle = (left + right) / 2;
    
                    Sort(left, middle);
    
                    //Sort(data, middle + 1, right);
                    //Merge(data, left, middle, middle + 1, right);
                }
            }
    
            private 
                void TwoTextBox(int ac, int ac1)
            {
                string a = ac.ToString();
                string b = ac1.ToString();
                string[] ab = { a, b };
                for (int i =ac; i <=ac1; i++)
                {
                    Control[] controls = flowLayoutPanel1.Controls.Find("txt" + ab[i], true);
                    foreach (Control c in controls)
                    {
                        if (c is TextBox)
                        {
    
                            c.BackColor = System.Drawing.Color.Red;
    
                        }
    
                    }
    
    
                }
    
            }      
            
    
        }
    }
    Last edited by Niheel; Oct 29 '11, 08:24 AM.
  • Maraj
    New Member
    • Nov 2011
    • 24

    #2
    Hy shammishetty.Th ere are two things you can do.
    1.You can create an instance of class and then call methods or
    2.You can define Twotextbox() and test1() as static methods.

    Comment

    Working...