I have this code:
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "blahblah")
{
processing ps = new processing();
pictureBox1.Image = ps.blahblah(bmp);
}
else
{...
}
}
So the action of the ComboBox is done by clicking on the button1.
It is possible to take action immediately after selecting Item? without button clicking?
Subscribe to the SelectedIndexChanged event
comboBox1.SelectedIndexChanged += OnSelectedIndexChanged;
private void OnSelectedIndexChanged(object sender, EventArgs e) {
// Handle combo box changing
}
Try using this event,
ComboBox1.SelectedIndexChanged
and do
AutoPostBack = "true"
in your mark up if you want to check the selected item immediately after selecting item.
Related
I'd like to be able to have a button ONLY enabled when a certain listview has a selected item... such as listView1.SelectedItems.Count > 0
I can enable a button once a listViewItem is selected... but I can't figure out how to UNenable once the user clicks away from the listView.
Is there any "ListViewItem DeActivate" function? I've looked around but can't find anything.
You can do this in the SelectedIndexChanged event ...
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
button1.Enabled = listView1.SelectedItems.Count > 0;
}
use this code i am writing this code by supposing that in list at 0 index you have nothing to select or it has --select-- value
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0)
{
//this code will disable the button if it has any selection
button1.Enabled =false;
}
if(listView1.SelectedItems.Count==0)
{
//this code will enable the button if it has any selection
button1.Enabled =true;
}
}
Look into the Lostfocus event and then try something like this.
private void Lost_Focus_Ev(object sender, RoutedEventArgs e)
{
My_button.IsEnabled = false;
}
Everytime a user selects another control the button will be disabled. You can renable the button when the listview is re selected.
I have one Combo box and it has SelectedIndexChanged Event but i want Ignore that event in some case how can i achieve that functionality.
describe code is below
private void Form1_Load(object sender, EventArgs e)
{
List<string> lstString = new List<string>();
lstString.Add("One");
lstString.Add("Two");
lstString.Add("Three");
foreach (string str in lstString)
cBox.Items.Add(str);
//Here I want Ignore cbox_SelectedIndexChanged Event
cBox.SelectedIndex = 0;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}
You can choose either of the 2 approaches.
Have a bool flag which will be set for those conditions when you want to ignore the event handler from running. And use that flag inside your SelectedIndexChanged method
Subscribe to the event only after you have set cBox.SelectedIndex=0 if that is the only case.
Instead of subscribing the event within the designer (I expect you to do this at the moment) you can subscribe to the event in code after the initialization is done.
private void Form1_Load(object sender, EventArgs e)
{
// Init stuff
cBox.SelectedIndex = 0;
// Event subscription
cBox.SelectedIndexChanged += cBox_SelectedIndexChanged;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}
This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}
I have a dropdown list and radio button. If something is selected from the dropdown by the user, I want the radio button cleared. If the radio button is selected I want the selection of the dropdown cleared. Unfortunately, this creates events that cancel each other out. I tried using the sender as shown below to determine if the value was being changed by code or by the user, but that doesn't work. How do I make these events only work if the user is the source of the action?
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender is RadioButton)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender is ComboBox)
{
// Display
rbBlank.IsChecked = false;
}
}
You won't be able to tell the difference between the two since the source will be the same instance for both occasions.
This doesn't answer the question directly but if you compare the SelectedIndex of comboBoxTitles in the SelectionChanged event handler, your problem should be solved
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBoxTitles.SelectedIndex != -1)
{
rbBlank.IsChecked = false;
}
}
Try to compare if sender == instance of a control instead of is type of.
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender == rbBlank)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender == comboBoxTitles)
{
// Display
rbBlank.IsChecked = false;
}
}
If you know the IDs of those controls, you can try something like this:
System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl) sender;
if( webControl.ID == <comboboxId>)
{
//Do something
}
I havent tried this, but I guess it might work.
Hi all i am having 2 imagebuttons a gridview and a button. Now if i clicked on Image button i will show a grid. Now under button click i would like to capture which image button was clicked if 1st image button is clicked i would like to some values and if 2nd one is clicked i would like to show another
You can discern which button was pressed by comparing the sender parameter:
void MyButton_Click(object sender, EventArgs e)
{
if (sender == MyButton1)
{
// 1st image button was clicked — some values
}
else if (sender == MyButton2)
{
// 2nd one was clicked — show another
}
}
Can't you create 2 events and one function?
ex:
//Hook both OnClick events to these!
private void OnButton1Click(object sender, EventArgs e) { BeenClicked(button1); }
private void OnButton2Click(object sender, EventArgs e) { BeenClicked(button2); }
private void BeenClicked(Button ClickedButton)
{
if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!");
}
Or alternatively you could use:
//Hook both OnClick events to this!
private void OnButtonClick(object sender, EventArgs e)
{
ClickedButton = (Button)sender;
if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!");
}
If I understood you right :)