I have a form where I can add tabs into a tabcontrol by pressing a button on the form. There are 4 textboxes and then the name of the tab. I have added using system.io;
Where would I even start to create a save for all of the tabs? I have the save button created, I just don't know where to start. I would guess I would need a loop of some kind.
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public string status = "no";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = txtName.Text;
//validate information
try { }
catch { }
//create new tab
string title = name;
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
//Add Labels
Label lb = new Label();
lb.Text = "Denomination:";
lb.Location = new System.Drawing.Point(150, 75);
lb.Name = "lbl";
lb.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb);
Label lb2 = new Label();
lb2.Text = "Year:";
lb2.Location = new System.Drawing.Point(150, 120);
lb2.Name = "lbl2";
lb2.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb2);
Label lb3 = new Label();
lb3.Text = "Grade:";
lb3.Location = new System.Drawing.Point(150, 165);
lb3.Name = "lbl3";
lb3.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb3);
Label lb4 = new Label();
lb4.Text = "Mint Mark:";
lb4.Location = new System.Drawing.Point(150, 210);
lb4.Name = "lbl4";
lb4.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb4);
//Add text boxes
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(250, 75);
tb.Name = "txt";
tb.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb);
TextBox tb1 = new TextBox();
tb1.Location = new System.Drawing.Point(250, 120);
tb1.Name = "txt1";
tb1.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb1);
TextBox tb2 = new TextBox();
tb2.Location = new System.Drawing.Point(250, 165);
tb2.Name = "txt2";
tb2.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb2);
TextBox tb3 = new TextBox();
tb3.Location = new System.Drawing.Point(250, 210);
tb3.Name = "txt3";
tb3.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb3);
//put data inside of textboxes
tb.Text = txtCoin.Text;
tb1.Text = txtYear.Text;
tb2.Text = txtGrade.Text;
tb3.Text = txtMint.Text;
// Add delete button
Button bn = new Button();
bn.Location = new System.Drawing.Point(560, 350);
bn.Name = "btnDelete";
bn.Text = "Delete";
bn.Size = new System.Drawing.Size(100, 50);
bn.Click += MyClick;
myTabPage.Controls.Add(bn);
}
private void MyClick(object sender, EventArgs e)
{
Form2 myform = new Form2();
myform.ShowDialog();
if (status == "yes")
{ tabControl1.TabPages.Remove(tabControl1.SelectedTab); }
status = "no";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
int counter;
int ccounter;
string outLine;
string pathFileName = Path.Combine(Application.StartupPath, "coins.dat");
StreamWriter writeIt = new StreamWriter(pathFileName);
for (counter = 0; ((tabControl1.TabCount) -1) != 0 ;)
{
}
writeIt.Close();
}
}
}
Use a foreach loop instead of your for (counter = 0; ((tabControl1.TabCount) -1) != 0 ;) loop:
foreach (TabPage tabPage in tabControl.TabPages)
Your for loop is a infinite loop, do it like that:
for (counter = 0; counter < tabControl1.TabCount; counter++)
{
// Save the tab
}
Related
I´m stuck on a problem using controls.find and I can´t find the error.
I´m creating a loginform using different panels.
Panels are created as follows:
public Panel CreatePanel()
{
Panel login = new Panel();
TextBox Login_UsernameTB = new TextBox();
TextBox Login_PasswordTB = new TextBox();
Label label1 = new Label();
Label label2 = new Label();
Label loginstatus = new Label();
Button Login_Loginbtn = new Button();
Button Login_Registerbtn = new Button();
PictureBox Login_Logo = new PictureBox();
PictureBox Login_ECard = new PictureBox();
//creatingPanel
login.Location = new Point(0, 0);
login.Name = "Login";
login.Size = new Size(1000, 400);
//locations
Login_ECard.Location = new Point(500, 250);
Login_ECard.SizeMode = PictureBoxSizeMode.StretchImage;
Login_ECard.Image = PatientSimulator.Properties.Resources.Ecard;
Login_ECard.Name = "Login_Ecard";
Login_Logo.Location = new Point(148, 12);
Login_Logo.SizeMode = PictureBoxSizeMode.StretchImage;
Login_Logo.Image = PatientSimulator.Properties.Resources.Logo;
Login_Logo.Name = "Login_Logo";
label1.Location = new Point(31, 200);
label1.Size = new Size(58, 13);
label1.Text = "Username:";
label1.Name = "label1";
label2.Location = new Point(31, 244);
label2.Size = new Size(56, 13);
label2.Text = "Password:";
label2.Name = "label2";
Login_UsernameTB.Location = new Point(148, 197);
Login_UsernameTB.Size = new Size(359, 20);
Login_UsernameTB.Text = "Testfirma1";
Login_UsernameTB.Name = "Login_UsernameTB";
Login_PasswordTB.Location = new Point(148, 241);
Login_PasswordTB.Size = new Size(359, 20);
Login_PasswordTB.Text = "Hallo1234#";
Login_PasswordTB.PasswordChar = '*';
Login_PasswordTB.Name = "Login_PasswordTB";
Login_Loginbtn.Location = new Point(432, 334);
Login_Loginbtn.Size = new Size(75, 23);
Login_Loginbtn.Text = "Login";
Login_Loginbtn.Click += Login_Loginbtn_Click;
loginstatus.Location = new Point(323, 360);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
Login_Registerbtn.Location = new Point(323, 334);
Login_Registerbtn.Size = new Size(75, 23);
Login_Registerbtn.Text = "Register";
Login_Registerbtn.Click += Login_Registerbtn_Click;
login.Controls.Add(Login_ECard);
login.Controls.Add(Login_Logo);
login.Controls.Add(label1);
login.Controls.Add(label2);
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(Login_Loginbtn);
login.Controls.Add(Login_Registerbtn);
login.Controls.Add(loginstatus);
return login;
}
In Form_load:
Panel login = CreatePanel();
login.Visible = true;
Controls.Add(login);
When I start the application, everything is displayed. if I type some text into systemstatus.text isn't gets displayed.
When doing a check if the entered password & username are correct, the strange stuff happens.
private void Login_Loginbtn_Click(object sender, EventArgs e)
{
Patient.DBAccess db = new Patient.DBAccess();
Sha256 sha = new Sha256();
string username = login.Controls.Find("Login_UsernameTB", true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB", true)[0].Text;
Patient.DatabaseRequestInterface.UserInterface user = new Patient.DatabaseRequestInterface.UserInterface();
user.Username = username;
if (db.IsUserExisting(user)){
user = db.ReadUserPassword(user);
string salt = Encoding.ASCII.GetString(user.PasswordSalt);
byte[] pw = sha.GetSha256(userpw, salt);
if (pw.SequenceEqual(user.PasswordHash))
{
//Programm starten
//token generieren
Patient.DatabaseRequestInterface.UserInterface useri = db.ReadFullUser(user);
UserAuthentificationToken = new UserInfo(useri.Username, useri.Uuid, DateTime.Now);
LoginSuccessEvent?.Invoke(this, new EventArgs());
login.Controls.Find("loginstatus", true)[0].Text = "Login successfull";
this.Close();
}
else
{
MessageBox.Show("Login failed, Username and/or password incorrect");
}
}
else
{
MessageBox.Show("Falscher User oder Passwort");
//Controls.Find("Login_StatusL", true)[0].Text = "Login failed, Username and/or password incorrect!";
}
}
The first 2 times I use controls.find it works, I get the strings entered by the users at the corresponding TextBoxes. When I try to change loginstatus, I get a System.IndexOutOfRangeException. My interpretation of the exception is, that there was no loginstatus found. And I don´t understand why. (the other 2 elements can´t be found within the if as well)
Can anyone help?
the controls.find("string",true)[0].Text = "XXX" works, I use it a lot at another part of the application
Any ideas?
Thanks in advance,
David
You miss the Name property in your code and Controls.Find strictly work on Name
Login_UsernameTB.Name = "Login_UsernameTB";
So your Code should be something like this:
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900, 900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50, 50);
Login_UsernameTB.Text = "Username here";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150, 50);
Login_PasswordTB.Text = "password here";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150, 150);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
Also, the login should be class level but not defined inside the Form_Load
like this :
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
.....
I have tested it and works fine.
My test code is
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SackOverflow
{
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900, 900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50, 50);
Login_UsernameTB.Text = "Username";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150, 50);
Login_PasswordTB.Text = "password";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150, 150);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
private void button1_Click(object sender, EventArgs e)
{
if (true)
{
string username = login.Controls.Find("Login_UsernameTB", true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB", true)[0].Text;
//UserInterface user = //some database stuff to get the password saved by the user
if ("123" == userpw)
{
login.Controls.Find("loginstatus", true)[0].Text = "Logging in";
Login();
}
else
{
login.Controls.Find("loginstatus", true)[0].Text = "Error";
}
}
}
private void Login()
{
MessageBox.Show("Login");
}
}
}
Result is:
I receive the message box MessageBox.Show("Login"); shown and Loginstatus is shown too.
i'm new to c# programming, I had coded in c#(winforms) to get the output as: if an Item in the list box is clicked then the items should be displayed in the text box ,i had coded but its little bit hectic to implement to go further.
public partial class Form1 : Form
{
TextBox[] tb = new TextBox[5];
TextBox[] t = new TextBox[5];
TextBox[] t1 = new TextBox[5];
int[] tblist = new int[5];
public Form1()
{
InitializeComponent();
tb[0] = new TextBox();
tb[1] = new TextBox();
tb[2] = new TextBox();
tb[3] = new TextBox();
tb[4] = new TextBox();
t[0] = new TextBox();
t[1] = new TextBox();
t[2] = new TextBox();
t[3] = new TextBox();
t[4] = new TextBox();
t1[0] = new TextBox();
t1[1] = new TextBox();
t1[2] = new TextBox();
t1[3] = new TextBox();
t1[4] = new TextBox();
} //how can I simplify this by not assigning new to every textbox that i had created
// this button click is used to save items in the textbox in the listbox selected item
here how can we minimize the code : listbox selected index differs but the functions remains the same..
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == 0)
{
tb[0].Text = textBox1.Text;
tb[1].Text = textBox2.Text;
tb[2].Text = textBox3.Text;
tb[3].Text = textBox4.Text;
tb[4].Text = textBox5.Text;
}
if (listBox1.SelectedIndex == 1)
{
t[0].Text = textBox1.Text;
t[1].Text = textBox2.Text;
t[2].Text = textBox3.Text;
t[3].Text = textBox4.Text;
t[4].Text = textBox5.Text;
}
if (listBox1.SelectedIndex == 2)
{
t1[0].Text = textBox1.Text;
t1[1].Text = textBox2.Text;
t1[2].Text = textBox3.Text;
t1[3].Text = textBox4.Text;
t1[4].Text = textBox5.Text;
}
}
//here an item is clicked in the list box., so then items in the text box can be store in the listbox selected index
private void listBox1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == 0)
{
textBox1.Text = tb[0].Text;
textBox2.Text = tb[1].Text;
textBox3.Text = tb[2].Text;
textBox4.Text = tb[3].Text;
textBox5.Text = tb[4].Text;
}
if (listBox1.SelectedIndex == 1)
{ textBox1.Text = t[0].Text;
textBox2.Text = t[1].Text;
textBox3.Text = t[2].Text;
textBox4.Text = t[3].Text;
textBox5.Text = t[4].Text;
}
if (listBox1.SelectedIndex == 2)
{
textBox1.Text = t1[0].Text;
textBox2.Text = t1[1].Text;
textBox3.Text = t1[2].Text;
textBox4.Text = t1[3].Text;
textBox5.Text = t1[4].Text;
}
`
You may use a for loop for your arrays.
for(var i = 0; i < tb.Length; i++)
{
tb[i] = new TextBox();
t[i] = new TextBox();
t1[i] = new TextBox();
}
This is a snippet from code that I use.
Load your values into DataTables
and add a tableLayoutPanel to the form where you want the textboxes to go.
Call SetTextboxes function with datatable (or you can send your list here, just change the parameters and loop a little.
This will dynamically add textboxes to your form very quickly.
class SurroundingClass
{
private void SetTextboxes(datatable DT)
{
//Clear the previous textboxes
pnlLayoutExpenses.Controls.clear();
//loop through table and create new textboxes
foreach (DataRow row in DT.Rows)
formAddTextbox(row("dataTableColumnWhichHoldsTextboxText"));
}
private void formAddTextbox(string fieldname)
{
Integer elementCount = 0;
TextBox txtYourField = new TextBox();
txtYourField.Width = 100;
txtYourField.Height = 20;
//txtYourField.ReadOnly = true;
txtYourField.Text = fieldname;
txtYourField.tag = elementCount;
// Use tableLayoutPanel
pnlLayoutExpenses.SetCellPosition(txtType, new TableLayoutPanelCellPosition(0, elementCount));
pnlLayoutExpenses.Controls.Add(txtType);
}
}
I'm trying to build tables dynamically with a text box and a button in each table. The tables are added successfully but when I pressed on the button it made a post back to the page and the tables were gone. So i build each table inside an updatePanel but when a button was clicked twice, a postback was occurred again.How can I prevent the potback? and then, how do I get the value inside the text boxes? TNX very much!
private void addTable(List<fileSaving> fs)
{
foreach (fileSaving f in fs){
UpdatePanel up = new UpdatePanel();
up.ID = "UpdatePanel-"+f.FileName;
up.UpdateMode = UpdatePanelUpdateMode.Conditional;
Table T = new Table();
T.CssClass = "filesTBL";
TableRow hTR = new TableRow();
TableCell td1 = new TableCell();
Image fileImg = new Image();
fileImg.ImageUrl = "images/"+f.FileExtension+".png";
td1.Controls.Add(fileImg);
hTR.Cells.Add(td1);
TableCell td2 = new TableCell();
td2.Text = f.ExpName;
hTR.Cells.Add(td2);
TableCell td3 = new TableCell();
Image expImg = new Image();
expImg.ImageUrl = "images/magnet.png";
td3.Controls.Add(expImg);
hTR.Cells.Add(td3);
T.Rows.Add(hTR);
TableRow mTR = new TableRow();
TableCell td4 = new TableCell();
td4.Text = "";
mTR.Cells.Add(td4);
TableCell td5 = new TableCell();
td5.Text = f.TeamID.ToString();
mTR.Cells.Add(td5);
TableCell td6 = new TableCell();
Image teamImg = new Image();
teamImg.ImageUrl = "images/team3.png";
td6.Controls.Add(teamImg);
mTR.Cells.Add(td6);
T.Rows.Add(mTR);
TableRow lTR = new TableRow();
TableCell td7 = new TableCell();
HyperLink downloadLink = new HyperLink();
downloadLink.Attributes.Add("href", "http://proj.ruppin.ac.il/igroup39/test2/tar5/tar5.zip");
downloadLink.ImageUrl = "images/download2.png";
downloadLink.ToolTip = "לחץ להורדה";
td7.Controls.Add(downloadLink);
lTR.Cells.Add(td7);
TableCell td8 = new TableCell();
if (f.ReportGrade != -1)
{
td8.Text = f.ReportGrade.ToString();
}
else
{
TextBox tb = new TextBox();
tb.ID = f.FileName + "-tb";
gFN = tb.ID;
tb.Width = 40;
td8.Controls.Add(tb);
Button btn = new Button();
btn.ID = f.FileName + "-btn";
btn.Text = "הזן ציון";
btn.Click += new EventHandler(btn_Click);
td8.Controls.Add(btn);
}
lTR.Cells.Add(td8);
TableCell td9 = new TableCell();
Image gradeImg = new Image();
gradeImg.ImageUrl = "images/grade.png";
td9.Controls.Add(gradeImg);
lTR.Cells.Add(td9);
T.Rows.Add(lTR);
up.ContentTemplateContainer.Controls.Add(T);
Page.Form.Controls.Add(up);
}
}
protected void btn_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button mybtn = (Button)sender;
Response.Write(mybtn.ID);
}
}
So after a long time thinking how to solved this, with help from some friend, I wrote this in page_load so when the button was pressed and postback occurred the page rebuild my dynamic controls, but only the button press- this is the role of the session.
My new code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (Session["showFiles"] != null)
{
bool flag = (bool)Session["showFiles"];
if (flag == true)
{
fileSaving fs = new fileSaving();
List<fileSaving> fileslist = fs.filesList(hiddenLBL.Text);
addTable(fileslist);
}
}
}
ExpScheduleClass ESCC = new ExpScheduleClass();
List<string> labCourses = ESCC.getCourseList();
// dynamically create a dropdown list (aka DDL)
DDLCourses = new DropDownList();
DDLCourses.DataSource = labCourses; // set the data source
DDLCourses.DataBind(); // bind the data to the DDL control
DDLCourses.AutoPostBack = true;
DDLCourses.SelectedIndexChanged += DDLCourses_SelectedIndexChanged;
coursePH.Controls.Add(DDLCourses);
}
private void DDLCourses_SelectedIndexChanged(object sender, EventArgs e)
{
string[] coursesIDArr = DDLCourses.SelectedValue.Split(' ');
/* courseLBL.Text = coursesIDArr[0];
string courseID = coursesIDArr[0];//take the id from the list*/
courseLBL.Text = coursesIDArr[2];
classLBL.Text = coursesIDArr[4];
currnetYearLBL.Text = coursesIDArr[6];
semesterLBL.Text = coursesIDArr[8];
courseLBL.ForeColor = System.Drawing.Color.Black;
currnetYearLBL.ForeColor = System.Drawing.Color.Black;
semesterLBL.ForeColor = System.Drawing.Color.Black;
classLBL.ForeColor = System.Drawing.Color.Black;
ExpScheduleClass SIEGL = new ExpScheduleClass();
List<String> lessonsList = SIEGL.getLessonList(coursesIDArr[2]);
DDLLessons.DataSource = lessonsList; // set the data source
DDLLessons.DataBind();
}
protected void DDLLessons_SelectedIndexChanged(object sender, EventArgs e)
{
string[] lessonsIdArr = DDLLessons.SelectedValue.Split(' ');
string lessonID = lessonsIdArr[0];
lessonLBL.Text = lessonID;
dateLBL.Text = lessonsIdArr[1];
lessonLBL.ForeColor = System.Drawing.Color.Black;
hiddenLBL.Text = lessonID;
fileSaving fs = new fileSaving();
List<fileSaving> fileslist = fs.filesList(lessonID);
addTable(fileslist);
Session["showFiles"] = true;
}
private void addTable(List<fileSaving> fs)
{
foreach (fileSaving f in fs)
{
UpdatePanel up = new UpdatePanel();
up.ID = "UpdatePanel-" + f.FileName;
up.UpdateMode = UpdatePanelUpdateMode.Conditional;
Table T = new Table();
T.CssClass = "filesTBL";
//------------Header Row------------------------------------------------
TableRow hTR = new TableRow();
TableCell td1 = new TableCell();
Image fileImg = new Image();
fileImg.ImageUrl = "images/" + f.FileExtension + ".png";
td1.Controls.Add(fileImg);
hTR.Cells.Add(td1);
TableCell td2 = new TableCell();
td2.Text = f.ExpName;
hTR.Cells.Add(td2);
TableCell td3 = new TableCell();
Image expImg = new Image();
expImg.ImageUrl = "images/magnet.png";
td3.Controls.Add(expImg);
hTR.Cells.Add(td3);
T.Rows.Add(hTR);
//------------Middle Row------------------------------------------------
TableRow mTR = new TableRow();
TableCell td4 = new TableCell();
td4.Text = "";
mTR.Cells.Add(td4);
TableCell td5 = new TableCell();
td5.Text = f.TeamID.ToString();
mTR.Cells.Add(td5);
TableCell td6 = new TableCell();
Image teamImg = new Image();
teamImg.ImageUrl = "images/team3.png";
td6.Controls.Add(teamImg);
mTR.Cells.Add(td6);
T.Rows.Add(mTR);
//------------Last Row------------------------------------------------
TableRow lTR = new TableRow();
TableCell td7 = new TableCell();
HyperLink downloadLink = new HyperLink();
//downloadLink.Attributes.Add("href", "/igroup39/test2/project/reportFiles/" + f.FileName);
downloadLink.Attributes.Add("href", "http://proj.ruppin.ac.il/igroup39/test2/tar5/tar5.zip");
downloadLink.ImageUrl = "images/download2.png";
downloadLink.ToolTip = "לחץ להורדה";
td7.Controls.Add(downloadLink);
lTR.Cells.Add(td7);
TableCell td8 = new TableCell();
if (f.ReportGrade != -1)
{
td8.Text = f.ReportGrade.ToString();
}
else
{
TextBox tb = new TextBox();
tb.ID = f.FileName + "-tb";
gFN = tb.ID;
tb.Width = 40;
td8.Controls.Add(tb);
Button btn = new Button();
btn.ID = f.FileName + "-btn";
btn.Text = "הזן ציון";
btn.Click += new EventHandler(btn_Click);
td8.Controls.Add(btn);
}
lTR.Cells.Add(td8);
TableCell td9 = new TableCell();
Image gradeImg = new Image();
gradeImg.ImageUrl = "images/grade.png";
td9.Controls.Add(gradeImg);
lTR.Cells.Add(td9);
T.Rows.Add(lTR);
up.ContentTemplateContainer.Controls.Add(T);
Page.Form.Controls.Add(up);
//filesTablesPH.Controls.Add(up);
}
}
protected void btn_Click(object sender, EventArgs e)
{
Button mybtn = (Button)sender;
string btnID = mybtn.ID;
string[] file_Name = btnID.Split('-');
string tbID = file_Name[0] + "-tb";
string grade = ((TextBox)filesTablesPH.FindControl(tbID)).Text;
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert("+grade+")", true);
}
It says createPassword and repeatPassword are not in the current context. Why and what happens ?
Code;
public MainPage()
{
InitializeComponent();
TextBox createPassword = new TextBox();
createPassword.Width = 400;
TextBox repeatPassword = new TextBox();
repeatPassword.Width = 400;
Button createButton = new Button();
createButton.Content = "Create New Password";
createButton.Click += new RoutedEventHandler(savePassword);
StackPanel content = new StackPanel();
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Children.Add(createPassword);
content.Children.Add(repeatPassword);
content.Children.Add(createButton);
LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
string password1 = createPassword.Text;
string password2 = repeatPassword.Text;
}
createPassword and repeatPassword must be class members to use them in different class methods:
TextBox createPassword;
TextBox repeatPassword;
public MainPage()
{
InitializeComponent();
createPassword = new TextBox();
createPassword.Width = 400;
repeatPassword = new TextBox();
repeatPassword.Width = 400;
Button createButton = new Button();
createButton.Content = "Create New Password";
createButton.Click += new RoutedEventHandler(savePassword);
StackPanel content = new StackPanel();
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Children.Add(createPassword);
content.Children.Add(repeatPassword);
content.Children.Add(createButton);
LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
string password1 = createPassword.Text;
string password2 = repeatPassword.Text;
}
I have the below code. I think I have pretty close to what I need. There is a main tab at startout (which does not contain tb, tb1, tb2, and tb3. Once I click the button, a tab is generated containing tb, tb1, tb2, tb3.
tb, tb1,tb2, and tb3 show errors of not existing. I simply cannot figure out how to get these saved.
public partial class Form1 : Form
{
public string status = "no";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = txtName.Text;
//validate information
try { }
catch { }
//create new tab
string title = name;
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
//Add Labels
Label lb = new Label();
lb.Text = "Denomination:";
lb.Location = new System.Drawing.Point(150, 75);
lb.Name = "lbl";
lb.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb);
Label lb2 = new Label();
lb2.Text = "Year:";
lb2.Location = new System.Drawing.Point(150, 120);
lb2.Name = "lbl2";
lb2.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb2);
Label lb3 = new Label();
lb3.Text = "Grade:";
lb3.Location = new System.Drawing.Point(150, 165);
lb3.Name = "lbl3";
lb3.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb3);
Label lb4 = new Label();
lb4.Text = "Mint Mark:";
lb4.Location = new System.Drawing.Point(150, 210);
lb4.Name = "lbl4";
lb4.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb4);
//Add text boxes
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(250, 75);
tb.Name = "txt";
tb.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb);
TextBox tb1 = new TextBox();
tb1.Location = new System.Drawing.Point(250, 120);
tb1.Name = "txt1";
tb1.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb1);
TextBox tb2 = new TextBox();
tb2.Location = new System.Drawing.Point(250, 165);
tb2.Name = "txt2";
tb2.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb2);
TextBox tb3 = new TextBox();
tb3.Location = new System.Drawing.Point(250, 210);
tb3.Name = "txt3";
tb3.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb3);
//put data inside of textboxes
tb.Text = txtCoin.Text;
tb1.Text = txtYear.Text;
tb2.Text = txtGrade.Text;
tb3.Text = txtMint.Text;
// Add delete button
Button bn = new Button();
bn.Location = new System.Drawing.Point(560, 350);
bn.Name = "btnDelete";
bn.Text = "Delete";
bn.Size = new System.Drawing.Size(100, 50);
bn.Click += MyClick;
myTabPage.Controls.Add(bn);
}
private void MyClick(object sender, EventArgs e)
{
Form2 myform = new Form2();
myform.ShowDialog();
if (status == "yes")
{ tabControl1.TabPages.Remove(tabControl1.SelectedTab); }
status = "no";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
int counter;
int ccounter;
string outLine ;
string pathFileName = Path.Combine(Application.StartupPath, "coins.dat");
StreamWriter writeIt = new StreamWriter(pathFileName);
foreach (TabPage tabPage in tabControl1.TabPages)
{
if (tabControl1.TabCount > 1)
{
outLine = tabPage + tb.Text + tb1.Text + tb2.Text + tb3.Text + "\t";
writeIt.WriteLine(outLine);
}
if (tabControl1.TabCount == 1)
{
outLine = tabPage + "\t";
writeIt.WriteLine(outLine);
}
}
writeIt.Close();
}
}
}
You need to store tb1, etc in fields in your form so they can be accessed by other methods.
tb, tb1,tb2, and tb3 show errors of not existing.
Yes, they would - you're declaring them as local variables within button1_Click. To access them from other methods, you'll either need to just examine the controls within the tab page, or declare them as instance variables instead. However, in that case you'd need to consider the fact that there may be multiple tab pages.
It sounds like you really just need to iterate over the controls within each tab page, and pick out the textboxes. Either that, or perhaps create your own subclass of TabPage which knows about the textboxes. Then you could find each instance of your custom TabPage and ask it to save itself.