想要使用相同的按钮为我的所有表单的第一个 - 下一个 - 上一个 - 上一个按钮创建不同类的功能 [英] Want to make function in diffrent class for my first-Next-Previous-Last buttons for my All forms with same buttons

查看:53
本文介绍了想要使用相同的按钮为我的所有表单的第一个 - 下一个 - 上一个 - 上一个按钮创建不同类的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友



我想在我的所有表格的第一个下一个 - 上一个 - 最后一个按钮的不同类(我的表格外)中创建功能相同的按钮。



我该怎么办?没有想法访问其他类的文本框。





我的代码在这里用于下一个按钮!



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 !!

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 !!

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");
                           }

                       }
                   }
             }}
          }
       }
   }





注意: - 我的表字段名称与我的文本框名称相同 - 只有Txt前缀!!比如字段名称是ID而不是文本框名称是TxtId.text等



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

推荐答案

不要那样考虑它,并尝试从OOP的角度考虑它。



您正在(或应该)看到的是一个包含四个按钮的控件,可以处理数据以返回要显示的行。 br />
一种方法是将SqlCommand对象传递给新控件,该控件使用它的查询,参数等准备摇摆,并提供一个返回包含该数据的DataTable的方法相关行。向Control添加一个事件,其中显示New Data Available,您的页面可以处理它,调用方法并显示数据。它的显示方式取决于页面 - 所以控件不需要知道任何文本框。



查看 UserControl [ ^ ]类 - 它可能会使你的代码更简单了!
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!


也许我理解完全错误的问题 - 但是:

为什么不用它上面的Buttons创建一个BaseForm做这项工作需要逻辑。必须从所有工作表单继承此BaseForm。表格名称允许BaseForm决定您想要更改哪个目的地表格。



问候语

Ralf
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


我想尝试帮助您开始规划如何使用多个表单(您可以使用与UserControls相同的策略)。



假设您已经定义了这些表单和类:



1.类DataManager:从Controller获取数据库访问请求,提取数据,将数据发送到控制器......



2.班长:



a。创建和管理所有二级表格



b。从DataManager请求数据,并将接收到的数据注入到辅助表单的控件中。



c。单击导航按钮时接收来自辅助表单的通知,或者发生其他事件需要您采取行动。



3.次要表格:假设它们全部相同在控制和布局方面:



a。您定义了一个基本模板,Controller将创建多个实例。



b。导航按钮等



4.主表格:可能处理:



a。登录?



b。请求控制器建立与DB的有效连接?



c。为用户提供编辑应用程序首选项的工具?



d。使用户能够根据用户操作以某种顺序开始/停止二级表格的演示



二级表格如何通知管理员:



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:

// 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);
}



在Controller中,您将在创建辅助表单时将可执行代码注入Action代理...在此示例中,辅助表单模板已命名'幻灯片:


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:

// 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


这篇关于想要使用相同的按钮为我的所有表单的第一个 - 下一个 - 上一个 - 上一个按钮创建不同类的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆