can anybody say me, how can I clear content inside RichTextBox using one button where: CheckedListBox - select which of rtb will be cleared?
I have problem with CheckedListBox - It works for one select position, but not for checked/marked.
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
string str = (string)checkedListBox1.Items[i];
if(str == "rtb1")
{
richTextBox1.Clear();
richTextBox1.Focus();
}
if(str == "rtb2")
{
richTextBox2.Clear();
richTextBox2.Focus();
}
}
}
Related
So i need to dynamically add and delete some TabPage. User clicked "Show Tab" = storage_pageadded.
private void storage_menuItem_Click(object sender, EventArgs e) {
storage_page.Text = storage_page.Name = "Storage";
main_tabControl.TabPages.Add(storage_page);
main_tabControl.SelectedTab = storage_page;
}
And the when he chooses another page the storage_page has to be removed
private void main_tabControl_SelectedIndexChanged(object sender, EventArgs e) {
for (int i = 0; i < main_tabControl.TabPages.Count; i++) {
if (main_tabControl.TabPages[i].Name.Equals("storage", StringComparison.OrdinalIgnoreCase) && main_tabControl.SelectedTab.Name != "Storage") {
main_tabControl.TabPages.RemoveAt(i);
break;
}
}
}
When i click "Show Tab" page shows up. But when i select other page i see the ArgumentOutOfRangeException leading to Application.Run(new Form_Authentication()); line
How can i do that?
I think the problem is you increase i variable by 1 from 0 to main_tabControl.TabPages.Count
I assume that main_tabControl.TabPages.Count = 10, what is happended if you're removing 7th element? At that time, main_tabControl.TabPages.Count = 4 and i variable = 6. So, i variable is exceed the range of TabPages.
You should to change your code:
private void main_tabControl_SelectedIndexChanged(object sender, EventArgs e) {
for (int i = main_tabControl.TabPages.Count - 1; i >=0 ; i--) {
if (main_tabControl.TabPages[i].Name.Equals("storage", StringComparison.OrdinalIgnoreCase) && main_tabControl.SelectedTab.Name != "Storage") {
main_tabControl.TabPages[i].Dispose();
break;
}
}
}
Hi on this site I found how to delete multiple checkedbox objects in a checklistbox
How to delete multiple checked items from CheckedListBox?
But it's not working for me.
My previous partner who handled this project before I did saved something in Global.answer class. I have tried to modify that script like this:
for (int i = checkedListBoxAnswers.Items.Count - 1; i >= 0; i--) {
if (checkedListBoxAnswers.GetItemCheckState(i) == CheckState.Checked)
{
Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
}
}
It can delete one checkbox correctly, but when I check for two or more checkboxes, it goes wrong...
I wonder how to do it correctly.
this is my delete button
private void buttonDelete_Click(object sender, EventArgs e)
{
if (checkedListBoxAnswers.SelectedIndices.Count < 1)
{
MessageBox.Show(this, "Please select answer to be deleted");
}
else
{
for (int i = checkedListBoxAnswers.Items.Count - 1; i >= 0; i--)
{
if (checkedListBoxAnswers.GetItemCheckState(i) == CheckState.Checked)
{
Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
}
}
updateCheckListBoxAnswers();
}
}
in-fact here is correct code:
CheckedListBox.CheckedItemCollection checkedItemColl = checkedListBoxAnswers.CheckedItems;
for (int i = checkedItemColl.Count; i > 0; i--)
{
int index = checkedItemColl[i - 1];
checkedListBoxAnswers.Items.Remove(index);
}
Because you using this code
Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
this will delete only the selected first item in the list, you must pass the index of selected Checked Item ,So it will delete that item only,
This is how you get all checked Item from List:
CheckedListBox.CheckedItemCollection checkedItemColl = checkedListBoxAnswers.CheckedItems;
for (int i = checkedItemColl.Count; i > 0; i--)
{
int index = checkedItemColl[i - 1];
checkedListBoxAnswers.Items.Remove(index);
}
remove comments and test the code. hope you will get benifit from it.
you can try this...
Global.answers.RemoveAt(i);
hie bro i have sample ... you can evolve tray this in your sweet home....
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
listBox1.Items.Add(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
if (e.NewValue == CheckState.Unchecked)
listBox1.Items.Remove(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
}
Your are Removing an Object so Parse that Object into Integer
by this way
Convert.ToInt32(Object)
in your case it will be..
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
listBox1.Items.Add(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
if (e.NewValue == CheckState.Unchecked)
listBox1.Items.Remove(Convert.ToInt32(checkedListBox1.Items[checkedListBox1.SelectedIndex]));
}
I'm trying to move selected items in list box1 to list box2, and vice versa. I have two buttons, >> and <<. When I select items in listbox1 and then click on >> the items should move from listbox1 to listbox2.
private void MoveListBoxItems(ListBox source, ListBox destination)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
foreach (var item in sourceItems)
{
destination.Items.Add(item);
}
while (source.SelectedItems.Count > 0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
MoveListBoxItems(listbox , lstActivity);
}
your code works fine. i tested it.
your question is "I try to move selected item in list box1 to list box2."
i think your button2 has problem.delete button2 and the code below
private void button2_Click_1(object sender, EventArgs e)
{
MoveListBoxItems(listbox , lstActivity);
}
then create other button and create click event.
full source:
private void MoveListBoxItems(ListBox source, ListBox destination)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
foreach (var item in sourceItems)
{
destination.Items.Add(item);
}
while (source.SelectedItems.Count > 0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
private void first2second_Click(object sender, EventArgs e)
{
MoveListBoxItems(FirstListbox, LastListbox);
}
private void second2first_Click(object sender, EventArgs e)
{
MoveListBoxItems(LastListbox, FirstListbox);
}
this code is work. if you want select more than one item change property
SelectionMode = MultiSimple;
private void buttonMoveToListBox1_Click(object sender, EventArgs e)
{
if(listBox1.SelectedIndex != -1)
{
listBox2.Items.Add(listBox1.SelectedValue);
listBox1.Items.Remove(listBox1.SelectedValue);
}
}
private void buttonMoveToListBox2_Click(object sender, EventArgs e)
{
if(listBox2.SelectedIndex != -1)
{
listBox1.Items.Add(listBox2.SelectedValue);
listBox2.Items.Remove(listBox2.SelectedValue);
}
}
There will be conflicts for every deleted row, so go with the below code:
>>
for (int intCount = ListBox1.SelectedItems.Count - 1; intCount >= 0; intCount--)
{
ListBox2.Items.Add(ListBox1.SelectedItems[intCount]);
ListBox1.Items.Remove(ListBox1.SelectedItems[intCount]);
}
<<
for (int intCount = ListBox2.SelectedItems.Count - 1; intCount >= 0; intCount--)
{
ListBox1.Items.Add(ListBox2.SelectedItems[intCount]);
ListBox2.Items.Remove(ListBox2.SelectedItems[intCount]);
}
If the above one doesn't work then try this:
while (ListBox1.SelectedItems.Count > 0)
{
ListBox2.Items.Add(ListBox1.SelectedItems[0].Text);
ListBox1.SelectedItems[0].Remove();
}
For more type of answers you can go with this link
Here Two ListBoxes. When i select the name from Left listbox and click ">>" Button the data move to Right ListBox
Code..
currentItemText = LeftListBox.SelectedValue.ToString();
currentItemIndex = LeftListBox.SelectedIndex;
RightListBox.Items.Add(currentItemText);
if (myDataList != null)
{
myDataList.RemoveAt(currentItemIndex);
}
ApplyDataBinding();
I solved using this code:
private void MoveListBoxItems(ListBox poSource, ListBox poDestination)
{
while (poSource.SelectedItems.Count > 0)
{
poDestination.Items.Add(poSource.SelectedItems[0]);
poSource.Items.Remove(poSource.SelectedItems[0]);
}
}
<script type="text/javascript">
$(document).ready(function() {
// > arrow
$('#SingleRightMove').click(function() {
$('#fromBox option:selected').remove().appendTo('#toBox');
//$("#tobox option").attr("selected", false);
$('#toBox').find("option").attr("selected", false);
});
// < arrow
$('#SingleLeftMove').click(function() {
$('#toBox option:selected').remove().appendTo('#fromBox');
$("#fromBox option").attr("selected", false);
});
// >> arrow
$('#AllRightMove').click(function() {
$('#fromBox option').remove().appendTo('#toBox');
$("#toBox option").attr("selected", false);
});
// << arrow
$('#AllLeftMove').click(function() {
$('#toBox option').remove().appendTo('#fromBox');
$("#fromBox option").attr("selected", false);
});
});
</script>
Get the >> and << buttons created with 2 list boxes like listbox2 and 3 below...get ur items in list box 2 first by something like get-content or get-adcomputer etc..
$buttonMOVERight_Click={
foreach ($Srv in $listbox2.selectedItems)
{$selectedServers=$Srv}
$listbox3.BeginUpdate()
foreach($TSrv in $Srv)
{$listbox3.Items.Add($TSrv);
$listbox2.Items.Remove($TSrv);}
$listbox3.EndUpdate()
}
$buttonMoveLeft_Click={
#TODO: Place custom script here
foreach ($Srvs in $listbox3.selectedItems)
{$ServersinRt=$Srvs}
$listbox2.BeginUpdate()
foreach($rSRv in $Srvs)
{$listbox2.Items.Add($rSRv);
$listbox3.Items.Remove($Srvs);}
$listbox2.EndUpdate()
}
I have a small program that has several checkedboxlists in VS2010. I wanted to allow a user to select all in one of the lists and came up with this looping structure...
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
Applications.Add(CheckedListBox1.Items[e.Index].ToString());
}
else if (e.NewValue == CheckState.Unchecked)
{
Applications.Remove(CheckedListBox1.Items[e.Index].ToString());
}
}
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (CheckedListBox1.SelectedIndex == 0)
{
for (int i = 1; i < CheckedListBox1.Items.Count; i++)
{
CheckedListBox1.SetItemChecked(i, CheckedListBox1.GetItemChecked(0));
}
}
else
{
if (!CheckedListBox1.GetItemChecked(CheckedListBox1.SelectedIndex))
{
CheckedListBox1.SetItemChecked(0, false);
}
}
}
The problem is this also puts the "Select All" checkbox into the output. Is there a way I can tweak the loop to not include the first checkbox (which is the "Select All" Check) or should I be going about this a different way?
Pretty unclear what "into the output" might mean. Using the SelectedIndexChanged event isn't very appropriate, the ItemCheck event signals checking an item. Try this instead:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (e.Index == 0) {
for (int item = 1; item < checkedListBox1.Items.Count; item++) {
checkedListBox1.SetItemChecked(item, true);
}
e.NewValue = CheckState.Unchecked; // Prevent "Check All" from getting checked
}
}
If you want to use SelectedIndexChanged anyway then still keep this event handler to prevent the item from getting checked.
I have a check list box control and I want to select only one item at a time and I am currently using this code to do the same.
private void CLSTVariable_ItemCheck(object sender, ItemCheckEventArgs e)
{
// Local variable
int ListIndex;
CLSTVariable.ItemCheck -= CLSTVariable_ItemCheck;
for (ListIndex = 0;
ListIndex < CLSTVariable.Items.Count;
ListIndex++)
{
// Unchecked all items that is not currently selected
if (CLSTVariable.SelectedIndex != ListIndex)
{
// set item as unchecked
CLSTVariable.SetItemChecked(ListIndex, false);
} // if
else
{
// set selected item as checked
CLSTVariable.SetItemChecked(ListIndex, true);
}
} // for
CLSTVariable.ItemCheck += CLSTVariable_ItemCheck;
}
this code is working fine.
but problem is that when I click again and again on selected item then that selected item should not be unchecked, means at least one item should be checked always...
I agree with commentators above - you should consider using radiobuttons. But if you really need CheckedListBox, then use this ItemChecked event handler instead:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (checkedListBox1.CheckedItems.Count == 1)
{
Boolean isCheckedItemBeingUnchecked = (e.CurrentValue == CheckState.Checked);
if (isCheckedItemBeingUnchecked)
{
e.NewValue = CheckState.Checked;
}
else
{
Int32 checkedItemIndex = checkedListBox1.CheckedIndices[0];
checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
checkedListBox1.SetItemChecked(checkedItemIndex, false);
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
}
return;
}
}
Well, it was an answer to me! I couldn't get the above code to work in the checkedListBox1_ItemCheck. I had to modify a portion of it ans include it in the checkedListBox1_SelectedIndexChanged event. But I couldn't remove the original code all together. Here is what I've added...
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox1.CheckedItems.Count > 1)
{
Int32 checkedItemIndex = checkedListBox1.CheckedIndices[0];
checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
checkedListBox1.SetItemChecked(checkedItemIndex, false);
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
}
}
Which is basically, if you have more than 1 box checked, switch the last one for the new one. I'm curious why the original code didn't work. And why it has to be there for my new code to work? Thank you.
I found this code it work so well
private void chkboxmov_ItemCheck(object sender, ItemCheckEventArgs e)
{
for (int ix = 0; ix < chkboxmov.Items.Count; ++ix)
if (ix != e.Index)
chkboxmov.SetItemChecked(ix, false);
}
"at least one item should be checked always"
The current solution (the last one) allows items to be checked off. If your purpose is to select exactly one item at all times, use this as a MouseUp event,
private void ChklbBatchType_MouseUp(object sender, MouseEventArgs e)
{
int index = ((CheckedListBox)sender).SelectedIndex;
for (int ix = 0; ix < ((CheckedListBox)sender).Items.Count; ++ix)
if (index != ix) { ((CheckedListBox)sender).SetItemChecked(ix, false); }
else ((CheckedListBox)sender).SetItemChecked(ix, true);
}