I want to make an error label come up when my flowLayoutPanel is empty, but i don't know how to check that the flowLayoutPanel is empty. This is my current code:
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls == null)
{
customtoolwarning.Visible = true;
}
else
{
customtoolwarning.Visible = false;
}
}
Please Help,
Thanks
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls.Count > 0)
{
customtoolwarning.Visible = true;
}
else
{
customtoolwarning.Visible = false;
}
}
The problem you're running into is you're checking Controls for null to determine if it's empty. The Controls property won't ever be null but instead will be non-null and have 0 length when empty. For example
if (flowLayoutPanel1.Controls.Count == 0) {
// It's empty
}
lblNoContacts.Visible = (flowLayoutPanel.Controls.Count == 0) ? true : false;
Related
How do I show a messagebox based on the various SelectedText in the Combobox? It currently just returns a NULL value when running.
I need to show the specific messagebox for each Combobox Text as once I can do this then depending on the SelectedText different SQL Connections will be used and Queries run.
I've included my code below. After some research it seems that the SelectedText control will always return a null value as it loses focus. How do I get around this?
private void button2_Click(object sender, EventArgs e)
{
if(comboSelectServer.SelectedText == "SERV1")
{
MessageBox.Show("SERV1");
}
else if(comboSelectServer.SelectedText == "SERV2")
{
MessageBox.Show("SERV2");
}
else if(comboSelectServer.SelectedText == "SERV3")
{
MessageBox.Show("SERV3");
}
}
Try this.
if (comboSelectServer.Text == "SERV1")
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.Text == "SERV2")
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.Text == "SERV3")
{
MessageBox.Show("SERV3");
}
However, this is easier...
if (comboSelectServer.SelectedIndex == 0) //SERV1
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.SelectedIndex == 1) //SERV2
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.SelectedIndex == 2) //SERV3
{
MessageBox.Show("SERV3");
}
Try like this
private void button2_Click(object sender, EventArgs e)
{
if(comboSelectServer.SelectedItem.ToString()== "SERV1")
{
MessageBox.Show("SERV1");
}
else if(comboSelectServer.SelectedItem.ToString()== "SERV2")
{
MessageBox.Show("SERV2");
}
else if(comboSelectServer.SelectedItem.ToString()== "SERV3")
{
MessageBox.Show("SERV3");
}
}
Maybe I'm missing something, but why not simply do:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(comboSelectServer.SelectedItem.ToString());
}
I added Checkboxcolumn near my columns.
i googling a lot but i cant find a good answer.
I Want to make Btn_Edit.Visiable=false; if checkbox checked count is up to 1.
after my researches i go to Use CellContentClick event but it isn't work fine.
My Code:
private void GrdVw_Reception_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
if (GrdVw_Reception.CurrentCellAddress.X == 0)
{
int UpTpOne = 0;
bool flag = false;
for (int i = 0; i < GrdVw_Reception.Rows.Count; i++)
{
if (Convert.ToBoolean(GrdVw_Reception.Rows[i].Cells["checkColumn"].Value) == true)
{
UpTpOne = UpTpOne + 1;
if (UpTpOne == 1)
{
flag = true;
}
else
{
flag = false;
}
}
}
if (flag == true)
{
Btn_Edit.Visible = true;
}
else
{
Btn_Edit.Visible = false;
}
}
}
In this step when i run the program and click on checkboxes for first Btn_Edit dont go to true and for secound click Btn_Edit.Visiable go to true.
I want checkedchange event or an event that when i click on checkbox go to event codes in that moment.
(Sorry for bad English)
I proved it myself and had to do this to work:
private void GrdVw_Reception_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == TheColumnIndexOfTheCheckBoxColumn){
bool currentValue = !(bool)GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value;
GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value = currentValue;
GrdVw_Reception.EndEdit();
MessageBox.Show("Value " + currentValue.ToString());
}
}
When the even click happens the value haven't change, so, i changed it myself and call EndEdit() method of the dataGridView. It works that way.
private void GrdVw_Reception_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
if (GrdVw_Reception.CurrentCellAddress.X == 0)
{
if (Convert.ToBoolean(GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value) == false)
{
GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value = CheckState.Checked;
}
else
{
GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value = CheckState.Unchecked;
}
int UpTpOne = 0;
bool flag = false;
for (int i = 0; i < GrdVw_Reception.Rows.Count; i++)
{
if (Convert.ToBoolean(GrdVw_Reception.Rows[i].Cells["checkColumn"].Value) == true)
{
UpTpOne++;
if (UpTpOne == 1)
{
flag = true;
}
else
{
flag = false;
}
}
}
if (UpTpOne == 0)
{
Btn_DelRecord.Visible = false;
}
else
{
Btn_DelRecord.Visible = true;
}
if (flag == true)
{
Btn_Edit.Visible = true;
}
else
{
Btn_Edit.Visible = false;
}
}
}
In this code i made value of checkbox to true by code and then counting number of checkboxescell that has been checked.
I wanted make visiable of Btn_Edit go to false if the checked boxes number is up to one and.....
You should make checkbox tick to ticked and value of cell to true by code because when you click on gridviewcellcheckbox for first, the row go to selecting and in secound click the value going to be true.
I googled a lot but can't reach any good answer and this code is a total of my thinking if you have any good idea for do it better please call me.
I try to put the selectedIndex of a listbox at the top of the displayed list with this code :
private void textBox1_TextChanged(object sender, EventArgs e)
{
sourceListBox.SelectionMode = SelectionMode.One;
if (textBox1.Text != string.Empty)
{
int index = sourceListBox.FindString(textBox1.Text);
if (index != -1 && sourceListBox.SelectedIndex != index)
{
sourceListBox.ClearSelected();
sourceListBox.SetSelected(index, true);
sourceListBox.TopIndex = sourceListBox.SelectedIndex;
}
}
else
{
sourceListBox.ClearSelected();
}
sourceListBox.SelectionMode = SelectionMode.MultiExtended;
}
But the selected index is stuck at the bottom of the listbox :
And this is the only part of code that change the behavior of the listbox. How can I fix that ?
It looks like the call to sourceListBox.SelectionMode = SelectionMode.MultiExtended; is resetting the TopIndex. Setting the TopIndex after that call will work:
private void textBox1_TextChanged(object sender, EventArgs e)
{
int topIndex = sourceListBox.TopIndex;
sourceListBox.SelectionMode = SelectionMode.One;
if (textBox1.Text != string.Empty)
{
int index = sourceListBox.FindString(textBox1.Text);
if (index != -1 && sourceListBox.SelectedIndex != index)
{
sourceListBox.ClearSelected();
sourceListBox.SetSelected(index, true);
topIndex = sourceListBox.SelectedIndex;
}
}
else
{
sourceListBox.ClearSelected();
}
sourceListBox.SelectionMode = SelectionMode.MultiExtended;
sourceListBox.TopIndex = topIndex;
}
Remove the check against the surrent SelectedIndex and your code that sets the TopIndex will execute always, also if your current SelectedIndex is equal to the result of the FindString
private void textBox1_TextChanged(object sender, EventArgs e)
{
.....
int index = sourceListBox.FindString(textBox1.Text);
if (index != -1)
{
sourceListBox.ClearSelected();
sourceListBox.SetSelected(index, true);
sourceListBox.TopIndex = sourceListBox.SelectedIndex;
}
....
}
Change
sourceListBox.TopIndex = sourceListBox.SelectedIndex;
to
sourceListBox.TopIndex = index;
to a winforms listbox
SendMessage(listBox.Handle,LB_SETTOPINDEX, 0, 0);
will make the topmost item visible, guaranteed, WITHOUT
changing its selection state, which can be a nice touch when just starting an app and deserializing to load a listbox.
I am just a student begining to study C#, so I apologize if my questions are not very clear. I am stuck for the answer. I don't know how to code the ArgumentOutofRangeException, so the user doesn't go beyond the edges of the Lists. I have 2 of them, with two index variables. I also have a problem with updateControls. Any help would be greatly appreciated.
private void updateControls()
{
pictureBox1.Image = resList[currentResIndex].Photo;
lblName.Text = resList[currentResIndex].Title;
lblCity.Text = resList[currentResIndex].City;
lblPrice.Text = resList[currentResIndex].Price.ToString("C");
pictureBox1.Image = comList[currentCommIndex].Photo;
lblName.Text = comList[currentCommIndex].Title;
lblCity.Text = comList[currentCommIndex].City;
lblPrice.Text = comList[currentCommIndex].Price.ToString("C");
}
private void btnNext_Click(object sender, EventArgs e)
{
if (cboType.SelectedItem.ToString() == "Residential") //if they chose residential then increment resIndex
currentResIndex++;
updateControls();
else
currentCommIndex++;//or else commIndex
updateControls();
}
if (cboType.SelectedItem.ToString() == "Residential"
&& currentResIndex < resList.Count -1) // add this condition
currentResIndex++;
updateControls();
You could just enforce the range in your btnNext_Click method:
private void btnNext_Click(object sender, EventArgs e)
{
if (cboType.SelectedItem.ToString() == "Residential")//if they chose residential then increment resIndex
currentResIndex++;
else
currentCommIndex++;//or else commIndex
currentCommIndex = Math.Min(comList.Length-1, currentCommIndex);
currentResIndex = Math.Min(resList.Length-1, currentResIndex);
updateControls();
}
try
{
if (cboType.SelectedItem.ToString() == "Residential") //if they chose residential then increment resIndex
{
currentResIndex++;
}
else
{
currentCommIndex++;//or else commIndex
}
updateControls();
}
catch (ArgumentOutOfRangeException ex)
{
//Do the things you want when this exception occurs
}
But imho you shouldn't be using this. You should check whether or not the end of the List is reached.
I'm trying to understand what's happening here. I have a CheckedListBox which contains some ticked and some un-ticked items. I'm trying to find a way of determining the delta in the selection of controls. I've tried some cumbersome like this - but only works part of the time, I'm sure there's a more elegant solution. A maybe related problem is the myCheckBox_ItemCheck event fires on form load - before I have a chance to perform an ItemCheck. Here's what I have so far:
void clbProgs_ItemCheck(object sender, ItemCheckEventArgs e)
{
// i know its awful
System.Windows.Forms.CheckedListBox cb = (System.Windows.Forms.CheckedListBox)sender;
string sCurrent = e.CurrentValue.ToString();
int sIndex = e.Index;
AbstractLink lk = (AbstractLink)cb.Items[sIndex];
List<ILink> _links = clbProgs.DataSource as List<ILink>;
foreach (AbstractLink lkCurrent in _links)
{
if (!lkCurrent.IsActive)
{
if (!_groupValues.ContainsKey(lkCurrent.Linkid))
{
_groupValues.Add(lkCurrent.Linkid, lkCurrent);
}
}
}
if (_groupValues.ContainsKey(lk.Linkid))
{
AbstractLink lkDirty = (AbstractLink)lk.Clone();
CheckState newValue = (CheckState)e.NewValue;
if (newValue == CheckState.Checked)
{
lkDirty.IsActive = true;
}
else if (newValue == CheckState.Unchecked)
{
lkDirty.IsActive = false;
}
if (_dirtyGroups.ContainsKey(lk.Linkid))
{
_dirtyGroups[lk.Linkid] = lkDirty;
}
else
{
CheckState oldValue = (CheckState)e.NewValue;
if (oldValue == CheckState.Checked)
{
lkDirty.IsActive = true;
}
else if (oldValue == CheckState.Unchecked)
{
lkDirty.IsActive = false;
}
_dirtyGroups.Add(lk.Linkid, lk);
}
}
else
{
if (!lk.IsActive)
{
_dirtyGroups.Add(lk.Linkid, lk);
}
else
{
_groupValues.Add(lk.Linkid, lk);
}
}
}
Then onclick of a save button - I check whats changed before sending to database:
private void btSave_Click(object sender, EventArgs e)
{
List<AbstractLink> originalList = new List<AbstractLink>(_groupValues.Values);
List<AbstractLink> changedList = new List<AbstractLink>(_dirtyGroups.Values);
IEnumerable<AbstractLink> dupes = originalList.ToArray<AbstractLink>().Intersect(changedList.ToArray<AbstractLink>());
foreach (ILink t in dupes)
{
MessageBox.Show("Changed");
}
if (dupes.Count() == 0)
{
MessageBox.Show("No Change");
}
}
For further info. The definition of type AbstractLink uses:
public bool Equals(ILink other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(this, other)) return true;
return IsActive.Equals(other.IsActive) && Linkid.Equals(other.Linkid);
}
There's little point that I see to do this in the ItemCheck event. Just calculate the delta when you save. Cuts out a bunch of code and trouble with spurious events.