I have a tabcontrol with 4 tabpages.I have a Checked list box with 8 items.I want to open the tabpages which were checked in the checkedlistbox.I tried like this.
private void clbScenario_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (clbScenario.SelectedIndex == 0 || clbScenario.SelectedIndex == 1 || clbScenario.SelectedIndex == 2 || clbScenario.SelectedIndex == 3 || clbScenario.SelectedIndex == 4)
{
tabControl1.TabPages.Add(tp1);
}
else
HideTabPage(tp1);
if (clbScenario.SelectedIndex == 5 || clbScenario.SelectedIndex == 8)
{
tabControl1.TabPages.Add(tp2);
//ShowTabPage(tp2);
}
else
HideTabPage(tp2);
if (clbScenario.SelectedIndex == 6)
{
tabControl1.TabPages.Add(tp3);
}
else
HideTabPage(tp3);
if (clbScenario.SelectedIndex == 7)
{
tabControl1.TabPages.Add(tp4);
}
else
HideTabPage(tp4);
}
But the result is not as i thought.Please help me anyone
You need to use ItemCheckEventArgs e not the CheckedListBox itself. e.index is gonna give you which item is checked/unchecked and e.CurrentValue is gonna give you wheter is checked/unchecked. What you need to consider is if the e.CurrentValue is unchecked that means it's actually gonna be checked because this is showing value of the control before the process.
private void clbScenario_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.Index >= 0 && e.Index <= 4)
{
if (e.CurrentValue.ToString() == "Unchecked") tabControl1.TabPages.Add(tp1);
else HideTabPage(tp1);
}
else if (e.Index == 5 || e.Index == 8)
{
if (e.CurrentValue.ToString() == "Unchecked") tabControl1.TabPages.Add(tp2);
else HideTabPage(tp2);
}
else if (e.Index == 6)
{
if (e.CurrentValue.ToString() == "Unchecked") tabControl1.TabPages.Add(tp3);
HideTabPage(tp3);
}
else if (e.Index == 7)
{
if (e.CurrentValue.ToString() == "Unchecked") tabControl1.TabPages.Add(tp4);
else HideTabPage(tp4);
}
}
Related
I am creating a scheduler where data I entered below on two textboxes have to appear according to the Day or week and Time values. I've tried with the following code, but didn't work.
private void btnEnterData_click(object sender, System.EventArgs e)
{
if (((cmbDayWeek.SelectedIndex == 0) && (chb910.Checked == true)))
{
rtb1.Text = (tbxMedNumAppn.Text + tbxDocSpecAppn.Text);
}
else if (((cmbDayWeek.SelectedIndex == 0) && (chb1011.Checked == true)))
{
rtb7.Text = (tbxMedNumAppn.Text + tbxDocSpecAppn.Text);
}
return;
//and codes all the way down
}
private void btnSavePatient_Click(object sender, EventArgs e)
{
if (((cmbDayWeek.SelectedIndex == 0) && (chb910.Checked == true)))
{
rtb1.Show();
}
if (((cmbDayWeek.SelectedIndex == 0) && (chb1011.Checked == true)))
{
rtb2.Show();
}
// codes all the way down
}
P.S. I'm assuming that I have to use If/Else statement, but I can't get the logic fully :(
Image of the scheduler:
I'm creating a datagridview in WinForms. Each cell in the datagridview is either a textboxcell or datagridview image cell. I'm firing a cellMouseDownEent( object sender, DataGridViewCellMouseEventArgs e). If the sected cell is a image cell I perform task1 and if it is textboxcell I perform task2. I'm not getting how to find out whether the current cell is image cell or text box cell. I tried setting tag property of image cell to 0 and textboxcell cell to 1 to identify which is being clicked, but no luck. Any advice is aapreciated.
Thanks,
I'm adding my code here:
Ignore if a column or row header is clicked
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
if (e.Button == MouseButtons.Right)
{
DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];
// Here you can do whatever you want with the cell
this.dgvAddFilters.CurrentCell = clickedCell; // Select the clicked cell, for instance
// Get mouse position relative to the vehicles grid
var relativeMousePosition = dgvAddFilters.PointToClient(Cursor.Position);
if (clickedCell.Tag.ToString()==null)
{
return;
}
else if (imageCell == null) return;
else if (e.ColumnIndex == 0 && e.RowIndex == 0)
{
if ((dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null))
// (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null))
{
dgvAddFilters.ContextMenuStrip = contMenuOr;
this.contMenuOr.Show(dgvAddFilters, relativeMousePosition);
}
else return;
}
else if ((e.ColumnIndex == 0)
&& (e.RowIndex > 0)
&& (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuFilterMenu;
this.contMenuFilterMenu.Show(dgvAddFilters, relativeMousePosition);
}
else if ((e.ColumnIndex == 0)
&& (e.RowIndex > 0)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value == null)
&& (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuOrEditDelete;
this.contMenuOrEditDelete.Show(dgvAddFilters, relativeMousePosition);
}
else if ((e.ColumnIndex == 0)
&& (e.RowIndex > 0)
&& (dgvAddFilters[e.ColumnIndex + 2, e.RowIndex].Value == null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex + 2].Value != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuAndDeleteEditMenu;
this.contMenuAndDeleteEditMenu.Show(dgvAddFilters, relativeMousePosition);
}
else if ((dgvAddFilters[e.ColumnIndex, (e.RowIndex + 2)] != null)
&& (dgvAddFilters[(e.ColumnIndex + 2), e.RowIndex].Value != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contmenuDeletEdit;
this.contmenuDeletEdit.Show(dgvAddFilters, relativeMousePosition);
}
else if ((dgvAddFilters[e.ColumnIndex, (e.RowIndex + 2)] != null)
&& (dgvAddFilters[e.ColumnIndex, e.RowIndex].Value != null))
{
dgvAddFilters.ContextMenuStrip = contMenuAndDeleteEditMenu;
this.contMenuAndDeleteEditMenu.Show(dgvAddFilters, relativeMousePosition);
}
else
{
return;
}
To know the type of cell that is clicked, You can try below way of doing.... See if it is helpful.
Get the clicked cell and check for its type.
Below is an example to check for checkbox type cell.
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
Type type = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType();
if (type.Name == "DataGridViewCheckBoxCell")
{
string value = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}
}
Does this help you?
using System.ComponentModel;
using System.Windows.Forms;
namespace DGVCellTypes_47599159
{
public partial class Form1 : Form
{
DataGridView dgv = new DataGridView();
BindingList<dgventry> dgventries = new BindingList<dgventry>();
public Form1()
{
InitializeComponent();
InitOurStuff();
}
private void InitOurStuff()
{
this.Controls.Add(dgv);
dgv.Dock = DockStyle.Top;
dgv.DataSource = dgventries;
dgv.CellMouseDown += Dgv_CellMouseDown;
for (int i = 0; i < 10; i++)
{
dgventries.Add(new dgventry { col1 = $"col1_{i}", col2 = $"col2_{i}", col3 = (i % 2) > 0 });
}
}
private void Dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewTextBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewImageCell)
{
//do something
}
else
{
//do something
}
}
}
public class dgventry
{
public string col1 { get; set; }
public string col2 { get; set; }
public bool col3 { get; set; }
}
}
I'm not getting how to find out whether the current cell is image cell or text box cell
private void Dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewTextBoxCell)
{
//do something
}
else if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewImageCell)
{
//do something
}
else
{
//do something
}
}
I have a ListBox that contains items, which I have chosen from a ComboBox. When I chose an item from my ComboBox, it also removes that item in my ComboBox.
I have a button on my form, that let's me duplicate the selected item in my ListBox, giving me two of the same items in the ListBox.
I also have a button on my form, that let's me remove the selected item from my ListBox. If I remove an item from the ListBox, that item goes back to the ComboBox, but if I have duplicated the item and I remove both items from the ListBox, they both show up in the ComboBox.
I don't want it to be possible to have two of the same item in the ComboBox
Here's the code I have tried to use:
private void buttonRemove_Click(object sender, EventArgs e)
{
try
{
if (ComboBox.ToString().Contains("Chicken McNuggets"))
{
found = true;
}
if (!found)
{
ComboBox.Items.Add("Chicken McNuggets");
found = false;
}
ListBox.Items.Remove(ListBox.SelectedItem);
}
catch
{
MessageBox.Show(// Message);
}
}
This is my first time asking a question in here.
edit:
private void buttonRemove_Click(object sender, EventArgs e)
{
try
{
if ((String)listBox.SelectedItem == "Big Mac" || (String)listBox.SelectedItem == "Quarter Pounder" || (String)listBox.SelectedItem == "McFeast" || (String)listBox.SelectedItem == "Cheeseburger" || (String)listBox.SelectedItem == "Hamburger" || (String)listBox.SelectedItem == "Big Tasty Bacon" || (String)listBox.SelectedItem == "McChicken" || (String)listBox.SelectedItem == "Fillet-O-Fish" || (String)listBox.SelectedItem == "Chicken Nuggets")
{
comboBox.Items.Add(listBox.SelectedItem);
listBox.Items.Remove(listBox.SelectedItem);
}
else if ((String)listBox.SelectedItem == "BBQ dip" || (String)listBox.SelectedItem == "Cheddar dip" || (String)listBox.SelectedItem == "Gulerod" || (String)listBox.SelectedItem == "Hvidløgs dip" || (String)listBox.SelectedItem == "Karry dip" || (String)listBox.SelectedItem == "Ketchup" || (String)listBox.SelectedItem == "Pommes Frites Sauce" || (String)listBox.SelectedItem == "Sennep dip" || (String)listBox.SelectedItem == "Sursød dip" || (String)listBox.SelectedItem == "Æbler")
{
comboBox2.Items.Add(listBox.SelectedItem);
listBox.Items.Remove(listBox.SelectedItem);
}
else if ((String)listBox.SelectedItem == "Gulerodskage" || (String)listBox.SelectedItem == "Kanelsnegl" || (String)listBox.SelectedItem == "McDonut chokolade" || (String)listBox.SelectedItem == "Sundae m. chokoladesauce" || (String)listBox.SelectedItem == "McDonut sukkerovertræk" || (String)listBox.SelectedItem == "McFlurry Daim" || (String)listBox.SelectedItem == "McFlurry Smarties" || (String)listBox.SelectedItem == "Sundae m. jordbærdsauce" || (String)listBox.SelectedItem == "Sundae m. karamelsauce" || (String)listBox.SelectedItem == "Triple chokolade muffin" || (String)listBox.SelectedItem == "Vaffelis")
{
comboBox3.Items.Add(listBox.SelectedItem);
listBox.Items.Remove(listBox.SelectedItem);
}
else if (listBox.SelectedItem.ToString().Contains("Stor") || listBox.SelectedItem.ToString().Contains("Mellem") || listBox.SelectedItem.ToString().Contains("Lille") || listBox.SelectedItem.ToString().Contains("9") || listBox.SelectedItem.ToString().Contains("6") || listBox.SelectedItem.ToString().Contains("4"))
{
string objectToString = listBox.SelectedItem.ToString();
string[] ord = objektToString.Split(' ');
string selectedItem = listBox.SelectedItem;
var check = comboBox.Items.Cast<string>()
.ToList()
.FirstOrDefault(c => c.Contains(selectedItem));
if (check != null)
{
comboBox.Items.Add("Chicken McNuggets");
}
else
{
listBox.Items.Remove(selectedItem);
}
if (listBox.SelectedItem.ToString().Contains("Pommes Frites"))
comboBox2.Items.Add(ord[1] + " " + ord[2]);
else if (listBox.SelectedItem.ToString().Equals("Stor Coca-Cola") || listBox.SelectedItem.ToString().Equals("Mellem Coca-Cola") || listBox.SelectedItem.ToString().Equals("Lille Coca-Cola"))
comboBox4.Items.Add(ord[1]);
else if (listBox.SelectedItem.ToString().Contains("Milkshake"))
comboBox4.Items.Add(ord[1] + " " + ord[2] + " " + ord[3]);
else
comboBox4.Items.Add(ord[1] + " " + ord[2]);
listBox.Items.Remove(listBox.SelectedItem);
}
}
catch
{
MessageBox.Show(// Message);
}
}
We need to Cast to string the items from ComboBox so we can now easily check the ListBox.SelectedItem that already from ComboBox.Items.
private void buttonRemove_Click(object sender, EventArgs e)
{
try
{
string selectedItems = listBox1.SelectedItem.ToString();
var check = comboBox1.Items.Cast<string>()
.ToList()
.FirstOrDefault(c => c.Contains(selectedItems));
if (check != null)
{
}
else
{
comboBox.Items.Add("Chicken McNuggets");
listBox1.Items.Remove(selectedItems);
}
}
catch
{
//MessageBox.Show();
}
}
In the following code, the two calls to Zoom(0.1f); and Zoom(-0.1f); work but I cannot trigger the two UndoRedoManager.Undo(); and UndoRedoManager.Redo(); calls for CTRL+Z and CTRL+Y. What am I doing wrong?
public void WorkspaceKeyDown(KeyEventArgs e)
{
if (e.Control == true)
isCtrlPres = true;
if (e.Shift == true)
isShiftPres = true;
if (e.Control == true && e.KeyCode == Keys.Z)
{
UndoRedoManager.Undo();
}
else if (e.Control == true && e.KeyCode == Keys.Y)
{
UndoRedoManager.Redo();
}
else if (e.Control == true && e.KeyCode == Keys.Oemplus)
{
Zoom(0.1f);
}
else if (e.Control == true && e.KeyCode == Keys.OemMinus)
{
Zoom(-0.1f);
}
.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Z)));
Is there a better way to check the button state for all mouse buttons than to check for any different button extra?
var mouseEventArgs = (System.Windows.Input.MouseEventArgs)e.StagingItem.Input;
if (mouseEventArgs.LeftButton == MouseButtonState.Released &&
mouseEventArgs.MiddleButton == MouseButtonState.Released &&
mouseEventArgs.RightButton == MouseButtonState.Released &&
mouseEventArgs.XButton1 == MouseButtonState.Released &&
mouseEventArgs.XButton2 == MouseButtonState.Released)
{
return;
}
If not, how could I do it more elegant without repeating myself so much?
Thanks in advance!
I don't think there is much you can do except refactoring this into a method, since there is no predefined collection for all buttons. If you want it completely out of sight you can use an extension method like this:
public static class Extensions
{
public static bool CheckUniformButtonState(this MouseButtonEventArgs e, MouseButtonState state)
{
switch (state)
{
case MouseButtonState.Pressed:
return (e.LeftButton == MouseButtonState.Pressed &&
e.RightButton == MouseButtonState.Pressed &&
e.MiddleButton == MouseButtonState.Pressed &&
e.XButton1 == MouseButtonState.Pressed &&
e.XButton2 == MouseButtonState.Pressed);
case MouseButtonState.Released:
return (e.LeftButton == MouseButtonState.Released &&
e.RightButton == MouseButtonState.Released &&
e.MiddleButton == MouseButtonState.Released &&
e.XButton1 == MouseButtonState.Released &&
e.XButton2 == MouseButtonState.Released);
default:
return false;
}
}
}
(Not that anyone would ever check if all 5 buttons are pressed..)
Then you can check like this:
if (mouseEventArgs.CheckUniformButtonState(MouseButtonState.Released))
{
return;
}
var buttonStates = new [] {
mouseEventArgs.LeftButton,
mouseEventArgs.MiddleButton,
mouseEventArgs.RightButton,
mouseEventArgs.XButton1,
mouseEventArgs.XButton2};
if (buttonStates.All(s => s == MouseButtonState.Released))
{
return;
}