I can't disable checkbox...
public void seat_reser_Load(object sender, EventArgs e)
{
string asd = "";
DataTable dt = ob.dataview("Select * from seat_res where bus_id='"+ bus_select.Id+"'and date='"+bus_select.date +"'");
foreach (DataRow d in dt.Rows)
{
asd += d[2].ToString(); // d[2] is seat column
asd+=",";
}
box[0] = CheckBox1;
box[1] = CheckBox2;
box[2] = checkBox3;
box[3] = checkBox4;
box[4] = checkBox5;
box[5] = checkBox6;
box[6] = checkBox7;
box[7] = checkBox8;
box[8] = checkBox9;
box[9] = checkBox10;
box[10] = checkBox11;
box[11] = checkBox12;
box[12] = checkBox13;
box[13] = checkBox14;
box[14] = checkBox15;
box[15] = checkBox16;
box[16] = checkBox17;
box[17] = checkBox18;
box[18] = checkBox19;
box[19] = checkBox20;
box[20] = checkBox21;
box[21] = checkBox22;
box[22] = checkBox23;
box[23] = checkBox24;
box[24] = checkBox25;
for (int h = 0; h < box.Length; h++)
{
box[h].Enabled = true;
}
string[] n = asd.Split(',');
for (int i = 0; i < n.Length; i++)
{
for (int j = 0; j < box.Length; j++)
{
if (n[i] == box[j].Text)
{
box[j].Enabled = false;
j++;
}
else if (!(box[j].Enabled == false))
{
box[j].Enabled = true;
}
}
}
}
Your code seems fine, did you want to set the checkbox's visibility or checked state to false, instead of disabling it? Did you want to make it invisible or simply unchecked?
In that case you might want to change your usage of Enabled to Visible or Checked, for example, like this:
box[h].Visible = false;
box[h].Checked = false;
What's the difference between Enabled, Checked and Visible?
Enabled - this will enable/disable the checkbox, you cannot check or uncheck the checkbox if it is disabled, however, the checkbox will still be visible on screen
Checked - this will check/uncheck the checkbox, it's identical to user actually clicking the checkbox
Visible - this will make the checkbox visible/invisible, so that user cannot see the control, nor can he click on it or interact with it in any way
Related
hello experts help please, i have two form in my project and one of those form1 have dataGridView and i want to update the gridview according to text value from form 2,
i had done updating dgv but all row updated on button click event
ex. i have 4 rows different description and cell value after click event
all those 4 rows updating to 1 value.
Form 1
dgv cellmouseclick event
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView2.Rows[e.RowIndex];
_choosenPart = row.Cells[0].Value.ToString();
_choosenQty = row.Cells[2].Value.ToString();
_choosenPrice = row.Cells[4].Value.ToString();
_choosenAmount = row.Cells[5].Value.ToString();
_choosenTotal = row.Cells[7].Value.ToString();
}
FrM_Edit EditGV = new FrM_Edit(this);
EditGV.Show();
In form 2
//constructor
Form F2_main = null;
public FrM_Edit(Form DgVForm1)
{
F2_main=DgVForm1;
InitializeComponent();
}
button click event
r2main_choosenPart = textBox1.Text;
r2main_choosenQty = textBox2.Text;
r2main_choosenPrice = textBox3.Text;
r2main_choosenAmount = textBox4.Text;
r2main_choosenTotal = textBox5.Text;
DataGridView Main_dg = (DataGridView)F2_main.Controls["dataGridView2"];
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
}
this.Close();
//this for loop takes all rows of your DataGridView. And it is normal that you see that all rows are changing because for all rows, you apply the same code.
DataGridView Main_dg = (DataGridView)F2_main.Controls["dataGridView2"];
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
}
Instead, you should update only the row that you want to updaate.
You can use differents methods.
For exemple:
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
if( Main_dg.Rows[i].Cells[0].Value == "valueToChange") //Checking an ID or other value.
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
break; // To break after finding the row that you are looking for.
}
}
Or
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
if(i == 2) //to update the third row of the grid.
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
break; // To break after finding the row that you are looking for.
}
}
Or you can use your DataGridViews selected row to update.
I have a property grid and a combo box, Based on the value of the combo box i change the propertygrid.SelectedItem and refresh the property grid.
The problem is that whenever i select a value to edit the whole screen starts flickering like it's repainting or redrawing. This only happens when i select a property on the property grid, it goes away when i go to another screen and comeback. I have not written any redraw functions on click events.
[edit1]I've added a code snippet of combo box selected item change
if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "AppSettings")
{
this.comboBoxHomerSublist.Visible = false;
this.propertyGridEditSettings.SelectedObject = Settings.SingleInstance.SettingsValues.AppSettings;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "WaferSpecificSettings")
{
this.comboBoxHomerSublist.Visible = true;
this.comboBoxHomerSublist.Items.Clear();
for (int i = 0; i < Settings.SingleInstance.SettingsValues.WaferSpeicificSettingsList.WaferSpeicificInformationList.Count;i++ )
{
this.comboBoxHomerSublist.Items.Add(Settings.SingleInstance.SettingsValues.WaferSpeicificSettingsList.WaferSpeicificInformationList.ElementAt(i));
}
this.comboBoxHomerSublist.SelectedIndex = 0;
this.propertyGridEditSettings.SelectedObject = this.comboBoxHomerSublist.SelectedItem;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "BinColorMapping")
{
this.comboBoxHomerSublist.Visible = true;
this.comboBoxHomerSublist.Items.Clear();
for (int i = 0; i < Settings.SingleInstance.SettingsValues.BinColorInfoList.BinColorInformationList.Count; i++)
{
this.comboBoxHomerSublist.Items.Add(Settings.SingleInstance.SettingsValues.BinColorInfoList.BinColorInformationList.ElementAt(i));
}
this.comboBoxHomerSublist.SelectedIndex = 0;
this.propertyGridEditSettings.SelectedObject = this.comboBoxHomerSublist.SelectedItem;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "CommonSettings")
{
this.comboBoxHomerSublist.Visible = false;
this.propertyGridEditSettings.SelectedObject = Settings.SingleInstance.SettingsValues.CommonSettings;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "DOETypeMapping")
{
this.comboBoxHomerSublist.Visible = true;
this.comboBoxHomerSublist.Items.Clear();
for (int i = 0; i < Settings.SingleInstance.SettingsValues.DOETypeMapingInfoList.DOETypeMapingInformationList.Count; i++)
{
this.comboBoxHomerSublist.Items.Add(Settings.SingleInstance.SettingsValues.DOETypeMapingInfoList.DOETypeMapingInformationList.ElementAt(i));
}
this.comboBoxHomerSublist.SelectedIndex = 0;
this.propertyGridEditSettings.SelectedObject = this.comboBoxHomerSublist.SelectedItem;
}
this.propertyGridEditSettings.Refresh();
I have a form which changes Title depends on users selection. Then i want to create a properties for the control, either radiobutton or checkbox. They have same properties but i cant implement well.
private void DeleteEdit()
{
Control[] EmployeesControl; //I think this is the problem, but i cant figure it out.
if (Form_AddEmployee.Text.Contains("Edit"))
{
EmployeesControl = new RadioButton[numberOfEmployees];
}
else if (Form_AddEmployee.Text.Contains("Delete"))
{
EmployeesControl = new CheckBox[numberOfEmployees];
}
for (int i = 0; i < EmployeesControl.Count(); i++)
{
EmployeesControl[i] = new EmployeesControl();
InitializeControls(EmployeesControl[i]);
EmployeesControl[i].Visible = true;
panelEmployee.Controls.Add(EmployeesControl[i]);
EmployeesControl[i].Text = stringTemp;
EmployeesControl[i].Location = new Point(100, 100 * (i+1));
EmployeesControl[i].Font = MyFont;
EmployeesControl[i].CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;
}
}
How can i make a variable if a certain control is a radiobutton or checkbox.
You can use GetType to check, after you will change EmployeesControl[i] to CheckBox or RadioButton.
private void DeleteEdit()
{
Control[] EmployeesControl; //I think this is the problem, but i cant figure it out.
if (Form_AddEmployee.Text.Contains("Edit"))
{
EmployeesControl = new RadioButton[numberOfEmployees];
for (int i = 0; i < EmployeesControl.Count(); i++)
{
EmployeesControl[i] = new RadioButton();
}
}
else if (Form_AddEmployee.Text.Contains("Delete"))
{
EmployeesControl = new CheckBox[numberOfEmployees];
for (int i = 0; i < EmployeesControl.Count(); i++)
{
EmployeesControl[i] = new CheckBox();
}
}
ButtonBase b;
CheckBox chk;
RadioButton rdo;
for (int i = 0; i < EmployeesControl.Count(); i++)
{
b = (ButtonBase)EmployeesControl[i];//You use b to set property
if (EmployeesControl[i].GetType() == typeof(RadioButton))
{
rdo = (RadioButton)EmployeesControl[i];
//Your code
//................
rdo.CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;
}
else
{
chk = (RadioButton)EmployeesControl[i];
//Your code
//...............
chk.CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;
}
//EmployeesControl[i] = new EmployeesControl();
//InitializeControls(EmployeesControl[i]);
//EmployeesControl[i].Visible = true;
//panelEmployee.Controls.Add(EmployeesControl[i]);
//EmployeesControl[i].Text = stringTemp;
//EmployeesControl[i].Location = new Point(100, 100 * (i + 1));
//EmployeesControl[i].Font = MyFont;
//EmployeesControl[i].CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;
}
}
I hope it will help you.
You can use ButtonBase class to your perpouse.
Declare array of controls by ButtonBase, so common properties of CheckBox and RadioButton you can use without casting.
Properties of CheckBox or RadioButton you can access by
var a = EmployeesControl[i] as CheckBox;
if (a != null)
{
//this is checkbox
continue;
}
var b = EmployeesControl[i] as RadioButton;
if (b != null)
{
//this is RadioButton
}
I am trying to populate TextBoxes from a list. I have been able to populate ComboBoxes with comboList:
var comboList = new System.Windows.Forms.ComboBox[4];
comboList[0] = cmbSite1Asset;
comboList[1] = cmbSite2Asset;
comboList[2] = cmbSite3Asset;
comboList[3] = cmbSite4Asset;
List<CRCS.CAsset> assets = _rcs.Assets;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int i = 0; i < 4; ++i)
{
comboList[i].Items.Add(id);
}
}
But when I try and apply the same principle to TextBoxes
var aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int n = 0; n < 8; ++n)
{
aosList[n].Items.Add(id);
}
}
TextBox does not like Items.Add ( aosList[n]Items.Add(id); )
I am looking fore a reference or guidance resolving this issue. Thanks!
You should use ComboBox for your problem,instead of iterating on each element,You simply use below lines to populate combobox.
comboList.DataSource=assets;
comboList.DisplayMember="ID";
comboList.ValueMember="ID";
However,if you want your values in TextBox,you can use TextBox.AppendText Method, but it will not work like ComboBox as it will contain texts+texts+texts, will not have indexes like ComboBox.
private void AppendTextBoxLine(string myStr)
{
if (textBox1.Text.Length > 0)
{
textBox1.AppendText(Environment.NewLine);
}
textBox1.AppendText(myStr);
}
private void TestMethod()
{
for (int i = 0; i < 2; i++)
{
AppendTextBoxLine("Some text");
}
}
A Combobox is a collection of items, and so has an Items property from which you can add/remove to change it's contents. A Textbox is just a control that displays some text value, so it has a Text property which you can set/get, and which denotes the string that is displayed.
System.Windows.Forms.TextBox[] aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
for (int n = 0; n < 8; ++n)
{
aosList[n].Text = assets[n].ID; // make sure you have 8 assets also!
}
int i = 1;
foreach (var asset in assets)
{
this.Controls["txtAsset" + i].Text = asset.ID;
i++;
}
How do I do this in a loop.
protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e)
{
lblTool1.Visible = false;
txtTool1.Visible = false;
lblTool2.Visible = false;
txtTool2.Visible = false;
lblTool3.Visible = false;
txtTool3.Visible = false;
lblTool4.Visible = false;
txtTool4.Visible = false;
lblTool5.Visible = false;
if (ddlTool.SelectedValue == "1")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
}
if (ddlTool.SelectedValue == "2")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
lblTool2.Visible = true;
txtTool2.Visible = true;
}
if (ddlTool.SelectedValue == "3")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
lblTool2.Visible = true;
txtTool2.Visible = true;
lblTool3.Visible = true;
txtTool3.Visible = true;
}
if (ddlTool.SelectedValue == "4")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
lblTool2.Visible = true;
txtTool2.Visible = true;
lblTool3.Visible = true;
txtTool3.Visible = true;
lblTool4.Visible = true;
txtTool4.Visible = true;
}
Instead of having a separate variable for each textbox and label, have a collection of them - whether that's a List<T> or an array or whatever.
Then you can do:
// Potentially use int.TryParse here instead
int visibleLabels = int.Parse(ddlTool.SelectedValue);
for (int i = 0; i < labels.Count; i++)
{
labels[i].Visible = (i < visibleLabels);
textBoxes[i].Visible = (i < visibleLabels);
}
(Alternatively use two loops, one to set some Visible properties to true, and one to set some to false.)
You can access a control by its name using
container.Controls["nameofcontrol"]
So technically, you could use this to lookup your control
(Untested code)
for(int index = 1; index <= Convert.ToInt32(ddlTool.SelectedValue); index++)
{
this.Controls["lblTool" + index.ToString()].Visible = true;
this.Controls["txtTool" + index.ToString()].Visible = true;
}
Use a UserControl for each set of connected controls and then enable/disable the UserControl instead of all the component controls. This is classic, basic modularization of your user interface.
Note that this will still require a little "redundant" code because you're working with an unusual UI paradigm by enabling up-to the ddlTool's selected value of your control. E.g., create your user control that contains a single Label and TextBox. Call it LabeledTextBox or something similar. Then you'd create a collection of your labeled text boxes and enable them up to int.Parse(ddlTool.SelectedValue) - 1.
foreach (Control ctrl in this.Controls)
{
int index = (int)ctrl.Name.Substring(ctrl.Name.Length - 1);
int maxIndex = (int)ddlTool.SelectedValue;
ctrl.Visible = (index <= maxIndex);
}
Here is an example with no error checking, but should match your code functionality.
protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedValue = int.Parse(ddlTool.SelectedValue.ToString());
for (int i = 1; i <= 4; ++i)
{
bool state = i <= selectedValue;
this.Controls["lblTool" + i.ToString()].Visible = state;
this.Controls["txtTool" + i.ToString()].Visible = state;
}
}