本文介绍了如何创建可以将值编辑为其他表单的设置表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建这样的主要表单
[]
设置表格
[]
显示表格
[]
当我点击主窗体(form1)中的show按钮时。显示表单,我希望它在插入值时显示值,然后从设置窗体中单击确定按钮。我不能这样做
请查看我的代码
主要表格:
I create main form like this
http://image.ohozaa.com/view2/xTGvIUCRxnvUo36h[^]
Setting form
http://image.ohozaa.com/view2/xTGwbCU6vmIpqRs3[^]
Show form
http://image.ohozaa.com/view2/xTGwrzWNgMdZ39P4[^]
When I click show button in the main form(form1). The show form,I want it show the value when I insert the value and click ok button from the setting form.I can't do it
Please look at my code
main form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void settingButton_Click(object sender, EventArgs e)
{
new Setting().ShowDialog();
}
private void showButton_Click(object sender, EventArgs e)
{
new Show().ShowDialog();
}
}
设置表格:
Setting form:
public partial class Setting : Form
{
ListViewItem[] listViewAddGenderAge_ListViewItem = new ListViewItem[5];
int checkAddClick;
String[] gender = new String[5];
String[] age = new String[5];
public Setting()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
checkAddClick = 0;
for (int i = checkAddClick; i < checkAddClick+1; i++)
{
gender[i] = genderText.Text;
age[i] = ageText.Text;
listViewAddGenderAge_ListViewItem[i] = new ListViewItem(gender[i]);
listViewAddGenderAge_ListViewItem[i].SubItems.Add(age[i]);
listViewAdd.Items.Add(listViewAddGenderAge_ListViewItem[i]);
}
checkAddClick += 1;
}
private void Setting_Load(object sender, EventArgs e)
{
}
Show showForm;
private void OKButton_Click(object sender, EventArgs e)
{
showForm = new Show(this,gender,age);
this.Close();
}
}
显示表格:
Show form:
public partial class Show : Form
{
Setting settingForm;
ListViewItem[] listViewShow_ListViewItem = new ListViewItem[5];
public Show()
{
InitializeComponent();
}
public Show(Form callingform,String[] age,String[] gender)
{
age = new String[5];
gender = new String[5];
settingForm = callingform as Setting;
for (int i = 0; i < 5; i++)
{
this.listViewShow_ListViewItem[i]=new ListViewItem(i.ToString());
this.listViewShow_ListViewItem[i].SubItems.Add(age[i]);
this.listViewShow_ListViewItem[i].SubItems.Add(gender[i]);
this.listViewShow.Items.Add(this.listViewShow_ListViewItem[i]);
}
}
}
谢谢你
thank you
推荐答案
// MainForm
using System;
using System.Windows.Forms;
namespace SynchronizedLists
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private SettingForm SettingForm = new SettingForm();
private ShowForm ShowForm = new ShowForm();
private ListView PointerToShowFormListView;
private void MainForm_Load(object sender, EventArgs e)
{
// inject a method into the public Action variable in the Setting Form
SettingForm.UpdateShowFormListView = UpdateShowFormListView;
// get a reference to the ListView on the Show Form
PointerToShowFormListView = ShowForm.ShowFormListView;
}
// this method will be invoked by code in the Setting Form
private void UpdateShowFormListView(ListViewItem newListViewItem)
{
// copy the new ListItem on the Setting Form
// and add it to the ListView on the Show Form
PointerToShowFormListView.Items.Add(newListViewItem.Clone() as ListViewItem);
}
private void btnShowSettingForm_Click(object sender, EventArgs e)
{
if (! SettingForm.Visible) SettingForm.Show();
}
private void btnShowShowForm_Click(object sender, EventArgs e)
{
if (! ShowForm.Visible) ShowForm.Show();
}
}
}
// SettingForm
using System;
using System.Windows.Forms;
namespace SynchronizedLists
{
public partial class SettingForm : Form
{
public SettingForm()
{
InitializeComponent();
}
// see notes: the Main Form will inject a pointer
// to an executable method defined in the Main Form
// into this variable
public Action<listviewitem> UpdateShowFormListView;
private void btnOkay_Click(object sender, EventArgs e)
{
// create the new item
var newItem = new ListViewItem(tBxGender.Text);
newItem.SubItems.Add(tBxAge.Text);
// add the new item to the ListView in this Form
// which is named 'settingListView
settingListView.Items.Add(newItem);
// invoke the method defined in MainForm
// that will update the ListView in the 'ShowForm
UpdateShowFormListView(newItem);
}
// prevent the Form from being closed and losing the data
private void SettingForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
}
}
// ShowForm
using System;
using System.Windows.Forms;
namespace SynchronizedLists
{
public partial class ShowForm : Form
{
public ShowForm()
{
InitializeComponent();
// get the reference to the ListView on this Form
ShowFormListView = this.listView1;
}
// make this Form's ListView available to the Main Form
public ListView ShowFormListView { private set; get; }
// prevent the Form from being closed and losing the data
private void ShowForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
}
}
这篇关于如何创建可以将值编辑为其他表单的设置表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!