C# 阅读运行时添加的文本框的文本
我有一个应用程序,用户可以根据需要添加任意数量的选项卡页面。这些选项卡页具有相同的控件(文本框和标签)。 每个选项卡页上的控件的名称如下:(在选项卡页1上,控件的名称为:txtServer1、txtPort1、txtser1、txtDbName1。如果用户单击“添加另一个连接按钮”,应用程序将创建第二个选项卡页,控件的名称为:txtServer2、txtPort2、txtser2、txtDbName2)等等 例如,如果用户要设置多个选项卡页: 用户单击“添加另一个连接”,创建了另一个具有相同控件的选项卡页,用户用适当的数据填充该选项卡页: 同样的事情发生在这里: 以下是使用控件动态添加tabpage的代码:C# 阅读运行时添加的文本框的文本,c#,.net,C#,.net,我有一个应用程序,用户可以根据需要添加任意数量的选项卡页面。这些选项卡页具有相同的控件(文本框和标签)。 每个选项卡页上的控件的名称如下:(在选项卡页1上,控件的名称为:txtServer1、txtPort1、txtser1、txtDbName1。如果用户单击“添加另一个连接按钮”,应用程序将创建第二个选项卡页,控件的名称为:txtServer2、txtPort2、txtser2、txtDbName2)等等 例如,如果用户要设置多个选项卡页: 用户单击“添加另一个连接”,创建了另一个具有相同控
//when 'add another connection' button is clicked
private void btnAddConnection_Click(object sender, EventArgs e)
{
string tabTitle = "Connection " + (tabControl1.TabCount + 1).ToString();
TabPage tabPage = new TabPage(tabTitle);
tabControl1.TabPages.Add(tabPage);
}
//when another tabpage has been added to tabcontrol
private void tabControl1_ControlAdded(object sender, ControlEventArgs e)
{
//control instances
TextBox txtServer = new TextBox();
TextBox txtPort = new TextBox();
TextBox txtUser = new TextBox();
TextBox txtDbName = new TextBox();
Label lblServer = new Label();
Label lblPort = new Label();
Label lblUser = new Label();
Label lblDbName = new Label();
tabControl1.SelectedTab = tabControl1.TabPages[tabControl1.TabCount - 1]; //select the newly addded tabpage
tabControl1.SelectedTab.BackColor = tabPage1.BackColor; //tabpage background color
//lblServer Properties
lblServer.Location = lblServer1.Location;
lblServer.Text = lblServer1.Text;
lblServer.ForeColor = lblServer1.ForeColor;
lblServer.Name = "lblServer" + tabControl1.TabCount.ToString();
//lblPort Properties
lblPort.Location = lblPort1.Location;
lblPort.Text = lblPort1.Text;
lblPort.ForeColor = lblPort1.ForeColor;
lblPort.Name = "lblPort" + tabControl1.TabCount.ToString();
//lblUser Properties
lblUser.Location = lblUser1.Location;
lblUser.Text = lblUser1.Text;
lblUser.ForeColor = lblUser1.ForeColor;
lblUser.Name = "lblUser" + tabControl1.TabCount.ToString();
//lblDbName Properties
lblDbName.Location = lblDbName1.Location;
lblDbName.Text = lblDbName1.Text;
lblDbName.ForeColor = lblDbName1.ForeColor;
lblDbName.Name = "lblDbName" + tabControl1.TabCount.ToString();
//txtserver properties
txtServer.Location = txtServer1.Location;
txtServer.Width = txtServer1.Width;
txtServer.Height = txtServer1.Height;
txtServer.Font = txtServer1.Font;
txtServer.Name = "txtServer" + tabControl1.TabCount.ToString();
//txtport properties
txtPort.Location = txtPort1.Location;
txtPort.Width = txtPort1.Width;
txtPort.Height = txtPort1.Height;
txtPort.Font = txtPort1.Font;
txtPort.Name = "txtPort" + tabControl1.TabCount.ToString();
//txtuser properties
txtUser.Location = txtUser1.Location;
txtUser.Width = txtUser1.Width;
txtUser.Height = txtUser1.Height;
txtUser.Font = txtUser1.Font;
txtUser.Name = "txtUser" + tabControl1.TabCount.ToString();
//txtdbname properties
txtDbName.Location = txtDbName1.Location;
txtDbName.Width = txtDbName1.Width;
txtDbName.Height = txtDbName1.Height;
txtDbName.Font = txtDbName1.Font;
txtDbName.Name = "txtUser" + tabControl1.TabCount.ToString();
//add controls to tabpage
tabControl1.SelectedTab.Controls.Add(lblServer);
tabControl1.SelectedTab.Controls.Add(lblPort);
tabControl1.SelectedTab.Controls.Add(lblUser);
tabControl1.SelectedTab.Controls.Add(lblDbName);
tabControl1.SelectedTab.Controls.Add(txtServer);
tabControl1.SelectedTab.Controls.Add(txtPort);
tabControl1.SelectedTab.Controls.Add(txtUser);
tabControl1.SelectedTab.Controls.Add(txtDbName);
}
当用户单击save按钮时,我希望应用程序读取文本框中的每个文本(url文本除外),以便将其保存到配置文件中
我能想到的就是这个
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string connection;
for (int i = 1; i <= tabControl1.TabCount; i++ )
{
connection = "server=" + txtServer[i].Text + ";port=" txtPort[i].Text + ";user=" + txtUser[i].Text + ";dbname=" + txtDbName[i];
//save to config file code goes here...
}
}
private void saveToolStripMenuItem\u单击(对象发送方,事件参数e)
{
串接;
对于(int i=1;itxtServer[i]。Text这是真的,但您尚未定义数组
你可以这样做
//global
List<TextBox> txtServerList= new List<TextBox>();
private void tabControl1_ControlAdded(object sender, ControlEventArgs e)
{
txtServer.Location = txtServer1.Location;
txtServer.Width = txtServer1.Width;
txtServer.Height = txtServer1.Height;
txtServer.Font = txtServer.Font;
txtServer.Name = "txtServer" + tabControl1.TabCount.ToString();
txtServerList.Add(txtServer)
.
.
.
}
//全局
List txtServerList=新列表();
私有无效选项卡Control1\u ControlAdded(对象发送方,ControlEventArgs e)
{
txtServer.Location=txtServer1.Location;
txtServer.Width=txtServer1.Width;
txtServer.Height=txtServer1.Height;
Font=txtServer.Font;
txtServer.Name=“txtServer”+tabControl1.TabCount.ToString();
txtServerList.Add(txtServer)
.
.
.
}
最后
for (int i = 1; i <= tabControl1.TabCount; i++ )
{
connection=txtServerList.get(i).Text + ......
}
for(int i=1;i您只需在控件上循环并找到匹配的名称,如下所示:
foreach(var page in tabControl1.TabPages){
var server = ProcessControls(page.Controls, "txtServer");
//... continue with the others
}
private TextBox ProcessControls(Control ctrlContainer, string name)
{
foreach (Control ctrl in ctrlContainer.Controls)
{
if (ctrl.GetType() == typeof(TextBox))
{
if(ctrl.Name.StartsWith(name))
return (TextBox)ctrl;
}
}
}
这是您可以尝试的最简单的方法,只需使用下面的函数从tabpage中的控件获取文本
public string getValue(string controlName, TabPage selectedTab)
{
if (selectedTab.Controls.ContainsKey(controlName)){
TextBox selectedtb = (TextBox)selectedTab.Controls[controlName];
return selectedtb.Text;
}
else
return null;
}
在您的保存代码中使用它,如下所示
string connection;
int i = 1;
TabControl.TabPageCollection pages = tabControl1.TabPages;
foreach (TabPage page in pages)
{
connection = "server=" + getValue("txtServer"+i,page) + ";port=" +getValue("txtPort"+i,page) + ";user=" + getValue("txtUser"+i,page) + ";dbname=" + getValue("txtDbName"+i,page);
i++;
//save to config file code goes here...
}
您可以使用ArrayList存储和访问所有动态添加的控件。为每个动态添加的控件设置名称。它可以基于ArrayList的大小
我将不显示c#的确切systax
首先
声明ArrayList或List(类型安全)
获取列表中的项目有几种方法。一种是使用索引
void someMethod(some parameters) {
Button button = buttons.ElementAt(index);
}
@Pio链接已断开。return ctrl
引发错误:“无法将类型“System.Windows.Forms.Control”隐式转换为“System.Windows.Forms.TextBox”。存在显式转换”现在,页.controls
有一个错误。“object”不包含“controls”的定义,并且找不到接受“object”类型的第一个参数的扩展方法“controls”getValue
总是返回null,因为它在选项卡页中找不到控件,这意味着您提供了错误的控件名称,可以吗显示您的错误跟踪。
void your_event (some parameters) {
Button temp = new Button("Hello");
temp.Name="button_name";
YourMainPanel.Controls.add(temp);
//after creating your button add it to the parent container. of any control where you want this button to be added.
//you can also set the coordinates of the button if you like
//after creating the button we need to add it to our List so that we can
//access it later, since the value of the 'temp' will change after this
//event was invoked in the future.
buttons.add(temp);
}
void someMethod(some parameters) {
Button button = buttons.ElementAt(index);
}