Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends

I Want to make function in diffrent class(out side my form) for my first-Next-Previous-Last buttons for my All forms with same buttons .

How should i do it ? no Idea to access textboxes in other class.


My Code is here for Next button !!

C#
private void CmdNext_Click(object sender, EventArgs e)
       {

           ConStr Constring = new ConStr();
           String CS = Constring.ConString("DBCS"); 
           using (SqlConnection con = new SqlConnection(CS))
           {
               SqlDataAdapter DA = new SqlDataAdapter("Select * from FeesHead order by ID", con);

               DataSet DS = new DataSet();
               DA.Fill(DS, "FeesHead");
               
               if ( i < DS.Tables["FeesHead"].Rows.Count-1)
               {
                   i++;
                   this.TxtID.Text = DS.Tables["FeesHead"].Rows[i]["ID"].ToString();
                   this.TxtFees.Text = DS.Tables["FeesHead"].Rows[i]["Fees"].ToString();
                   this.TxtFeesHead.Text = DS.Tables["FeesHead"].Rows[i]["FeesHead"].ToString();
                   this.TxtRemarks.Text = DS.Tables["FeesHead"].Rows[i]["Remarks"].ToString();

                   if (i == DS.Tables["FeesHead"].Rows.Count - 1)
                   {
                       MessageBox.Show("This is Last Record"); 
                   }<pre lang="c#">


}


}
}
I am Trying Class library for above procedure My code is here !! I need some corrections !! I feed difficulty to access my form text box here so I left blank this space !!

C#
public void move_prev(String Con_String, String Table)
       {
           {
               using (SqlConnection con = new SqlConnection(Con_String))
               {
                   SqlDataAdapter DA = new SqlDataAdapter("Select * from " + Table +"", con);

                   DataSet DS = new DataSet();
                   DA.Fill(DS, Table);

                   for (int j=0; j<= DS.Tables[Table].Columns.Count;j++ )
                   {
                       int i = 0;
                       String name_of_column = DS.Tables[Table].Columns[j].ColumnName;

                       if (i == DS.Tables[Table].Rows.Count - 1 || i != 0)
                       {
                           i--;

                              = DS.Tables[Table].Rows[i][name_of_column].ToString();


                           if (i == 0)
                           {
                               MessageBox.Show("This is First Record");
                           }

                       }
                   }
             }}
          }
       }
   }


Note :- My field names of the table as same as my Textbox names - with just Txt Prefix !! like Field name is ID than Textbox name is TxtId.text etc
Posted
Updated 4-May-15 19:21pm
v3
Comments
BillWoodruff 3-May-15 9:54am    
A key question is whether you need to use multiple Forms, or whether you will have a group of Controls (UserControls) that you will display one-at-a-time on one Form.

If you clarify that, I have some ideas I'll share with you; there are several ways you can implement that strategy.
Md Jamaluddin Saiyed 4-May-15 1:08am    
I want to use one public class in multiple forms. I want to make method for First-next-prev-Last buttons for my different forms! which can perform same action in different forms !!
BillWoodruff 4-May-15 1:58am    
Okay, this is a Windows Forms Application started with a "Main Form" ? And, you'll have a number of other, "secondary Forms," you wish to display, with first/last/next/previous buttons ?

You have worked out how you will fill each "secondary Form" with its appropriate data, and what types of interaction/communication you need to have between each secondary Form and the Main Form, or between secondary Forms ?

Have you ever used a Windows Forms Application stared with an Application Context rather than a "Main Form" ?
Md Jamaluddin Saiyed 4-May-15 3:03am    
Yes My forms Application started with Main form ! I have number of secondary forms and I wish to display First-Last-next-Previous buttons on each form ! I want to create user control library for this action
BillWoodruff 4-May-15 5:35am    
Reasons one would use secondary Forms would be:

1. you can animate them so they move across the screen and outside the screen.

2. you can fade them in-and-out by changing the Opacity.

3. you might to use them on multiple monitors.

Reasons one would use UserControls displayed on one Form (the Main Form, usually) would be:

1. they are "light-weight" objects compared to Forms in terms of memory use and, possibly, more efficient to render on computers with lower-capacity hardware.

2. you can still animate them so they scroll off the Form.

Please tell us why you want to use both secondary Forms and a UserControl, and what, exactly, do you mean by "user control library" : are you creating an Application, or some kind of Control that you could re-use in other Applications ?

What have you tried so far ?

Stop thinking about it like that, and try to consider it from an OOPs point of view.

What you are (or should be) looking at is a Control which contains your four buttons, and can work with data to return the row(s) to display.
One way to do that is to pass an SqlCommand object to the new control, which is "ready to rock" with it's query, parameters, and so forth, and provide a method which returns a DataTable containing the relevant rows. Add an event to the Control which signals "New Data Available" and your pages can handle it, call the method, and display the data. How it's displayed is up to the page - so the control doesn't need to know about any text boxes.

Have a look at a UserControl[^] class - it may make your code a lot simpler!
 
Share this answer
 
Comments
Md Jamaluddin Saiyed 3-May-15 3:13am    
Thanks for your reply sir . I am working on windows application software !! can you example me with code ?
OriginalGriff 3-May-15 3:30am    
Follow the link - there is a small sample at the end.
If you Google for "UserControl tutorial" you will find a heap of examples!
Perhaps I understood the question completly wrong - but :
Why don't you create a BaseForm with the Buttons on it and the needed logic to do the work. This BaseForm must be inherited from all your working-Forms. The Name of the Form allows the BaseForm to decide to which Destination-Form you want to change.

Greetings
Ralf
 
Share this answer
 
Comments
BillWoodruff 5-May-15 5:09am    
+5 That's a very good suggestion for the case where you want all secondary Forms to share some common functionality, but you want the freedom to make each secondary Form also have unique elements. In the case where all secondary Forms have identical Controls and Layout, this is not really required.

Where it gets tricky if if you want to hide an inherited Control on a Form: it can be done with a little hacking, but, in design-mode, that inherited Control will be "locked."
I'd like to try and help you get started on planning how to use multiple Forms (you could use the same strategy with UserControls).

Let's say you have these Forms and Classes defined:

1. Class DataManager : gets requests for Database access from the Controller, pulls the data, sends it to the Controller ...

2. Class Controller :

a. creates and manages all secondary Forms

b. makes requests for data from the DataManager, and injects data received into Controls on the secondary Forms.

c. receives notifications from the secondary Forms when navigation buttons are clicked, or other events occur you need to take action based on.

3. Secondary Forms: assuming they are all identical in terms of Controls and layout:

a. you define a basic template that the Controller will create multiple instances of.

b. navigation buttons, etc.

4. The Main Form: possibly handle:

a. log-in ?

b. request the Controller to establish a valid connection to the DB ?

c. provide facility for user to edit application preferences ?

d. enable the user to start/stop presentation of the secondary Forms in some sequence based on user actions

How the Secondary Forms notify the Controller:

C#
// inside the Secondary Form template

// define four public Action delegates:

public Action<int> GoBack;

public Action<int> GoNext;

public Action<int> GoStart;

public Action<int> GoEnd;

// note that each secondary Form will have an integer ID field

// define Event Handlers for the navigation Buttons that invoke these Action delegates

private void btnNext_MouseClick(object sender, MouseEventArgs e)
{
    GoNext(ID);
}

private void btnBack_MouseClick(object sender, MouseEventArgs e)
{
    GoBack(ID);
}

private void btnStart_MouseClick(object sender, MouseEventArgs e)
{
    GoNext(ID);
}

private void btnLast_MouseClick(object sender, MouseEventArgs e)
{
    GoBack(ID);
}

In the Controller you will inject executable code into the Action delegates as you create the secondary forms ... in this example the secondary Form template is named 'Slide:
C#
// in the Controller

public const int NumberOfSlides = 10;

private LIst<slide> Slides = new List<slide>();

private Slide currentSlide, nextSlide;

private int LastSlideID;

public void CreateSlides()
{
    LastSlideID = NumberOfSlides - 1;
    
    for (int i = 0; i < NumberOfSlides; i++)
    {
        var slide = new Slide();
    
        slide.Owner = MainForm;  // assumes you have a reference to the main Form in the Controller
    
        Slides.Add(slide);
    
        slide.GoBack = HandleBack;
        slide.GoNext = HandleNext;
        slide.GoStart = HandleStart;
        slide.GoEnd = HandleEnd;
    }
    
    SuspendLayout();
    
    foreach (var slide in Slides)
    {
        slide.Show();
    }
    
    currentSlide = Slides[0];
    nextSlide = Slides[1];
    currentSlide.BringToFront();
    
    ResumeLayout();
    
    currentSlide.Activate();
    currentSlide.Focus();
}

// an example of how code in the Controller handles the 'Next notification

private void HandleNext(int slideId)
{
    nextSlide = (currentSlide.ID == LastSlideID) ? Slides[0] : Slides[currentSlide.ID + 1];
    Transition();
}

// the code in the Transition() method will handle doing whatever you want to do to present the next slide
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900