I want to bind the ComboBox value upto the TextBox value. Here is an example:
Let's say I have one TextBox and one ComboBox
Now in TextBox there is value of 5. So as per the TextBox value is 5 my ComboBox would be bind upto 5 and in the list of ComboBox it would be saw the number upto 5 i.e. 1,2,3,4,5.
Same if the TextBox contain the value 3 then as per the change of TextBox value on the spot ComboBox should be bind upto the TextBox value.
I am working on it also but there is some list error.
Here is my code:
List<string> hafta = new List<string>();
hafta.Add(txt_hafta.Text);
for (int i = 0; i <= hafta.Count; i++)
{
cmb_hafta.BindingContext = this.BindingContext;
cmb_hafta.DataSource = hafta[i];
cmb_hafta.DisplayMember = i.ToString();
}
I am not sure that this code is perfect.
On TextBox text changed:
int count = 0;
Int32.TryParse(txt_hafta.Text, out count);
List<int> dataSource = new List<int>();
for (int i = 1; i <= count; i++)
{
dataSource.Add(i);
}
hafta.DataSource = dataSource;
hafta.DropDownStyle = ComboBoxStyle.DropDownList
If you talk about binding, then you can bind ComboBox.DataSource property to TextBox.Text property with custom eventhandler for Binding.Format event, where you can "convert" string to collection of numbers.
Put code below in the constructor of your form.
var binding =
new Binding("DataSource", txt_hafta, "Text", true, DataSourceUpdateMode.Never);
binding.Format += (sender, args) =>
{
int.TryParse(args.Value.ToString(), out int maxNumber);
args.Value = Enumerable.Range(1, maxNumber).ToList();
};
cmb_hafta.DataBindings.Add(binding);
You can do something like this:
List<string> hafta = new List<string>();
int total = Int32.Parse(txt_hafta.Text);
for (int i = 0; i <= total ; i++)
{
hafta.Add(i.ToString());
}
cmb_hafta.DataSource = hafta;
Related
I got the following:
List<TextBox[]> ListMonths = new List<TextBox[]>();
I use it for store the same textboxes for each month, I fill it like this
for (int i = 0; i <= 11; i++)
{
......
TextBox[] TBaux = new TextBox[18];
for (int o = 0; o <= 17; o++)
{
TBaux[o] = (TextBox)element.FindName("TB" + o + i);
}
ListMonths.Add(TBaux);
}
So that way I got the textboxes for each month in ListMonths.
How can I modify the Text property of one of the textbox (for instance textbox[2]) that is stored in one of the month lists (for instance ListMonths[1])?
ListMonths[1][2].Text = "blabla";
Which is the same as doing:
TextBox[] textBoxes = ListMonths[1];
TextBox textBox = textBoxes[2];
textBox.Text = "blabla";
I have a Combobox, in which let's say that an item's Display Text is "School" and it's Item Value is 19. So i have stored this 19 into a DataGrid.
Then, i retrieve Combobox Value from DataGrid, then what i want to do is simply that based on value retrieved from DataGrid, combobox should set it's display Item or SelectedItem which have Value 19. In above scenario Combobox should display its selected item "School" if its value was 19.
So far i have wrote code upto this point. But it always giving me First Item of a Combobx.
DataGrid gd = (DataGrid)sender;
DataRowView rowSelected = gd.SelectedItem as DataRowView;
if(rowSelected!=null)
{
for (int i = 0; i < comboBox1.Items.Count;i++ )
{
if (Convert.ToString(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = comboBox1.Items.IndexOf(comboBox1.SelectedValue);
}
comboBox1.SelectedItem= comboBox1.Items[index];
}
textBox9.Text=rowSelected[14].ToString();
}
Now i am able to retrieve the Combobox Item, Based on its value which i am retrieving from the WPF DataGrid.
for (int i = 0;i <comboBox1.Items.Count; i++)
{
comboBox1.SelectedIndex = i;
if ((string)(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = i;
}
}
comboBox1.SelectedIndex = index;
Change your code to
if(rowSelected!=null)
{
int index = comboBox1.Items.IndexOf(rowSelected[14]);
comboBox1.SelectedItem = comboBox1.Items[index];
}
or
Use FindStringExact() method of combobox
int i = comboBox1.FindStringExact("Combo");
if(i >= 0)
{
}
I have for example this values in datagridView : 1;2;3;4;
I want display this values in combobox like this : 1
2
3
4
My code show only last value in combobox: 4
My code of showing :
string cmbValue = CmbText;
string[] cmb = cmbValue.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cmb.Length; i++)
{
comboBox1.Text = cmb[i];
}
and here is my code of creating array of combobox and calling method to change cell in dataGrid:
string cmbText = comboBox1.Text;
string[] cmb = new string[] { cmbText};
frm1.ChangeCellCmb(2, cmb);
this.Dispose();
Someone know how to do this ? I cant simply set collection of values combobox, because values of combobox are reading from datagrid and it's reading from DB.
Many thanks.
You need to loop and add the required items as follows:
for (int i = 0; i < cmb.Length; i++)
comboBox1.Items.Add(cmb[i]);
This will add all the required items to the drop down menu. To select/display '4' the 3rd entry in cmb by default do
comboBox1.SelectedIndex = 3;
or
comboBox1.SelectedItem = "4";
I hope this helps.
I am doing a crossword puzzle and i have 100 text boxes in a panel like this :
Every text box have an id of 00 - 99 since there is 100 of it .
First Row will have an id 00-09 , 2nd row will have an id of 10-19 and so on.
When user types something in some text box will be null and some text box will have values in it. How do I save values from a text box of a certain id to a database? For example the above image, HELP, text box id of 22 will have the value H , id of 23 will have the value of E , id of 24 will have value of L , id of 25 will have value of P.
I don't want to save the null values of the text box , I want to save values of the textboxes which are not null. I also need to take into account their textbox ids so that when I populate them back, I just have to insert them through ID .
I am new to C# , appreciate any help/advise/solutions on this.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
//hw.Write("<table>");
for (int i = 0; i <= 9; i++)
{
//hw.Write("<tr>");
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
tb.MaxLength = (1);
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
tb.ID = i.ToString() + j.ToString(); // giving each textbox a different id 00-99
Panel1.Controls.Add(tb);
}
Literal lc = new Literal();
lc.Text = "<br />";
Panel1.Controls.Add(lc);
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
// textBox.Enabled = false;
textBox.Text = "";
}
}
}
The proper way to do this is to wrap these textboxes inside a Repeater or Datalist controls. You can ready about these controls from here. This way when number of rows increase you will not have to change to your loop or hard-coded values.
As for your question to store values for a given Id, you can define row# and col# in your database and sort on row# and col#, this should work.
The easiest way is to make a 2D array (or List) of your TextBoxes. In where you create your TextBoxes:
List<List<TextBox>> textBoxList = new List<List<TextBox>>();
for (int i = 0; i <= 9; i++)
{
List<TextBox> textBoxRow = new List<TextBox>(); //this could be columns, not sure
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
....
textBoxRow.Add(tb);
}
...
textBoxList.Add(textBoxRow);
}
Now you can read/write to those array entries, such as:
string readValue = textBoxList[2][5].Text;
textBoxList[1][7].Text = "asdf";
HI I have requirement that
1) display given no.of textboxes dynamically and save to DB
2) if I change the number then new textboxes should append to UI
EX: TextBox AddButton
If I give 2 in textbox and click on add
Then 2 textboxes should appear. I filled some data in those textboxes. Now When I change the value 2 to 5 then 3 more textboxes should append(condition:old textboxes data should retain)
If the second value is less than or equal to first value then do nothing.
My code is
void Append()
{
string Data = string.Empty;
TextBox tb;
if (Convert.ToInt32(hdnCnt.Value) < Convert.ToInt32(txtNoofGames.Text))
{
for (int i = 0; i < Convert.ToInt16(txtNoofGames.Text); i++)
{
if (i <= Convert.ToInt32(hdnCnt.Value))
{
tb = (TextBox)Form.FindControl("txtGame1");
Data = tb.Text;
}
TextBox Newtb = new TextBox();
Newtb.ID = "txtGame" + i;
Form.Controls.Add(Newtb);
if (i <= Convert.ToInt32(hdnCnt.Value))
{
Newtb.Text = Data;
}
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (hdnCnt.Value != "")
Append();
hdnCnt.Value = txtNoofGames.Text;
for (int i = 0; i < Convert.ToInt16(txtNoofGames.Text); i++)
{
TextBox tb = new TextBox();
tb.ID = "txtGame" + i;
Form.Controls.Add(tb);
}
}
I am getting exception "object reference not set to an instance of object" at Data = tb.Text; in append method.
You didn't initialize it from the looks of it
TextBox tb = new TextBox();
Hope that helps,
Instead of Form.Controls.Add(tb);
Please try with Page.Form.Controls.Add(tb);