按钮只能在ListView中有记录之后再添加到那些记录对应的行的末尾。

设计窗体,中间的是ListView控件

获取数据并显示到界面上之后(除了接受和拒绝之外的所有字段都获取到数据并显示在ListView中之后),点击getOffer按钮,在ListView中将出现接受offer和拒绝offer的Button,如下所示。

这个界面的代码如下所示。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Security.Cryptography;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using static OccupationSystem.ServerProtocol;

using static System.Windows.Forms.VisualStyles.VisualStyleElement;

using Button = System.Windows.Forms.Button;

namespace OccupationSystem.DeliverySituations

{

public partial class BeEmployedList : Form

{

LoginRS m_info;

Client m_c;

DeliverySituation m_pre;

Dictionary m_ButtonAcceptToRowIndex = new Dictionary();

Dictionary m_ButtonRefuseToRowIndex = new Dictionary();

Dictionary m_ButtonRowIndexToAccept = new Dictionary();

Dictionary m_ButtonRowIndexToRefuse = new Dictionary();

//1.向服务端发起数据请求

public BeEmployedList(DeliverySituation pre, LoginRS info, Client c)

{

InitializeComponent();

m_info=info;

m_pre=pre;

m_c=c;

SendBeEmployedListRq();

}

//发送数据请求的函数

private void SendBeEmployedListRq()

{

ApplyInfo rq = new ApplyInfo();

rq.type=TYPE.beEmployedList_rq;

rq.uid1=m_info.uid;

m_c.SendData(rq);

}

//2.将服务端发回来的数据显示在界面上

public void dealBeEmployedMemberRs(ApplyInfo rs)

{

string[] items = new string[6];

items[0]=rs.uid2.ToString();

items[1]=rs.uname2;

items[2]=rs.pid.ToString();

items[3]=rs.pname;

items[4]="";

items[5]="";

while (!this.IsHandleCreated) { };

this.BeginInvoke(new Action(() =>

{

ListViewItem item = new ListViewItem(items);

listView.Items.Add(item);

}));

}

//4.1接受offer按钮点击后的处理函数

private void btn_clickAccept(object sender, EventArgs e)

{

var p = (Button)sender;

p.Text="已接受";

int row = m_ButtonAcceptToRowIndex[p];

m_ButtonRowIndexToRefuse[row].Enabled=false;

ApplyInfo rq = new ApplyInfo();

rq.type=TYPE.accpetOffer_rq;

rq.uid1=m_info.uid;

rq.uid2=int.Parse(listView.Items[row].SubItems[0].Text);

rq.pid=int.Parse(listView.Items[row].SubItems[2].Text);

m_c.SendData(rq);

}

//4.2拒绝offer按钮点击后的处理函数

private void btn_clickRefuse(object sender, EventArgs e)

{

var p = (Button)sender;

p.Text="已拒绝";

int row = m_ButtonRefuseToRowIndex[p];

m_ButtonRowIndexToAccept[row].Enabled=false;

ApplyInfo rq = new ApplyInfo();

rq.type=TYPE.refuseOffer_rq;

rq.uid1=m_info.uid;

rq.uid2=int.Parse(listView.Items[row].SubItems[0].Text);

rq.pid=int.Parse(listView.Items[row].SubItems[2].Text);

m_c.SendData(rq);

}

//3.点击getOffer按钮,出现Button 接受offer和拒绝offer按钮

private void button_getOffer_Click(object sender, EventArgs e)

{

for (int i = 0; i

{

Button btn = new Button();

btn.Size=new Size(listView.Items[i].SubItems[4].Bounds.Width-100, listView.Items[i].SubItems[4].Bounds.Height);

btn.Location=new Point(listView.Items[i].SubItems[4].Bounds.Left, listView.Items[i].SubItems[4].Bounds.Top);

btn.Text="接受offer";

btn.Click+= new System.EventHandler(btn_clickAccept);

btn.Show();

this.listView.Controls.Add(btn);

m_ButtonAcceptToRowIndex[btn]=i;

m_ButtonRowIndexToAccept[i]=btn;

Button btn2 = new Button();

btn2.Size=new Size(listView.Items[i].SubItems[5].Bounds.Width-100, listView.Items[i].SubItems[5].Bounds.Height);

btn2.Location=new Point(listView.Items[i].SubItems[5].Bounds.Left, listView.Items[i].SubItems[5].Bounds.Top);

btn2.Text="拒绝offer";

btn2.Click+= new System.EventHandler(btn_clickRefuse);

btn2.Show();

this.listView.Controls.Add(btn2);

m_ButtonRefuseToRowIndex[btn2]=i;

m_ButtonRowIndexToRefuse[i]=btn2;

}

button_getOffer.Enabled=false;

}

//点击返回按钮回到上一界面

private void button_back_Click(object sender, EventArgs e)

{

this.Hide();

m_pre.Show();

this.Dispose();

}

}

}

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。