i seem to get stuck a lot but... I want to have a method displayData() that when called will change richtTextBox.Text to a string value I have. But I don't know how to access the richt text box from within my method. I tried changing it to public using its properties but that didn't work . And Form3.richTextBox doesn't work either. I have a feeling this isn't actually possible, so if not what should I do to work around this problem?

Recommended Answers

All 11 Replies

It is possible. You must first create an instance of the form you are trying to access, and then call the object in the form:

Form3 form3 = new Form3()
form3.richTextBoxInstance.Text = "new Text"; //richTextBoxInstance should be the name of the object

Remember you must call the richTextBox through its name in the form, not by object type.

I thought of that but when I use this method there is a flicker and hte page keeps loading in different areas, its really... annoying! is there a way to stop that problem?

I suspect you have something else going on at that time. We'll need the code in the form your accessing the RichTextBox, and the code for the form that contains the RichTextBox.

Its the same form, the text box is on form3 and I want to update the text box on form3 after a method call. My current code creates a new instance of form3 when the variables are updated and on load it displays these variable values from the Data.cs class.

i think you have to make getter and setter functions for specific textbox and then use them to get and set values . i think there is a property of modifier , it is private bydefault change it to public try it.

Show the code you're using

Try to declare a new object of richtextbox.
then u can access all of properies and methods of richtextbox

Ok, here is the form:

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.IO;
using System.Data.Common;
using System.Data.OleDb;  

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        // Variables------------------------------------------------------------------------------------------------------------------------

        // when cleaning up the program move these into the Data class along with NavigateRecords() method 
        //public static string make;
        //public static string model;
        //public static string registration;
        //public static string mileage;
        //public static string yearMade;
        //public static string price;
        //public static string imageNav;

        private static int IndexToLoad;

        // Program Logic Below -------------------------------------------------------------------------------------------------------------       

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            // This will use the Data.Search() method to jump to a specific row in the dataset ds1
            IndexToLoad= Data.search(txtBoxReg.Text);

            if (IndexToLoad == Data.numOfRowsCars)
            {
                // Then the registration hasn't been found
                MessageBox.Show("Registration not found!");
            }
            else
            {
                // Then make a call to NavigateRecords() method with the new Index to load the appropriate data
                Data.Index = IndexToLoad;
                Data.NavigateRecords(Data.Index);
                Data.updateForm3();
                this.Hide();
            }
        }



        private void Form3_Load(object sender, EventArgs e)
        {
            // on form load call method to load data
            Data.NavigateRecords(Data.Index);

           // display the details 

           richTxtBoxDetails.Text = "Details: " + '\n' + 
                                    "Make: " + Data.make + '\n' +
                                    "Model: " + Data.model + '\n' +
                                    "Registration: " + Data.registration + '\n' +
                                    "Milage: " + Data.mileage + '\n' +
                                    "Year Made: " + Data.yearMade + '\n' + 
                                    "Price: " + Data.price + '\n' + 
                                    "Car Photo: " + Data.imageNav;

           // load the image to display // if applicable

           try
           {
               if (Data.imageNav != null)
               {
                   picBoxCar.Image = Image.FromFile(Data.imageNav);
               }
           }
           catch (FileNotFoundException)
           {
               // do nothing
           }

        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            // decrement Index the navigation index. 

            if (Data.Index > 0)
            {
                Data.Index -=1;
                Data.NavigateRecords(Data.Index);

                // then call the updateForm() method
                Data.updateForm3();
                this.Hide();
            }
            else
            {
                MessageBox.Show("This is the first record held");
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            // increment the Index 
            if (Data.Index < Data.numOfRowsCars-1)
            {
                Data.Index += 1;
                Data.NavigateRecords(Data.Index);

                // then call the updateForm() method and hide the current instance
                Data.updateForm3();
                this.Hide();
            }
            else
            {
                MessageBox.Show("This is the last record held");
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Hide();
            loadForms.loadWelcome();
        }                
    }
}

And here is the Data class:

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.IO;
using System.Data.Common;
using System.Data.OleDb;  


namespace WindowsFormsApplication1
{
    // This class will contain methods and fields needed to handle the data. 
    class Data
    {
        // Fields -----------------------------------------------------------------------------------------------------------------

        private static System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
        private static System.Data.OleDb.OleDbDataAdapter da;
        private static System.Data.OleDb.OleDbCommandBuilder cb = new System.Data.OleDb.OleDbCommandBuilder(da);

        public static DataSet ds1; // tblCars
        public static DataSet ds2; //tblStaff

        private static string sql;
        private static string dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;";
        private static string dbSource = "Data Source = E:\\cars.accdb";

        private static string userName;
        private static string userPin;

        private static string reg;

        private static bool valid;

        public static int numOfRowsCars;
        public static int numOfRowsStaff;

        public static int Index =0;

        public static string make;
        public static string model;
        public static string registration;
        public static string mileage;
        public static string yearMade;
        public static string price;
        public static string imageNav;






        // Methods -----------------------------------------------------------------------------------------------------------------

        public static void loadDB()
        {
            // load data into datasets  - this will be called on form2 the login screen            

             con.ConnectionString = dbProvider + dbSource;
             loadTblStaff();
             loadTblCars();


        }

        public static void loadTblStaff()
        {
            // prepare, open and load the staff table into dataset ds2

            con.Open();

                sql = "SELECT * FROM tblStaff";
                ds2 = new DataSet();

                da = new OleDbDataAdapter(sql, con);
                numOfRowsStaff = da.Fill(ds2, "tblStaff");

            con.Close();

        }

        public static void loadTblCars()
        {
            // prepare, open and load the cars table into dataset ds1

            con.Open();

                sql = "SELECT * FROM tblCars";
                ds1 = new DataSet();

                da = new OleDbDataAdapter(sql, con);
                numOfRowsCars = da.Fill(ds1, "tblCars");

            con.Close();
        }       

        public static int Validate(string Name, string Pin)
        {
            // this method will be used to validate login credentials - it will return 0 for invalid, 1 for staff privalages and 2 for admin

            // check to see if log in details are valid
            for (int count = 0; count < numOfRowsStaff; count++)
            {
                userName = ds2.Tables["tblStaff"].Rows[count][0].ToString();
                userPin = ds2.Tables["tblStaff"].Rows[count][1].ToString();

               // MessageBox.Show("username: " + userName + '\n' + "Pin Number: " + userPin);

                if (Name == userName && Pin == userPin)
                {
                    valid = true;
                    break;
                }
                else
                {
                    valid = false;
                }
            }          



            if (valid && Pin == "9999")
            {
                // returning 2 will load the admin screen
               return 2;
            }
            else if (valid)
            {
                // returning 1 will load the staff screen
               return 1;
            }
            else
            {
                // returning 0 will clear the input fields and return an error message
                return 0;
            }
        }

        public static int search(string Registration)
        {
            // this method will search for registration, if found it will return the index value , else it will return max value + 1          

            for (int count = 0; count < numOfRowsCars; count++)
            {
                reg = ds1.Tables["tblCars"].Rows[count][2].ToString();
                //MessageBox.Show(reg);
                if (Registration == reg)
                {
                    // Then return the index of the registration number                     
                    return count;
                }               
            }
            return numOfRowsCars;                
        }

        public static void updateForm3()
        {
            // This method will create a new instance of Form3 with the update value of the index - this method will be called any time data needs changed
            Form3 updated = new Form3();
            updated.Show();

        }

        public static void updateForm4()
        {
            // This method will create a new instance of Form3 with the update value of the index - this method will be called any time data needs changed
            Form4 updated = new Form4();
            updated.Show();

        }

        public static void NavigateRecords(int Index)
        {
            // This method will be used to load the appropriate values - It will take an index as its parameters

            // load the appropriate data
           make = ds1.Tables["tblCars"].Rows[Index][0].ToString();
           model = ds1.Tables["tblCars"].Rows[Index][1].ToString();
           registration = ds1.Tables["tblCars"].Rows[Index][2].ToString();
           mileage = ds1.Tables["tblCars"].Rows[Index][3].ToString();
           yearMade = ds1.Tables["tblCars"].Rows[Index][4].ToString();
           price = ds1.Tables["tblCars"].Rows[Index][5].ToString();
           imageNav = ds1.Tables["tblCars"].Rows[Index][6].ToString();


        }



        public static void insertStaffRecord(string Name, string Password)
        {
            // This method will be used on Form4 to insert a new row into the staff table

            // create the row and add it to the dataset
            DataRow staffRow = ds2.Tables["tblStaff"].NewRow();
            staffRow[0] = Name;
            staffRow[1] = Password;

            ds2.Tables["tblStaff"].Rows.Add(staffRow);

            // update the database
            da.Update(ds2, "tblStaff");
        }

        public static void insertCarRecord(string Make,string Model,string Registration, string Mileage, string yearMade, string Price, string CarPhoto)
        {
            // This method will be used on Form4 to insert a new row into the cars table 

            // create the row and add it to the dataset
            DataRow carsRow = ds1.Tables["tblCars"].NewRow();
            carsRow[0] = Make;
            carsRow[1] = Model;
            carsRow[2] = Registration;
            carsRow[3] = Mileage;
            carsRow[4] = yearMade;
            carsRow[5] = Price;
            carsRow[6] = CarPhoto;

            ds1.Tables["tblCars"].Rows.Add(carsRow);

            // update the database
            da.Update(ds1, "tblCars");
        }

        public static void deleteRecord()
        {
            // This method will be used to delete a row of data from the cars table
            ds1.Tables["tblCars"].Rows[Index].Delete();

            //update the database
            da.Update(ds1, "tblCars");
        }

        public static void clearValues()
        {
            // THis method will set all data to blank for loading the admin page

            make = "";
            model = "";
            registration = "";
            mileage = "";
            yearMade = "";
            price = "";
            imageNav = "";
        }
    }
}

incidentally I am also having trouble with the updating of the database in the data class, not sure why.

A couple of options come to mind. One have your method return a string then richtextbox1.text = displayData() Option 2 pass the richtextbox by reference to the method, then you can fill it with the string value.

declare

public static void displayData(ref RichTextBox rtb)
{
    string MyString = "Test 1";
    rtb.Text = NyString;
}

call

Data.displayData(ref richtextbox1)

I don't understand what you're doing here? I tried to copy the code thinking mayeb i will understand when I see it but im unsure where to make the displayData() call and what to pass to it?

p.s sorry for the long absense , had things to do.

You put the void procedure in your Data class. then call it like shown.

I think part of your problem, is you're opening a new form3 from form3 then hiding the original form3. It seems to me you might want to consider using an M(ultiple)D(ocument)I(nterface) form, and open and close these different forms from the parent. This will simplify what you are doing, and make it easier to achieve your final goal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.