ComboBox SelectedIndexChanged event not firing - c#

I am using VS 2017 for an Visual C# application (.Net 4.6.2, 32 bit) that calls a form from the main form. In that second form the SelectedIndexChanged event is not firing for one of the ComboBoxes. Following is the code. If I have to register the event I don't know how. I originally had copied and pasted the ComboBoxes onto the form. Then I deleted that control and re-added the ComboBoxes from the ToolBox. Any help would be appreciated.
namespace Lottery_C_Sharp
{
public partial class Dialog_Matches_Input_Lotomania : Form
{
MatchesMethods_LM m;
public string[] lotomania_list = new string[10];
public string[] pick10_list = new string[5];
Utilities u;
public Dialog_Matches_Input_Lotomania(MatchesMethods_LM mm)
{
InitializeComponent();
m = mm;
u = new Utilities();
set_combos();
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("comboBox3_SelectedIndexChanged");
if (m.NumCurrLimit == 99)
{
set_lotomania_time_text();
set_lotomania_totals_text();
}
else
{
set_pick10_time_text();
set_pick10_totals_text();
}
}
public void set_combos()
{
set_lists();
comboBox1.Items.Clear();
comboBox2.Items.Clear();
comboBox3.Items.Clear();
if (m.NumCurrLimit == 99)
{
textBox1.Text = "Brazilian LotoMania";
AddToCombo(comboBox1, lotomania_list);
comboBox1.SelectedIndex = 0;
AddToCombo(comboBox2, lotomania_list);
comboBox2.SelectedIndex = 0;
AddToCombo(comboBox3, lotomania_list);
comboBox3.SelectedIndex = 0;
set_lotomania_time_text();
set_lotomania_totals_text();
}
else
{
textBox1.Text = "USA New York Pick 10";
AddToCombo(comboBox1, pick10_list);
comboBox1.SelectedIndex = 0;
AddToCombo(comboBox2, pick10_list);
comboBox2.SelectedIndex = 0;
AddToCombo(comboBox3, pick10_list);
comboBox3.SelectedIndex = 0;
set_pick10_time_text();
set_pick10_totals_text();
}
}

You should register the event for Your desired combobox in Designer properties, like this:
It generates event registration in Form1.Designer.cs (form name depends on how You've named it) auto-generated file:
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox3_SelectedIndexChanged);
Then Your function, where You can perform some actions when event occures shows in Form1.cs
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
// Do something
}

You have several options to register the event.
Option 1:
Register in code:
public Dialog_Matches_Input_Lotomania(MatchesMethods_LM mm)
{
InitializeComponent();
...
comboBox1.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
}
Option 2:
Double click on comboBox1 in the designer, this will automatically add and register an event.
Option 3:
Select the comboBox1 in the designer, right click and select "Properties", in the properties window select the event icon on top (the lightning symbol), find "SelectedIndexChanged" and enter the name of the event or double click to automatically add and register an event.

Related

How to open a form by Clicking on a specific Row in a ListView

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.

how to set event click when click editor button

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.

Array picturebox event click in c#

Im trying to make an event of 8 picturebox together this is my code, but when i click it put always the same image, the event its been doing twice, i put a message on the event of pictureboxes and it appears twice.
public partial class Form1 : Form
{
int jug=1;
PictureBox[] PicBox = new PictureBox[9];
Image circu = Image.FromFile("Circulo1.png");
Image cruz = Image.FromFile("Cruz1.png");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PicBox[0] = this.pcb0;
PicBox[1] = this.pcb1;
PicBox[2] = this.pcb2;
PicBox[3] = this.pcb3;
PicBox[4] = this.pcb4;
PicBox[5] = this.pcb5;
PicBox[6] = this.pcb6;
PicBox[7] = this.pcb7;
PicBox[8] = this.pcb8;
for (int i = 0; i < 9; i++)
{
PicBox[i].Click += new System.EventHandler(PictureBoxes_Click);
}
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
if (jug == 1)
{
jug = 2;
p.Image = cruz;
}
else
{
jug = 1;
p.Image = circu;
}
}
Try refreshing the pictureboxes with built in function. If that not solve the problem, set picturebox image property to null, then refresh and set the image you want.
Or you try setting click event on the design page of your ide, bind it the same function(in this case Pictureboxes_click)
There is no reason to enter the event twice. Your code actually works. I think maybe you have some other controls in your form which uses the same event. Just make sure that the event is being used only by the pictureboxes.
Another thing to do: Put breakpoint at the event and see what controls appear as sender. This will help you fix your problem.
Use pictureboxes' Tag property to understand which one is entering the event:
for (int i = 0; i < 9; i++)
{
PicBox[i].Tag = i;
}
When code enters the event, you can look at p.Tag to see which one triggered the event.
You have only 2 different images with single instances. Try to clone the images, so each PictureBox gets it's own instance of the image:
private void PictureBoxes_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
if (jug == 1)
{
jug = 2;
p.Image = (Image)cruz.Clone();
}
else
{
jug = 1;
p.Image = (Image)circu.Clone();
}
}
Or you can replace the cloning by using the "FromFile"-method, which automatically creates the new instances:
p.Image = Image.FromFile("Circulo1.png");

Capture the right button click event

I'm playing around abit with winforms and its controls and just discovered how to do custommade buttonclicks. However, there is a problem. I've got a loop, that's looping through a list of elements, and if a condition appears - I'm creating a button that will pop up a gridview.
public void draw(ref Panel inputPanel) //draws the eventline
{
int stepCounter = 0;
for (int i = 0; i < DaysList.Count-1; i++)
{
Button b1;
if (DaysList[i].Elements.Count > max)
{
b1 = new Button(); //Create the box
b1.Width = 120;
b1.Height = 40; //Set width and height
b1.Location = new Point(stepCounter + 35, 70); //Location
inputPanel.Controls.Add(b1); //
b1.Text = "Check event date in grid";
b1.Show();
b1.BringToFront();
b1.Click += new EventHandler((sender, e) => btn_Click(sender, e, DaysList[i].Elements));
stepCounter += 200;
}
}
}
That was my method for creating the buttons and a click event for the when my condition appears. The function that is passed to the eventhandler looks like this:
public void btn_Click(object sender, EventArgs e, List<EventElement> inputElems)
{
Button button = sender as Button;
DataGridForm window = new DataGridForm(inputElems);
window.Show();
}
public class EventElement
{
public EventElement()
{
}
public int Count{get;set;}
public string Date{get;set;}
}
The clickpart of the event is fine but whenever i click the spawned buttons, I get the wrong data into the gridview. As an example: The loop has created four buttons for me and they are presented on a straight line on the form. But whenever i click one of the buttons - dosnt matter which one of them, the button always return the data of the last spawned button. A more clear example: lets say we have the list inputElems looks like this:
inputElems[0].Count -> 2644
inputElems[1].Count -> 2131
inputElems[2].Count -> 8467
inputElems[3].Count -> 5462
When i now click the second button, the input to the second buttons parameter list should have the values (sender, e, 2131), right? but for some reason, the last argument gets the same like the 4th element in the list, even though i call the secondly created button.
I figured that it has something to do with me always calling the last added button_click to the eventhandler of the button, if so, how do I call different clicks from the EventHandler?
Instead of passing inputElems with the EventHandler, you can use Tag.
E.g. use:
b1.Tag=i;
Then in your click event handler:
public void btn_Click(object sender, EventArgs e)
{
Button button = sender as Button;
DataGridForm window = new DataGridForm(DaysList[int.Parse(button.Tag.ToString())].Elements);
window.Show();
}
The problem is that the for loop is out of scope, and thus unable to provide you with the data you're looking for. A more straight forward approach might be something like this:
public void draw(ref Panel inputPanel) //draws the eventline
{
int stepCounter = 0;
for (int i = 0; i < DaysList.Count-1; i++)
{
Button b1;
if (DaysList[i].Elements.Count > max)
{
b1 = new Button(); //Create the box
b1.Width = 120;
b1.Height = 40; //Set width and height
b1.Location = new Point(stepCounter + 35, 70); //Location
inputPanel.Controls.Add(b1); //
b1.Text = "Check event date in grid";
b1.Show();
b1.BringToFront();
b1.Tag = DaysList[i].Elements;
b1.Click += btn_Click;
stepCounter += 200;
}
}
}
and then in btn_Click, do this:
public void btn_Click(object sender, EventArgs e)
{
Button button = sender as Button;
int inputElems = (List<EventElement>)button.Tag;
DataGridForm window = new DataGridForm(inputElems);
window.Show();
}

Delete a set of panels with UserControls created in a loop

I created a panel with a button "add" and a button "delete". If you click on "add" a panel is created right below this header, you can create as much as you want, they are listed.
On each panel there is a checkbox and I would like to delete the panel if the checkbox is checked once the button delete is clicked.
I can get the intuition of a loop : for, but still too novice to get through this without a little tip.
-
public partial class Test_auto : Form
{
ArrayList strategyFutureList = new ArrayList();
public Test_auto()
{
InitializeComponent();
Instance = this;
}
//Create a new future strategy
public void CreateStrategyFuture()
{
ConsoleStrategyItem strategyItemFuture = new ConsoleStrategyItem();
strategyItemFuture.Location = new Point(3, 3);
futureContainer.Height += 85;
strategyFutureList.Add(strategyItemFuture);
futureContainer.Controls.Add(strategyItemFuture);
ConsoleStrategyItem.Instance.txtStrategyName.Text = "Strat Future " + strategyFutureList.IndexOf(strategyItemFuture) + " ";
ConsoleStrategyItem.Instance.Name = "strategyFuture" + strategyFutureList.IndexOf(strategyItemFuture);
ConsoleStrategyItem.Instance.cbxDeleteStrategy.Name = "cbxDeleteFuture" + strategyFutureList.IndexOf(strategyItemFuture);
}
//Makes it appear
private void btnAddStrategyFuture_Click_1(object sender, EventArgs e)
{
CreateStrategyFuture();
}
//Delete a-some selected strategies
public void DeleteStrategyFuture()
{
for (int i = 0; i < strategyFutureList.Count; i++)
{
if (ConsoleStrategyItem.Instance.cbxDeleteStrategy.Checked = true)
{
}
}
}
private void btnDeleteStrategyFuture_Click(object sender, EventArgs e)
{
DeleteStrategyFuture();
}
}
you don't have to create a separate ArrayList to maintain the list of UserControls being added to the futureContainer, you can simply iterate through the Controls collection of it.
simply do this in your DeleteStrategyFuture() method
public void DeleteStrategyFuture()
{
var length=futureContainer.Controls.Count;
foreach(int i=0; i< length; i++)
{
if(futureContainer.Controls[i].GetType()==typeof(ConsoleStrategyItem))
{
bool isChecked =((ConsoleStrategyItem)futureContainer.Controls[i])
.Instance.cbxDeleteStrategy.Checked;
if(isChecked)
{
futureContainers.Controls.Remove(futureContainers.Controls[i]);
}
}
}
}

Categories