How can I clear textboxes so submit button can be re-used? - c#

I am making a program for school in C#, and its purpose is to allow the user to enter film data, which it then puts into an object for that film. It will also include other functionality such as the user being able to search for a film (it says I have to make 3 film objects and store them in an array all being input by the user).
I have created the first part of the Windows Forms application and it is a screen that gets all the input from the user like the name, director, rating, etc... and there is a submit button which creates the object. Is there a way, without creating a new form, to use the same screen and clear the textboxes so that when the submit button is clicked again it creates a NEW OBJECT like 'film2'?
Here is my code for the submit button:
private void button1_Click(object sender, EventArgs e)
{
int year = Convert.ToInt32(dBox_year.Text);
Film film1 = new Film(tbox_name.Text, tbox_director.Text, tbox_actor1.Text, tbox_actor2.Text, year, tbox_rating.Text);
filmArray[0] = film1;
}
So, you see how I would like to have the textboxes on the main screen clear themselves, and reuse the same screen but only it would be 'Film film2 = ...' etc.
This is not an assesed piece and we haven't covered this in class yet so I have tried.

private void button1_Click(object sender, EventArgs e)
{
int year = Convert.ToInt32(dBox_year.Text);
Film film1 = new Film(tbox_name.Text, tbox_director.Text, tbox_actor1.Text, tbox_actor2.Text, year, tbox_rating.Text);
filmArray[0] = film1;
//clearing after adding to array
//or you can just use .Clear() method
tbox_name.Text = String.Empty;
tbox_director.Text = String.Empty;
tbox_actor1.Text = String.Empty;
tbox_actor2.Text = String.Empty;
tbox_rating.Text = String.Empty;
}

tbox_name.Clear() - Clears all text from the text box control.(Inherited from TextBoxBase.)

You could use a List instead of an Array, declared at form level:
private List<Film> filmList = new List<Film>();
Then your button click even would look like
private void button1_Click(object sender, EventArgs e)
{
int year = Convert.ToInt32(dBox_year.Text);
filmList.Add(new Film(tbox_name.Text, tbox_director.Text, tbox_actor1.Text, tbox_actor2.Text, year, tbox_rating.Text));
tbox_name.Text = string.Empty;
tbox_director.Text = string.Empty;
tbox_actor1.Text = string.Empty;
tbox_actor2.Text = string.Empty;
tbox_rating.Text = string.Empty;
dBox_year.Text = string.Empty;
}
Here you're creating a new Film object and adding it straight away to the list of films, and then clearing the text boxes afterwards.
If there's a specific reason you need an array, then you can always later do
filmList.ToArray()
Hope this helps!

When Submit button is clicked you want to add the object at the end of the array, not put it at the first position.So you will need an extra variable named, let say, filmCount, which you initialize with 0 and increment on each submit.
Film film1 = new Film(tbox_name.Text, tbox_director.Text, tbox_actor1.Text, tbox_actor2.Text, year, tbox_rating.Text);
filmArray[filmCount++] = film1;
then you clear the texboxes
foreach(TextBox TB in this.Controls)
{
TB.Text = "";
}

Related

How to add entries every time a button is pressed

I am making a mobile app using Xamarin Forms and I am writing all of my code including the visual aspects in c# (in the .cs files).
Essentially I need to be able to add a new entry every time a button is pressed and then get the text entered into said entry.
Right now I can create a new Entry and give it a name that I can use to reference it:
private Entry entry1;
Layout.Children.Add(entry1 = new Entry
{
//entry code
});
//when some button is pressed
string entry1Text = entry1.Text;
I want to make it so that every time the user presses a button, it creates a new entry, but I also need to be able to get the text from it. How can I make it so that it creates a new entry with a new name like entry2, entry3, etc... without manually writing out like 10 entries and then making them visible? I need to do this because I don't know how many entries the user will add (could be more than 10).
int numberOfEntries = 1;
void addEntry_Clicked(object sender, EventArgs e)
{
string entryNumber = numberOfEntries.ToString();
//the following 2 lines are what doesn't work with the name of an entry, but is what I want to do
private Entry entry + entryNumber;
Layout.Children.Add(entry + entryNumber = new Entry
{
//entry code
});
numberOfEntries+=1;
}
//some button is pressed
string entryText = (entry + entryNumber).Text;
The problem is I can't add a number to the name of an entry like entry +"2"
Is this even possible for me to do?
you need to keep a separate data structure to track your controls, like this
Dictionary<string,Entry> entries = new Dictionary<string,Entry>();
private void AddEntry(string name)
{
var entry = new Entry();
myLayout.Children.Add(entry);
entries.Add(name,entry);
}
then you can get their value like this
var text = entries["entryA"].Text;

Referencing a dynamically created control in a new TabPage

newbie programmer here after hours of searching has left me stumped.
I'm having trouble with referencing a control inside a tab created at RunTime with a button press. Basically what I have is a tabletop RPG calculator, using a Windows Form, that has a tabControl holding tab pages, with each tab page holding user-inputted stats for that individual enemy to be used in calculations.
The problem is that I want the user to be able to click a button to generate a new enemy tab page. Here is my code for generating an enemy tab page with a TextBox.
int enemyNumber = 0;
// Creates a new Enemy Tab
private void button2_Click_1(object sender, EventArgs e)
{
// Create a new TabPage
var newTabPage = new TabPage()
{
Text = "Enemy " + enemyNumber,
};
// Add Enemy Name Box
var newEnemyNameBox = new TextBox()
{
Name = "enemyNameBox" + enemyNumber,
Text = "",
Location = new Point(127, 11),
Size = new Size(133, 20)
};
// Add the controls to the new Enemy tab
newTabPage.Controls.Add(newEnemyNameBox);
// Add the TabPage to the TabControl
tabControl1.TabPages.Add(newTabPage);
// Increases the enemy's "reference number" by 1
// So that enemy tabs will be generated in order enemyTab0, enemyTab1, etc.
enemyNumber += 1;
}
This all works nicely. Unfortunately, after this point things have gotten ugly. I need to reference that TextBox named "enemyNameBox" + enemyNumber, and I'm not sure how to do so.
What I did was create "archVariables" to store the values from whatever enemy tab is selected, then use the appropriate archVariable in the program's calculations. IE: archEnemyName. The idea is that whatever tab the user is currently selected on (determined via SelectedIndex) the TextBox from that page will be used for the program's output.
Here are the two things I've tried after researching the matter:
// Attempt 1
private void defendCalcButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tabControl1.SelectedIndex; i++)
{
archEnemyNameBox = ((TextBox)Controls["enemyNameBox" + i]).Text;
}
}
This code simply throws a NullReferenceException when I press the button. So after researching more I tried this:
// Attempt 2
private void defendCalcButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tabControl1.SelectedIndex; i++)
{
TextBox tb2 = new TextBox();
tb2 = ((TextBox)(enemyTab.Controls.Find("enemyNameBox" + i, true)));
archEnemyNameBox = tb2.Text;
}
}
This time I got an Error: Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.TextBox'
I feel like the second method I have here is probably closer to the correct way to do this, but apparently I'm still not getting it right. I've learned a lot by searching the information on stackoverflow and msdn.microsoft but nothing has gotten me past this problem.
Any help would be appreciated.
basically the problem with your second attemp is that enemyTab.Controls.Find("enemyNameBox" + i, true) returns an array of Controls Control[] and you're trying to convert that to a Control here is the problem, you should get the first control in that array and then convert it to a Control so it should be like this:
private void defendCalcButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tabControl1.SelectedIndex; i++)
{
TextBox tb2 = new TextBox();
tb2 = ((TextBox)(enemyTab.Controls.Find("enemyNameBox" + i, true)[0]));
archEnemyNameBox = tb2.Text;
}
}
but it is not the BestWay to do so it seems that everytime a user adds a new tabPage it will have the same Controls right? so why not create an userControl with any Control you have on your TabPage? so when you press the user press to add a new tab your code should be like so:
private void CreateNewEnemyTab()
{
var newTabPage = new TabPage()
{
Text = "Enemy " + enemyNumber,
};
EnemyTabUserControl enemyTab = new EnemyTabUserControl(enemyNumber);
here the EnemyTabUserControl should have all the components you need;
newTabPage.Controls.Add(enemyTab);
tabControl1.TabPages.Add(newTabPage);
}
and the code to bring the TextBox from the current tab could be as follow (you are going to need to reference LINQ)
using System.Linq;
//First Lets create this property, it should return the selected EnemyTabUserControl inside the tabControl
public EnemyTabUserControl CurrentTab {
get {
return tabControl1.SelectedTab.Controls.OfType<EnemyTabUserControl>().First();
}
}
// then if we make the textbox you want to reference from outside the code we can do this
CurrentTab.NameOfTheTextBox;
Patrick has solved your fundamental problem, but I don't think you need the loop in there at all. Here I've broken the steps out so you can see what needs to happen a little better:
private void defendCalcButton_Click(object sender, EventArgs e)
{
Control[] matches = this.Controls.Find("enemyNameBox" + tabControl1.SelectedIndex.ToString(), true);
if (matches.Length > 0 && matches[0] is TextBox)
{
TextBox tb = (TextBox)matches[0];
archEnemyNameBox = tb.Text;
}
}

C# listbox add data from another form

I'm a C# student and I'm a little stuck at on my midterm project.
I dropped my project and spec here: https://www.dropbox.com/sh/eo5ishsvz4vn6uz/CE3F4nvgDf
If you run the program, it will come to the last area I left off at..
private void btnAddScore_Click(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList = tempScore; (was Form1.scoreList[i] = tempScore;)
}
txtScoresList.Text += Convert.ToString(tempScore) + " ";
}
There's a main form, a secondary add form, and a third and fourth form, all the controls are in place, just the wiring is what's left over.
(1) In the above code, there are supposed to be 3 scores passed to the main form, which, along with a student name string, are to populate the ListBox on the main form. I can't figure out how to access that ListBox, anytime I type "listStudents" nothing happens.
(2) I'm also not sure how to limit an input of only 3 scores when I'm clicking the "add" button 1 time, which means I know my for loop is probably completely wrong. I don't know if I should save those scores to an array, list, or individual vars, being that it can be 3 (or more, but 3 is fine) scores.
(3) When I hit "OK" on the AddNewStudent form, do I write my code there to populate the main form ListBox, or does it go in the main form?
Update:
private void Form1_Load(object sender, EventArgs e)
{
lbStudents.Items.Clear();
//something like
foreach (decimal i in scoreList2)
{
scoreList = scoreList2.ToString(); //gives me a cannot implicitly convert error
}
lbStudents.Items.Add(tempInfo1 + " " + scoreList2);
}
//I want the listbox to populate like "Name - |100| |90| |80|"
This code seems to me, to be correct, for getting the ListBox populated, but I'm unsure of how to add the entire contents of the list to a string, and then add that to the listbox.
This will get your code building and running.
Change the following declaration in form1
public static decimal[] scoreList = new decimal[3];
to
public static List<decimal> scoreList = new List<decimal>();
and update your btnAddScore_Click handler to
//save scores to temp static var, populate noread txtbox txtScoresList with scores
for (int i = 0; i < 3; i++)
{
//save score to static var for trans-form data sending
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList.Add(tempScore);
}
The rest is not too difficult, you should be able to work it out.

I need to know how to take the selected item of a comboBox and make it appear on a windows form application?

I have a windows form application with a ComboBox on it and I have some strings in the box. I need to know how when I select one of the strings and press my create button, how can i make that name show up on another windows form application in the panel I created.
Here is the code for adding a customer
public partial class AddOrderForm : Form
{
private SalesForm parent;
public AddOrderForm(SalesForm s)
{
InitializeComponent();
parent = s;
Customer[] allCusts = parent.data.getAllCustomers();
for (int i = 0; i < allCusts.Length; i++)
{
Text = allCusts[i].getName();
newCustomerDropDown.Items.Add(Text);
newCustomerDropDown.Text = Text;
newCustomerDropDown.SelectedIndex = 0;
}
now when i click the create order button I want the information above to be labeled on my other windows form application.
private void newOrderButton_Click(object sender, EventArgs e)
{
//get the info from the text boxes
int Index = newCustomerDropDown.SelectedIndex;
Customer newCustomer = parent.data.getCustomerAtIndex(Index);
//make a new order that holds that info
Order brandSpankingNewOrder = new Order(newCustomer);
//add the order to the data manager
parent.data.addOrder(brandSpankingNewOrder);
//tell daddy to reload his orders
parent.loadOrders();
//close myself
this.Dispose();
}
The context is not very clear to me, but if I got it right, you open an instance of AddOrderForm from an instance of SalesForm, and when you click newOrderButton you want to update something on SalesForm with data from AddOrderForm.
If this is the case, there are many ways to obtain it, but maybe the one that requires the fewer changes to your code is this one (even if I don't like it too much).
Make the controls you need to modify in SalesForm public or at least internal (look at the Modifiers property in the Design section of the properties for the controls). This will allow you to write something like this (supposing customerTxt is a TextBox in SalesForm):
parent.customerTxt.Text = newCustomerDropDown.SelectedItem.Text;

Custom dialog box control in c#

I have a question I hope some of you might be able to answer, I haven't found any ways to do this on google or here.
What I want:
- A custom control that functions just like an input box. (But it has to be a winform control that can be added to a form. Not a form.)
- It has to be able to grab the value from its text box and send it to the parent in the function it was called in.
Here is how I want to call it:
string str = MyBox.GetString("control title");
Can anyone help?
I don't know if this is event possible in c#. I couldn't figure it out, but if anyone can please answer!
You want something like this
public partial class MyBox : Form
{
public MyBox()
{
InitializeComponent();
}
public string ResultText { get; set; }
public static string GetString(string title)
{
var box = new MyBox {Text = title};
if (box.ShowDialog() == DialogResult.OK)
{
return box.ResultText;
}
return string.Empty;
}
private void okButton_Click(object sender, EventArgs e)
{
this.ResultText = txtUserInput.Text;
this.DialogResult = DialogResult.OK;
}
}
where MyBox would be a Form with TextBox - txtUserInput and an okay button linked to the okButton_Click event.
And you can make calls from other forms like this:
string userInput = MyBox.GetString("Title for MyBox");
If you want the box to reside on a form, you can just use a regular TextBox to get the inupt. Maybe eclose it in a GroupBox to give a "title", add a description label.
Lastly, and most importantly, add an "Update" Button to the GroupBox. Inside this button's Click handler, you can retrieve the value of the textbox with string str = textbox.Text.

Categories