How To add all the element in textboxes into an array? - c#

Actually I'm facing problems with my textboxes and just to say that its my first Visual C# Application i do.
I just have many textboxes, around 14 textboxes and all i want is that after the user enter the values in each on i need this value to be saved in an array Temp[]
for example (
textbox1.text=1 so Temp[0]=1
textbox2.text=4 so Temp[1]=4
and etc....
I've included also a pic for my app:
so as you can see i the user must enter values for the first set and another values for the second set and i need to save the entered values in each set in an array to show later the union and the intersection of the sets.

Try this.
For 1st panel
int a=0;
foreach (TextBox tbx in Panel1.Controls.OfType<TextBox>())
{
Temp1[a]= tbx.text ;
a++;
}
For 2nd Panel
a=0;
foreach (TextBox tbx in Panel2.Controls.OfType<TextBox>())
{
Temp2[a]= tbx.text ;
a++;
}
Edit: You have to define 1st set of textboxes in panel1 and 2nd set in panel2.

Related

how to add up numbers in a listbox in c# on button click

hi guys I'm wondering how I can add numbers to a Listbox on button click. for example, I have a button that adds number 20 to Listbox, but when pressed twice the Listbox reads "2020" instead of 40. How can I fix this? this is what I have is Textbox1.Text = 20.ToString();
int pos = listBox1.Items.IndexOf("20"); if(pos != -1) listBox1.Items[pos]=Int32.Parse(listBox1.Items[pos])+20).ToString();
in the first line you locate the item you want to add the new num to with "IndexOf",you get the position of this item in a temp int "pos".
then in the second line you replace this item with himself + the new num using Int.Parse to change the string in the list to an int.

How to set character limits to a textbox that is read-only?

I am creating a calculator app in visual studio. I created some buttons in windows form that will input numbers in a textbox. Since the input did not came from a keyborad, the MaxLength properties is not working for me. I set the textbox to read only so that the user can only input through the buttons. How can I set the character limits (characters because I add "," in thousands, ten thousands etc. I only allow 12 digits + the 3 commas making a total of 15 characters in a textbox) that in a textbox that is filled with buttons?
You can create a custom TextBox that ensures the text is never larger than the MaxLength property.
class RestrictedTextBox : TextBox
{
public override string Text
{
get
{
return base.Text;
}
set
{
if (value.Length > MaxLength)
base.Text = value.Substring(0, MaxLength);
else
base.Text = value;
}
}
}
Really need more information as to what type of app your using to build the calculator like WPF/Winforms/Web.
Since your using a UI element, and this is strictly a UI display you could do this in the code behind file for the UI page.
You'll need to check the length of the textbox to determine if the length is under whatever your limit is, and if so then add the button's value to the the text property for that textbox
something like the following
If (textbox.Text.Length < 15)
textbox.Text += Button.Content.Value
As for inserting the commas you'll again need to check the length and can insert a comma at the correct spot when needed.

C# : How can I make dynamically controls for a multidimensional array

When I write a number in a label and press a button, it must show me a dynamical allocation: the first column is number current, then 4 columns with comboboxes. So, when I press 3 it might show 3 rows with number and comboboxes and so on. How can I make this allocation? with multidimensional array and list of objects
You need to add a FlowLayoutPanel to your Form and then add rows to it as below:
private void AddRow()
{
//First create and setup your controls that are supposed to be in a row:
ComboBox cb1 = new ComboBox(){ /* settings */ };
// Other Controls
//Then add them to the FlowLayoutPanel:
flowLayoutPanel1.Controls.Add(cb1);
// add other controls
// Then set a line break at the end of row:
flowLayoutPanel1.SetFlowBreak(cb4, true); // let's say cb4 is the last control of the row.
}
Then you can use the method like this:
int numRows;
bool success = int.TryParse(textBox1.text, out numRows); // lets say textBox1 contains number of rows.
if(success)
{
for(int i = 0; i < numRows; i++)
{
AddRow();
}
}

Looping textbox IDs for retrieving text from textboxes in C#

I have 50 text boxes starting from TextBox1 till TextBox50. I want to retrieve values from all these 50 text boxes. I tried looping but it failed. I want some code like TextBox(i).Text where i varies from 1 to 50.
The loop should produce the following result.
Response.Write(TextBox1.Text);
Response.Write(TextBox2.Text);
son on till
Response.Write(TextBox50.Text);
How can I achieve this?
You can use FindControl which takes a string as parameter, pass it "TextBox" + i like:
TextBox tb = this.FindControl("TextBox" + i) as TextBox;
if (tb != null)
{
Response.Write(tb.Text);
}
You can loop thought them simply as following :
string value=""; // store each textbox value in this variable
foreach (Control x in this.Controls) // loop through the controls in the form
{
if (x is TextBox) // if the program found a textbox in the form
{
value = (x as TextBox).Text; // set the value of the textbox number x to the string value
listBox1.Items.Add(value); // here is a listbox to show the resule
}
}

C# Create a bill message box with items that were checked displayed

There's a form with three checkboxes and a button. Upon clicking on the button a messagebox is suppose to show up and display all the items that were checked on the form with price before tax and total. The price before tax and total are taken care of, but how would I display what was checked by the user on that form in the message box that will serve as a bill.
if (checkCheeseSnackBread.Checked == true)
{
price += 10;
items += "Cheese Snack Bread - $10";
}
else
{
price -= 10;
}
Just need some guidance.
First I must say that the code you've written seems to hold a logical eror. If the checkbox isn't checked then you shouldn't do anything. In your code you subtract the item-price from the total price. This way you give a discount of 10. If they don't buy it, don't add it. That's all.
So, now you know what they've bought, you can use a StringBuilder to populate the message.
Quick and dirty:
StringBuilder builder = new StringBuilder();
builder.AppendLine("Ticket")
builder.AppendLine();
if (checkCheeseSnackBread.Checked) // == true is not needed
{
price += 10;
items += "Cheese Snack Bread - $10";
builder.AppendLine(Cheese Snack Bread - $10);
}
// Do the same for other checkboxes
// Add the totals
Messagebox.Show(builder.ToString());
There is an other way: loop through the controls on the form, if it's a checkbox ==> add the text to the StringBuilder. This way it doesn't matter how much checkboxes there are. You just have to make sure that the text-property is used to print on the ticket (messagebox).
If you just want to display a string you generate.
Check this out:
http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

Categories