I want to insert text into textboxes creating during run time
user can decide how many textboxes will create (range 1-99) I am using this code to create textboxes
for (int i = 0; i < Calculation.Num; i++)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtBox" + i;
txtRun.Location = new System.Drawing.Point(35, 50 + (20 * i) * 2);
txtRun.Size = new System.Drawing.Size(75, 25);
this.Controls.Add(txtRun);
}
Suppose user create 5 textboxes it will look like this
after that user insert numbers in each textbox then click calculate then all numbers stored in array using this code
Button btn = sender as Button;
if (btn.Name.Equals("btn1"))
{
var sum = this.Controls.OfType<TextBox>()
.Where(t => char.IsDigit(t.Name.Reverse().Take(1).FirstOrDefault())
&& t.Enabled)
.Select(t =>
{
double i;
if (!double.TryParse(t.Text, out i)) { return 0d; }
NumbersHolder[abc] = i;
abc++;
return i;
})
.Sum();
}
after that i want to calculate those numbers if number is between 70-76 replace that number with 2.75 if it is between 77-79 replace it with 3.0 if it is between 80-86 replace with 3.3 and if it is 87-100 replace with 4.0
A little confused by what you are asking.
You want the TextBox controls you are generating in the for loop to have values?
Just set
txtRun.Text = "your value";
before adding it.
Related
I want the following code to give me the exact number of S values from the textboxes named : Box1_1 , Box1_2 , Box1_3, Box1_4, Box1_5 ...
But when i try to see the value it's always blank. What can i do ?
for (int i = 1; i <= 7; i++){
for (int j = 1; j <= 10; j++){
string box = "Box" + i.ToString() + "_" + j.ToString();
TextBox nameBox = new TextBox();
nameBox.Name = box;
if(string.Compare(nameBox.Text, "S")==0){
numberS++;
}
}
}
This is a cheeky little one-liner using Linq (split over multiple lines for clarity):
var textBoxes = this.Controls
.OfType<TextBox>() // controls that are TexteBoxes
.Where(t => t.Name.StartsWith("Box") && t.Text.StartsWith("S"))
.ToList();
int numberS = textBoxes.Count();
We get all TextBox controls using OfType<TextBox>()
This assumes the name of the TextBoxes you're interested in start with "Box". The corresponding Linq is Where(t => t.Name.StartsWith("Box")).
It looks like you're only interested in the TextBoxes that have a value that starts with "S". The corresponding linq is Where(t => t.Text.StartsWith("S")). I have combined these in a single Where.
Then we get the count: .Count()
If you want the count of textboxes that contain S (not just start with S) then use t.Text.Contains("S") in the Where clause instead.
If you want to get the TextBox names (Box1_1, Box1_2, etc) then you can use Select which will take the Name property from each TextBox and return a List<string>
var txtNames = this.Controls
.OfType<TextBox>() // controls that are TexteBoxes
.Where(t => t.Name.StartsWith("Box") && t.Text.StartsWith("S"))
.Select(t => t.Name) // select textbox names
.ToList();
txtNames is a List<string> containing the textbox names which start with S.
First of all, you need a collection of your TextBox'es or scrap them from your window. Example of how to do the second is here.
Here is your code that I modified:
public int CountS()
{
var numberS = 0;
for (int i = 1; i <= 7; i++)
{
for (int j = 1; j <= 10; j++)
{
string box = "Box" + i.ToString() + "_" + j.ToString();
TextBox nameBox
= UIHelper.FindChild<TextBox>(Application.Current.MainWindow, box); //using example linked above
//= textBoxes.First(tbx => tbx.Name == box); //if you got collection of your textboxes
numberS += nameBox.Text.Count(c => c == 'S'); //if you wont also count a small "s" add .ToUpper() before .Count
}
}
return numberS;
}
My table in MySQL is like this:
I have a panel in my Form, and I want to create a TextBox for each data on this table and write sikicerik data to that TextBox's text.
Actually I did it but it creates only one TextBox and selects only the first data on the table.
My code is like this:
int count = oku.FieldCount;
reader.Read();
{
for (int i = 0; i < count; i++)
{
TextBox txt1 = new TextBox();
Point txtyer = new Point(x, y);
txt1.Text = reader["sikicerik"].ToString();
txt1.Name = i.ToString();
x = x + 25;
y = y + 25;
panel1.Controls.Add(txt1);
}
}
It creates just one TextBox and writes "5" in it.
How can I do that repeatedly?
What you need to understand is that you need to call reader.Read() after going through each record. Let's say your result data set has 5 records, so in order to read the 1st record, you need to call reader.Read() which would populate the reader object with the appropriate data. In order to read the 2nd record, you need to again call reader.Read(). Something like this:
int count = reader.FieldCount;
for (int i = 0; i < count; i++)
{
reader.Read();
TextBox txt1 = new TextBox();
Point txtyer = new Point(x, y);
txt1.Location = txtyer;
txt1.Text = reader["sikicerik"].ToString();
txt1.Name = i.ToString();
y = y + 25;
panel1.Controls.Add(txt1);
}
The reason why you may not be able to view all TextBoxes is that you are updating both x and y coordinates of the TextBox. What you may probably want to do is just increment the y coordinate as above and all the TextBoxes would appear vertically. You may also want to resize your form so that the TextBoxes don't get hidden.
Also, as #Steve mentioned in the comments above (which I missed), you need to assign the location of the new TextBoxes created as is done in the code above.
I suppose that you get the FieldCount from the reader variable.
Now in this context you should loop over the read and inside the loop create the textbox for each field retrieved by the original query.
Finally, if you don't set the Location property of the TextBoxes they will be created each on top of the others and you see only the topmost one with the latest value obtained in the loop
// Get the number of fields present in the reader....
int count = reader.FieldCount;
// Read one record at time
while(reader.Read())
{
// Create a textbox for each field in the record
for (int i = 0; i < count; i++)
{
TextBox txt1 = new TextBox();
// Set its location on screen
// Probably if you have many fields you need to
// use a better algorithm to calculate x,y position
txt1.Location = new Point(x, y);
txt1.Text = reader[i].ToString();
txt1.Name = i.ToString();
x = x + 25;
y = y + 25;
panel1.Controls.Add(txt1);
}
}
If you want to create textboxes only for the field sikicerik you have two options. The first one is the recommended because it causes less overhead on your database consist in changing the SELECT query used to build the read to
SELECT sikicerik FROM yourTableName
The other option is a simple change to your code. You don't need to loop over FieldCount because you already know that you are interested in only one field
// Read one record at time
while(oku.Read())
{
textBox1.Text = oku["soru"].ToString();
label1.Text = Form2.sinavno;
// Create the single textbox required for the only field required
TextBox txt1 = new TextBox();
// Set its location on screen
txt1.Location = new Point(x, y);
txt1.Text = oku["sikicerik"].ToString();
txt1.Name = i.ToString();
x = x + 25;
y = y + 25;
panel1.Controls.Add(txt1);
// Repeat the loop for each record.
}
I have a basic program with 26 TextBoxes named textBox1 to textBox26 and a button. The button currently gives a value of 1,x or 2 to each TextBox. I also want it to compare the values of a TextBox and the one with a name contaning a number 13 greater than the last without using an array or multiple if/else statements. For example I want to compare textBox1 with textBox14. I currently have this code:
Random r = new Random();
int ran;
int x;
int y;
private void button1_Click(object sender, EventArgs e)
{
foreach (Control b in this.Controls.OfType<TextBox>())
{
ran = r.Next(1, 4);
if (ran == 1)
{
b.Text = "1";
}
else if (ran == 2)
{
b.Text = "x";
}
else if (ran == 3)
{
b.Text = "2";
}
x = b.TabIndex;
y = x + 13;
var box = (TextBox)this.Controls.Find("textBox" + x.ToString());
var box1 = (TextBox)this.Controls.Find("textBox" + y.ToString());
box.Text = box1.Text;
}
}
This gives the the error message:
There is no argument given that corresponds to the required formal parameter 'search Children' of 'Control.ControlCollection.Find(string, bool)'
on the lines with:
var box = (TextBox)this.Controls.Find("textBox" + x.ToString());
var box1 = (TextBox)this.Controls.Find("textBox" + y.ToString());
Your specific error is because you're not providing a value for the argument (as the error states...):
var box = (TextBox)this.Controls.Find("textBox" + x.ToString(), false);
Use false, as in my example, or true depending on your needs. Do you want the search to include controls (i.e. children) that are nested inside the controls you are searching?
How would I go about making a for loop that changes the text in more than 1 textbox?
for (int i; i < 5; i++)
{
textbox(i).text = "something"
}
But I don't know how to get the I to represent the number after the textbox, does anyone know how to?
Store the textboxes in an Array and then loop over the array
for (int i; i < 5; i++)
{
textboxArray[i].text = "something"
}
You could use Controls.Find:
var txts = this.Controls.Find("textbox" + i, true); // true for recursive search
foreach(TextBox txt in txts)
txt.Text = "something";
or - if the TextBoxes are in the same container control(like the Form or a Panel)- with LINQ:
var txts = this.Controls.OfType<TextBox>().Where(txt => txt.Name == "textbox" + i);
foreach(TextBox txt in txts)
txt.Text = "something";
Actually you don't need the loop variable, you could also use String.StartsWith to get all:
var txts = this.Controls.OfType<TextBox>().Where(txt => txt.Name.StartsWith("textbox"));
foreach(TextBox txt in txts)
txt.Text = "something";
if you dont want to alter every textbox on your form just simply add them to a List:
List<TextBox> TextBoxes = new List<TextBox>();
TextBoxes.Add(This.TextBox1);
TextBoxes.Add(This.TextBox3):
then as others have suggested you could either linq or regular foreach the textboxes in the list
TextBoxes.Foreach(Textbox => TextBox.Text = "something");
or
foreach (TextBox r in TextBoxes)
{
r.Text = "something;
}
I have a form (Windows Forms) with dynamically created textboxes:
TextBox[] tbxCantServ = new TextBox[1];
int i;
for (i = 0; i < tbxCantServ.Length; i++)
{
tbxCantServ[i] = new TextBox();
}
foreach (TextBox tbxActualCant in tbxCantServ)
{
tbxActualCant.Location = new Point(iHorizontal, iVertical);
tbxActualCant.Name = "tbx" + counter++;
tbxActualCant.Visible = true;
tbxActualCant.Width = 44;
tbxActualCant.MaxLength = 4;
this.Controls.Add(tbxActualCant);
}
Now I want to fill them with data, how could I do that?
If I created some textboxes dynamically with the names:
"tbxActualServ.Name = "txt" + counter;"
How can I write in them? How can I access to them?
For example, if I have created tbx1, tbx2 and tbx3, I would have a "for" that fills tbx1.Text with "1", tbx2.Text with "2", and tbx3.Text with "3".
something like
"for from i=0 to counter {
tbx[i] = i
}"
of like:
this.Controls.OfType<TextBox>().Where(r => r.Name == "tbx" + counter).¿¿Write??(r => r.Text = i).ToString();
Thanks!
You could do something like this:
this.Controls.OfType<TextBox>().ToList<TextBox>().ForEach(tb => tb.Text = "bla bla");
Evening,
Guessing from your tags that this is a web forms project.. Im going to have to make some other assumptions.
I am guessing that you are creating your text boxes in code, something like
TextBox tb1 = new TextBox();
form1.Controls.Add(tb1);
TextBox tb2 = new TextBox();
form1.Controls.Add(tb2);
If this is the case then I believe that you could do something like this:
for (int i = 0; i < 2; i++)
{
TextBox tb1 = page.findControl("tb" + i.ToString());
tb1.Text = "This is number " + i.ToString();
}
There is another alternative, you could keep a collection of the controls as you create them, you could then iterate over the collection.
To be honest, without more details about your code it will be difficult to give a full answer, I think that this answers what you are looking for, if not update your question with more details and more of the code (the code where you are dynamically creating the controls would be useful)
While it's possible to access controls by their names (the way you do it depends on the technology - are you using WinForms, WPF, Web Forms, ...?), using an array of controls is a much better solution. Here's some pseudo-C#:
MyControl[] controls = new MyControl[length];
for(int n = 0; n < controls.Length; n++)
{
controls[n] = new MyControl(...);
}
// ...
for(int n = 0; n < controls.Length; n++)
{
DoSomethingWith( controls[n] );
}