i cant call or use event click of editor button that i create. the screen shot for the button is
the code that i make is like this
RepositoryItemComboBox repositoryItemComboBox1 = new RepositoryItemComboBox();
EditorButton lp = new EditorButton();
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
repositoryItemComboBox1.Items.Clear();
GridView view = sender as GridView;
for (int i = 0; i < gridView1.RowCount; i++)
{
if (gridView1.GetDataRow(i) == null)
{
break;
}
string code = gridView1.GetDataRow(i)["code"].ToString();
if (!repositoryItemComboBox1.Items.Contains(code))
{
repositoryItemComboBox1.Items.Add(code);
}
}
if (e.Column.FieldName == "code" && view.IsFilterRow(e.RowHandle))
{
repositoryItemComboBox1.Buttons.Add(lp);
repositoryItemComboBox1.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Plus;
repositoryItemComboBox1.Buttons[1].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Minus;
e.RepositoryItem = repositoryItemComboBox1;
}
when i click the minus nothing happen because no handler(event).
what i want is when i click that minus button it clear gridview filter
FYI : iam using devexpress
You can hook to the repositoryItem's ButtonClick event. In this event, you will know which button has been clicked. So let's say you create your button this way :
private void SomeMethod()
{
var buttonPlus = new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Plus);
var buttonMinus = new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Minus);
repositoryItemComboBox1.Buttons.Add(buttonPlus);
repositoryItemComboBox1.Buttons.Add(buttonMinus);
}
In the repositoryItemComboBox1_ButtonClick, you have access to the button properties in the "e" argument. In this example, i'm using the "Kind" property, but you could use the tag or anything really.
private void repositoryItemComboBox1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Minus)
{
// do something
}
else if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
{
// do something else
}
}
This is the way I do it.
Related
My task is to create two edit buttons for a listbox, one edit-start button and one edit-end button, with relevant functionality.
The user should be able to edit a selected item on the list box after pressing the edit-start button. the change should then be saved after pressing edit-end.
Thanks for any input on this matter.
If your project is in c# WinForms, i recomended following solution:
Add ListBox (name is MyListBox), Two Buttons(btnBeginEdit and btnEndEdit) and one edit component(MyTextBox) to your form;
In form source you may use like this code:
public Form1()
{
InitializeComponent();
for (var i = 1; i <= 10; i++)
MyListBox.Items.Add($"Item-{i}");
}
private void btnBeginEdit_Click(object sender, EventArgs e)
{
if (MyListBox.SelectedIndex == -1)
{
MessageBox.Show("Please select ListBox item firstly!");
return;
}
var item = MyListBox.SelectedItem.ToString();
MyTextBox.Text = item;
MyListBox.Enabled = false;
}
private void btnEndEdit_Click(object sender, EventArgs e)
{
if (MyListBox.SelectedIndex == -1)
return;
MyListBox.Items[MyListBox.SelectedIndex] = MyTextBox.Text;
MyListBox.Enabled = true;
}
I am writing a program where I dynamically add buttons, I do that by storing them in a Dictionary to get a certain value from them later on (the color of the background).
I need to set a Click event on every one of them, but every Click event has to be a little different, as by clicking the button, a ColorDialog pops up and changes the background of the button.
Is there a way to know which button I clicked? In the following code, the button1 click event adds the other buttons and sets the EventHandler for each of them, what should be the code for the EventHandler? Thank you so much in advance guys.
int i = 0;
Dictionary<int, Button> buttonDictionary = new Dictionary<int, Button>();
Dictionary<int, ColorDialog> colorsDictionary = new Dictionary<int ColorDialog>();
public void button1_Click(object sender, EventArgs e)
{
i++;
buttonDictionary.Add(i, new Button());
buttonDictionary[i].Click += new EventHandler(Click);
this.Controls.Add(buttonDictionary[i]);
}
public void Click(object sender, EventArgs e)
{
//Somehow get the int key of the button that was clicked???? (in this case: int j)
int j;
if (!colorsDictionary.ContainsKey(j))
{
colorsDictionary.Add(j, new ColorDialog());
}
if (colorsDictionary[j].ShowDialog() == DialogResult.OK)
{
buttonDictionary[j].BackColor = colorsDictionary[j].Color;
}
}
The code is made just for adding the buttons, I will be glad for any kind of help, thank you guys!
Well, a direct answer to your question is: cast the sender to a Button
Button pressedButton = (Button) sender;
and then check to which button of the dictionary it matches:
foreach (var entry in buttonDictionary)
{
if (entry.Value == pressedButton)
{
j = entry.Key;
break;
}
}
However, that's overly complex for what you want to achieve. It would be much easier if you had a direct relationship between the button and the color picker:
Dictionary<Button, ColorDialog> buttonDictionary = new Dictionary<Button, ColorDialog>();
Then fill it like this:
public void button1_Click(object sender, EventArgs e)
{
i++;
var button = new Button();
this.Controls.Add(button);
button.Click += new EventHandler(Click);
buttonDictionary.Add(button, null);
}
And later access it with
public void Click(object sender, EventArgs e)
{
Button pressedButton = (Button) sender;
ColorDialog dialog = buttonDictionary[pressedButton];
if (dialog == null)
{
dialog = new ColorDialog();
buttonDictionary[pressedButton] = dialog;
}
if (dialog.ShowDialog() == DialogResult.OK)
{
pressedButton.BackColor = dialog.Color;
}
}
Even more, the question is why you would need so many ColorDialgos, since it should be possible with one dialog only. You can get rid of i, j, all dictionaries and most handling as well. IMHO, the following should be sufficient:
public void button1_Click(object sender, EventArgs e)
{
var button = new Button();
Controls.Add(button);
button.Click += Click;
}
public void Click(object sender, EventArgs e)
{
Button pressedButton = (Button) sender;
ColorDialog dialog = new ColorDialog {Color = pressedButton.BackColor};
if (dialog.ShowDialog() == DialogResult.OK)
{
pressedButton.BackColor = dialog.Color;
}
}
Bonus info:
I don't exactly know what you want to achieve. But your buttons will all be in the same place, overlapping each other. To avoid this, drag a flow layout panel onto the form and then add the buttons to the flow layout:
flowLayoutPanel1.Controls.Add(button);
This will ensure that your buttons are nicely arranged.
I am trying to open a new form by clicking on a row in a ListView and pass the NoteId that is listed in the specific Row to the new Form, can anyone help please?
Sorry if this is a silly question, but I have only been programming since last month and my research proved fruitless :(
private void populatingMainList()
{
List<Note> getAllNotes = GetAllNotes();
lstMain.Items.Clear();
for (int i = 0; i < getAllNotes.Count; i++)
{
lstMain.FullRowSelect = true;
string _note;
ListViewItem lvi = new ListViewItem(_note = getAllNotes[i].NoteComplete.ToString());
if (_note == "True")
{
lvi.Text = "";
lvi.Checked = true;
}
else
{
lvi.Text = "";
lvi.Checked = false;
}
lvi.SubItems.Add(getAllNotes[i].NoteTitle);
lvi.SubItems.Add(getAllNotes[i].NoteDot.ToString("dd-MM-yyyy"));
lvi.SubItems.Add(getAllNotes[i].NoteNote);
lvi.SubItems.Add(getAllNotes[i].NoteId.ToString());
lstMain.Items.Add(lvi);
}
}
private void lstMain_SelectedIndexChanged_1(object sender, EventArgs e)
{
//I believe that some sort of code that retrieve NoteId from the specific Row must be added here.
if (_list == true)
{
frmSticky StickyForm = new frmSticky(_currentUser, _noteid);
}
}
private void lstMain_SelectedIndexChanged_1(object sender, EventArgs e)
{
var lst = sender as ListView;
_noteid = lst.SelectedItems[0].SubItems[3];
if (_list == true)
{
frmSticky StickyForm = new frmSticky(_currentUser, _noteid);
}
}
You can use a contextmenustrip for your listview and then add a button on it with function to open the form you are trying to.
1.Find ContextMenuStrip and add it to your application from toolbox.
2.Add it to your listview as shown in the image below.
3.Select the contextmenu you added and create a new button by clicking on "Type here".
4.Double click that button on your contextmenu and write the code you want to execute when you click on that button on contextmenu from listview.
I have a GridView in which i have this column :
bandedGridColumn.ColumnEdit = InitEdit_Material();
Here is the InitEdit_Material method :
public static RepositoryItemLookUpEdit InitEdit_Material()
{
RepositoryItemLookUpEdit riMaterial = new RepositoryItemLookUpEdit();
riMaterial.Columns.Add(new LookUpColumnInfo("ID", "ID"));
riMaterial.Columns.Add(new LookUpColumnInfo("CustomsMaterial.Name", "Name"));
riMaterial.DataSource = Service.GetAll(svc.EntityTypeToGet.Material).Data.All_Material;
riMaterial.DisplayMember = "MaterialFullname";
riMaterial.ValueMember = "ID";
riMaterial.AutoSearchColumnIndex = 1;
riMaterial.BestFitMode = BestFitMode.BestFitResizePopup;
riMaterial.NullText = "";
return riMaterial;
}
This is what it looks like :
I want to perform some actions (set other cell's value based on current cell value) whenever user choose a new value in this cell, but the problem is all the possible event i know only fires once the cell lost focus, i've tried :
private void vwVD_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e)
{
if (vwVD.FocusedColumn.Name == "colMaterialID")
MessageBox.Show("only show when focus lost");
return;
}
private void vwVD_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
if (e.Column.Name != "colMaterialID") return;
MessageBox.Show("only show when focus lost");
}
You can try to use GridView.CellValueChanging event:
private void vwVD_CellValueChanging(object sender, CellValueChangedEventArgs e)
{
if (vwVD.FocusedColumn.Name == "colMaterialID")
{
//Perform some actions. Use e.Value.
}
}
I'm creating a matching game. What I'm trying to accomplish is a check that will see if the tag of the previously clicked button, matches the tag of the "currently" clicked button. If those tags match, it would disable both the buttons as they are no longer an option in the game.
Part of my confusion is where to integrate this portion of code without screwing up the majority of my work.
Random myRandom = new Random();
var buttons = new List<Button> { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12 };
var carString = new List<string> { "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T", "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T" };
while (matchingButtonIndex < numOfButtons)
{
int index = myRandom.Next(carString.Count);
var name = carString[index];
if (name != null)
{
buttons[matchingButtonIndex].Tag = name;
carString[index] = null;
matchingButtonIndex = matchingButtonIndex + 1;
}
}
}
void SwitchTagWithText()
{
string text = lastButton.Text;
lastButton.Text = lastButton.Tag.ToString();
lastButton.Tag = text;
}
private void button1_Click(object sender, EventArgs e)
{
if (lastButton != null)
{
SwitchTagWithText();
}
lastButton = sender as Button;
SwitchTagWithText();
buttoncount++;
label2.Text = buttoncount.ToString();
}
I would suggest that you create a new class to handle this e.g.
public class ButtonManager {
private Button lastButton;
public void SwitchTagWithText(Button button){
string text = lastButton.Text;
lastButton.Text = lastButton.Tag.ToString();
lastButton.Tag = text;
// Or whatever the logic is you need.
}
public void ButtonClicked(Button button){
this.SwitchTagWithText(button);
// Or whatever the logic is you need.
}
}
OK the above is incomplete, but hopefully the main idea comes through - that you have a separate class to deal with the reading/setting of tag values and determining whether buttons should be disabled.
So in your from load, create a new instance of ButtonManager that can be used throughout the form's code, so make it a field.
You have a button count variable which you are incrementing with each click. So you can use that:
private void button1_Click(object sender, EventArgs e)
{
thisButton = sender as Button;
buttoncount++;
SwitchTagWithText(thisButton);
if (buttoncount == 1)
{
lastButton = thisButton;
}
else if (buttoncount == 2)
{
if (lastButton.Tag == thisButton.tag)
{
// Disable both buttons.
}
else
{
// Switch buttons back
}
buttoncount = 0;
}
}
Something like:
private void button1_Click(object sender, EventArgs e)
{
if (lastButton != null)
{
SwitchTagWithText();
var thisButton = sender as Button;
if(thisButton.Text != lastButton.Text
&& thisButton.Tag.ToString() == lastButton.Tag.ToString())
{
//two different buttons with the same tag were clicked in succession
thisButton.Enabled = false;
lastButton.Enabled = false;
lastButton = null;
return;
}
lastButton = thisButton;
}
else
{
lastButton = sender as Button;
}
SwitchTagWithText();
buttoncount++;
label2.Text = buttoncount.ToString();
}