I have a textbox for data entry and 10 textboxes for showing data. 10 viewer textboxes are visible=false by default. For example when I enter textbox count to be "3" , only 3 textboxes should be visible. (Then I can do whatever I want with those textboxes)
Here's my code;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
SqlCommand pencere = new SqlCommand("select COUNT (distinct(grup)) as ürün from fiyat", conn);
SqlCommand pencereisimleri = new SqlCommand("select distinct(grup) as ürün from fiyat", conn);
conn.Open();
SqlDataReader dr = pencere.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter(pencereisimleri);
DataTable dt1 = new DataTable();
da.Fill(dt1);
List<String> stringArr = new List<String>();
for (int a = 0; a < dt1.Rows.Count; a++)
{
TextBox[a].Visible = true;
TextBox[a].Text = "Open Textbox";
}
Not sure, if you're going for this, but how about adding the text boxes to a collection, like an array or List?
Example:
private List<TextBox> boxes = new List<TextBox>();
boxes.Add(textBox1);
boxes.Add(textBox2);
// etc...
Then you could use a foreach (or for) loop, to go through your boxes:
for (int a = 0; a < dt1.Rows.Count; a++)
{
boxes[a].Visible = true;
boxes[a].Text = "Open Textbox";
}
int count = dt1.Rows.Count;
foreach (Control x in this.Controls)
{
if (count == 0)
break;
if (x is TextBox)
{
((TextBox)x).Text = "Open Textbox";
((TextBox)x).Visible = true;
count--;
}
}
you can bind visibility to an array
Visibility[] tbViz = new Visibility[10];
public Visibility[] TbViz { get { return tbViz; } }
public void UpdateViz(int num)
{
for (int i = 0; i < tbViz.Length; i++)
{
if (i < num)
tbViz[i] = System.Windows.Visibility.Visible;
else
tbViz[i] = System.Windows.Visibility.Hidden;
}
}
Visisbility="{binding path=TbViz[0]}"
if winforms same approach
public void UpdateViz(int num)
{
for (int i = 0; i < tbViz.Length; i++)
tbViz[i].Visisble = (i < num);
}
Related
I have a special thing to solve and can find a solution.
The Datagrid is bound to SQL query and has dynamic inner Join that is detectable all-time changing. The user can select any imported Data to show differences between up to 10 databases.
The Labelname of columns is EPreis and BPreis.
Now I need to find all EPreis and all BPreis eggs 5xBPreis 5xEPreis on Row.
Funktion1
BPreis cheapest Background color = green
Secend Preis Background Color = Yello
3...... 10.
The same on EPreis.
C#
protected void run_Click(object sender, EventArgs e)
{
SqlDataAdapter adp1 = new SqlDataAdapter(getSearch, con);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
mergeresult.DataSource = dt1;
mergeresult.DataBind();
}
protected void mergeresult_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.count - 1; i++)
{
for (int cellIndex = 0; cellIndex < Row.Cells.Count; cellIndex++)
{
if ((Label)e.row.cell.cellindex == "EPreis")
{
//? Howto find all PReis and EPreis in row.count
// is posible to put cell[x] and cell.text in array
// and at end of row sorting array 1 to x to colering
// the cell.
// Later to expand funktion Tolltip or mous over to show %
// Diverent to cheeperst
}
}
}
}
}
}
I found a solution but it still doesn't work properly.
The highest and lowest prices are sometimes reversed.
enter image description here
if (e.Row.RowIndex >= 0)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int totalRow = mergeresult.Rows.Count;
int totalColumns = mergeresult.Columns.Count;
for (int i = 0; i < totalRow + 1; i++)
{
//int anzahl = (int)ViewState["Anzahlfrom"];
//string[] NumOfPreisfields = new string[anzahl];
var Preisfields = new List<string>();
var Preisfieldslist = new List<(int cellNr, string Preis, string color)>();
for (int j = 0; j < e.Row.Cells.Count; j++)
{
var SearchColumnName = "EK";
DataControlFieldCell cell = (DataControlFieldCell)e.Row.Cells[j];
if (cell.ContainingField is BoundField)
{
if (((BoundField)cell.ContainingField).DataField.Contains(SearchColumnName))
{
Preisfields.Add(j.ToString());
Preisfieldslist.Add((j, cell.Text.ToString(), ""));
//cell.ForeColor = System.Drawing.Color.Red;
}
}
if (j == e.Row.Cells.Count -1)
{
//var SortedPreisfieldList = Preisfieldslist.OrderBy(r => r.Preis);
var LowerstPreis = Preisfieldslist.Min(r => r.Preis);
var HiegerstPreis = Preisfieldslist.Max(r => r.Preis);
for (int c = 0; c < Preisfieldslist.Count; c++)
{
if (LowerstPreis == Preisfieldslist[c].Preis)
{
int n = Preisfieldslist[c].cellNr;
e.Row.Cells[n].ForeColor = System.Drawing.Color.Green;
}
if (HiegerstPreis == Preisfieldslist[c].Preis)
{
int n = Preisfieldslist[c].cellNr;
e.Row.Cells[n].ForeColor = System.Drawing.Color.Red;
}
}
}
}
}
} // ende for (int i = 0; i < totalRow +1 ; i++)
}
I am trying to store a result of a candidate but it will return a 0; nothing else, and it will always increment the value of cot if value if arraylist are match or not match but it will increase the value of cot.
Please help me - thanks in advance
public void get_result()
{
int cot = 0, inc = 0;float final_result=0;
ArrayList arr = new ArrayList();
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("(select correct from quiz_question)", con);
con.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
for (int j = 0; dr.Read(); j++)
{
inc++;
string ans = dr[0].ToString();
arr.Add(ans);
Response.Write(arr.ToString());
}
ArrayList arr1 = new ArrayList();
arr1 = (ArrayList)Session["correct_ans"];
for(int l = 0; l < inc; l++)
{
if(arr[l]!=arr1[l])
{
cot++;
}
}
Session["result"] = (Double)((cot/100)*100);
//final_result = ((cot) / 100);
//final_result = final_result * 100;
//Session["result"] = (float)final_result;
//Response.Write(final_result);
}
I'm trying to get data from a file and store them in an array then display the data in a listbox the find then sum and display it in a text box. Here's my code and it doesn't work. I'm not sure what i'm doing wrong.
private void findClick(object sender, EventArgs e)
{
int sum;
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader InputFile = new StreamReader(ofd.FileName))
{
while (InputFile.EndOfStream == false)
{
int[] array = new int[listBox.Items.Count];
for (int i = 0; i < listBox.Items.Count; i++)
{
// array[i] = Convert.ToInt32(listBox.Items[i].ToString());
array[i] = int.Parse(listBox.Items[i].ToString());
sum = array.Sum();
TotalAmtlabel.Text = sum.ToString("N0");
TotalNumberslabel.Text = listBox.Items.Count.ToString();
TotalAmountlabel.Text = string.Format("{0:N0}", sum);
}
}
}
}
}
}
listBox.Items.AddRange(File.ReadAllLines(ofd.FileName));
Try this and modify according to your needs:
string[] amounts = File.ReadAllLines(ofd.FileName);
int currentSum = 0;
int totalSum = 0;
ListItem[] amountItems = new ListItem[amounts.Length];
for (int i = 0; i < amounts.Length; i++)
{
if (int.TryParse(amounts[i], out currentSum))
{
totalSum += currentSum;
}
amountItems[i] = amounts[i];
}
listBox.Items.AddRange(amountItems);
TotalAmountlabel.Text = string.Format("{0}", totalSum);
You can also datasource to bind the list. Please go through below MSDN references once atleast to understand security cautions:
ListItem
ListBox
Hello everyone I need some help with the positioning of an array of buttons.I want to make this function so it scans the name of the previous button and it names the next one +1,afterwards I want to position these buttons on the screen having a certain space between them and them being positioned in the center of the screen.I have tried many times to modify my method but I don't know how to get this method to work.
This is how my method looks like.
UPDATED
PS.Reference not set to an instance of an object Q.Q
public Button[] ButtonCreator(byte numOfBtnsNeeded,Form1 form)
{
Button[] mybtns = new Button[numOfBtnsNeeded];
foreach (Button b in mybtns)
{
for (int i = 0; i < mybtns.Length; i++)
{
mybtns[i].Name = i.ToString();
mybtns[i].Parent = form;
mybtns[i].Height = 50;
mybtns[i].Width = 50;
for (int k = i + 1; k < mybtns.Length; k++)
{
mybtns[i].Location = new Point(190, 80);
mybtns[k].Location = Point.Add(new Point(mybtns[i].Location.X + 10,mybtns[i].Location.Y + 10),new Size(mybtns[i].Size.Width,mybtns[i].Size.Height));
}
}
}
foreach (Button b in mybtns)
{
b.Show();
}
return mybtns;
}
Play with this example...
public partial class Form1 : Form
{
private List<List<Button>> grid = new List<List<Button>>();
public Form1()
{
InitializeComponent();
byte numRows = 5;
byte numCols = 5;
for (byte i = 0; i < numRows; i++)
{
grid.Add(ButtonRowCreator(numCols, 25, (i+1) * 50));
}
}
public List<Button> ButtonRowCreator(byte numOfBtnsNeeded, int x, int y)
{
List<Button> btns = new List<Button>();
for (int i = 0; i < numOfBtnsNeeded; i++)
{
Button btn = new Button();
btn.Size = new Size(50, 50);
btn.Location = new Point(x + (i * btn.Width), y);
btns.Add(btn);
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
return btns;
}
void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Text = "X";
int curRow = -1, curCol = -1;
for(int i = 0; i < grid.Count; i++)
{
int index = grid[i].IndexOf(btn);
if (index != -1)
{
curRow = i;
curCol = index;
Console.WriteLine("curRow = " + curRow.ToString() + ", curCol = " + curCol.ToString());
}
}
// ... now you can use "curRow", "curCol" and "grid" to do something ...
// reset all BackColors:
foreach (List<Button> row in grid)
{
foreach (Button col in row)
{
col.BackColor = Button.DefaultBackColor;
}
}
// the below should give you some examples for the
// syntax necessary to access buttons in the grid
// highlight current row:
foreach (Button col in grid[curRow])
{
col.BackColor = Color.Yellow;
}
// highlight current col:
for (int i = 0; i < grid.Count; i++)
{
grid[i][curCol].BackColor = Color.Yellow;
}
}
}
You cannot change a foreach variable reference (ie b). If you want to initialize an array you should use for loop:
for(int i = 0; i < numOfBtnsNeeded; i++)
{
var button = mybtns[i] = new Button();
//Here you can modify the reference of button.
}
Also, mybtns will be full of nulls since Button is a reference type which means it's default value is a null.
you want something like:
public Button[] ButtonCreator(byte numOfBtnsNeeded)
{
Button[] mybtns = new Button[numOfBtnsNeeded];
for (int i = 0; i < mybtns.Length; i++)
{
mybtns[i] = new Button();
mybtns[i].Name = (i + 1).ToString();
}
return mybtns;
}
I'm not sure why you're using a byte over an int, but it works either way.
Essentially, when you create the array, you're not creating the objects within the array. And you cannot modify the thing you are looping over within a foreach loop, so you need a for loop.
i have a 10x10 text box ( 100 of them )
I write this code to write into text file :
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text)) // ignore this
{
textBox.Style["visibility"] = "hidden";
}
textBox.Enabled = false;
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
}
else
{
stringWriter.Write("," + textBox.Text );
numberofCommas++;
recordsWritten++;
}
if (recordsWritten == 10)
{
stringWriter.WriteLine();
recordsWritten = 0;
}
else
{
}
From the above i want to have 10 rows of 9 commas in the text file but instead i have 9 rows of 10 commas in the text file , is my code logic wrong? because i have been looking it for hours , i still couldn't solve it . sorry if my logic is bad , i am new to programming.
I think that you should increment recordsWritten in the last step:
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
recordsWritten++;
}
Here is a better way to do it using Linq:
var textBoxes = Panel1.Controls.OfType<TextBox>().Select((t, i) => new { TextBox = t, Index = i }).ToList();
foreach (var tb in textBoxes)
{
if (string.IsNullOrEmpty(tb.TextBox.Text))
tb.TextBox.Style["visibility"] = "hidden";
tb.TextBox.Enabled = false;
}
foreach (var line in textBoxes.GroupBy(e => e.Index / 10)
.Select(e =>
string.Join(", ",
e.Select(a => a.TextBox.Text).ToArray())))
stringWriter.WriteLine(line);
I wouldn't recommend you to use 100 TextBox objects, you can use a DataGridView binded to a DataTable with 10 rows and 10 columns. You can still edit your data and save it to a file.
Try the below code
StringWriter stringWriter1 = new StringWriter();
DataTable dataTable1 = new DataTable();
private void Form1_Shown(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
int i;
for (i = 0; i < 10; i++)
{
dataTable1.Columns.Add("Column" + (i + 1), typeof(string));
}
for (i = 0; i < 10; i++)
{
DataRow dataRow1 = dataTable1.NewRow();
dataTable1.Rows.Add(dataRow1);
}
dataGridView1.DataSource = dataTable1;
}
private void button1_Click(object sender, EventArgs e)
{
string rowString = "";
int i,j;
for (i = 0; i < 10; i++)
{
rowString = "";
for (j = 0; j < 10; j++)
{
if (dataTable1.Rows[i][j].ToString().Contains(",") == true)
{
//Enclosing the field data inside quotes so that it can
//be identified as a single entity.
rowString += "\"" + dataTable1.Rows[i][j] + "\"" + ",";
}
else
{
rowString += dataTable1.Rows[i][j] + ",";
}
}
rowString = rowString.Substring(0, rowString.Length - 1);
stringWriter1.WriteLine(rowString);
}
}
you just need to add a DataGridView onto your Form.