Calling a method when a listview item is focused C# - c#

I am making a small C# application where the user enters information. the information is stored in an object and the object is in turn stored in a list. The information is displayed to the user in a listview.
I want to make it so that when the user clicks on an item in the listview the index of that item is passed to the list which finds the object with the same index and gets its information. The information is then shown in the same textboxes that the user enters his or hers information in.
My problem is that i do not now what method to call when the user selects a row in the listview.
This is what i have:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1)
{
index = listView1.FocusedItem.Index;
textBox1.Text = manager.FocusedContact(index).FirstName;
textBox2.Text = manager.FocusedContact(index).LastName;
textBox3.Text = manager.FocusedContact(index).Street;
textBox4.Text = manager.FocusedContact(index).City;
textBox5.Text = manager.FocusedContact(index).ZipCode;
}
}
i tried:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
textbox1.Text = "hi";
}
so i know that private void listView1_SelectedIndexChanged is the wrong method, or is there some option for the listview that i forgot to toggle on or off?

You should be able to retrieve the selected index like this:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
var index = listView1.SelectedIndex;
}
If the event is not firing at all, check that the event handler is registered correctly in the form's Designer.cs file. In your case, it should look like this:
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);

Related

Selected index changed fail into ListBox

I created a windows forms like this
As you can see in selected changed event I disable button move to, it works correctly, problem starts when I try to
return an item it to main list, move to button keeps disable instead enable it again when I select item of first list. Someone knows
what occurs there?
Events:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = false;
btnMoveTo.Enabled = true;
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = true;
btnMoveTo.Enabled = false;
}
You need to make sure that there actually is an item being selected since ListBox.SelectedIndexChanged event gets fired even when there're no items selected - making the new SelectedIndex equal to -1. Also, from the way you asking, I expect you want to enable btnMoveTo when there's a selected item in lstTechUnnotified and otherwise, disable it - and the same for btnReturnTo and lstTechToNotified; if that's it, then the easy way is:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnMoveTo.Enabled = (lstTechUnnotified.SelectedIndex > -1);
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = (lstTechToNotified.SelectedIndex > -1);
}
Though I'm not sure about your button names..

ComboBox.SelectedItem not working as expected C# asp.net

I have a combo box I added items to in the page load method and within a button click I am trying to set either the text that is typed or the item selected from the dropdown. I am using ComboBox.SelectedItem.ToString() which works when something is typed in but once the user selects something from dropdown the string is empty that is passed through.
protected void Page_Load(object sender, EventArgs e)
{
if (ScriptManager.GetCurrent(Page) == null)
{
Page.Form.Controls.AddAt(0, new ScriptManager());
}
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value1);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value2);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value3);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value4);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value5);
}
protected void ChangeAppSettings_Click(object sender, EventArgs e)
{
string FOPath = FODirectory.Text;
string PolicyStoreName = DiffComboBoxUrls.SelectedItem.ToString(); //this comes back as "" when an item is selected and works when something is typed in
.....
}

Get items selected from another form

Hi I have a Windows Form application. I have a Textbox. I want to implement a functionality like when the user clicks on the textbox, a list should be made available to the user and then the item selected from the list should be filled in the textbox. The list should not be available if some other control is focussed other than the textbox. What would be the better way to do this? Should I implement the list in the same form as the textbox or should I use another form for the list?
I want to implement a functionality like in the Tally Accounting
Software.
Make a panel which contains a listView
When you use a panel you can change the Visible property to hide all the content in it.
panel1.Visible = true; //visible
panel1.Visible = false; //invisible
Now you can say: when the textbox is clicked show me the list:
private void textbox1_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
Now, when the form gains focus, you can hide the list:
private void form1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
When the selected item of the List changes set the Text of your Textbox:
private void ListView1_ItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
{
foreach ( ListViewItem item in ListView1.SelectedItems)
{
textbox1.Text = item.SubItems[1].Text;
}
}
There is a few events for the text box control you can use for this.
You could use Enter and Leave to control the .Visible or .Enabled property of your list.
Use one list and dynamically fill using the enter and leave. You will need a marker to indicate what textBox you are manipulating.
Example
TextBox activeText;
private void txtBox1_Enter(object sender, EventArgs e)
{
lstMyList.dataSource = list1Data;
activeText = (TextBox)sender;
}
private void lstMyList_SelectedValueChanged(object sender, EventArgs e)
{
ListBox myList = (ListBox)sender;
activeText.Text = myList.SelectedValue.ToString();
}
Maybe something like that?
In my view use a separate form and in new form all list will be displayed. Clicking on textbox you need to show the new form.
For Getting the selected items there are many ways
Send a LIST parameter to New form(Constructor)
Get method of new form when form is closed
Static global variable
METHOD 1.
FORM2 frm = new FORM2(LIST<string> items)
frm.ShowDailog();
textBox1.text = add items from items
METHOD 2.
FORM2 frm = new FORM2()
if(frm.ShowDailog() == DialogResults.Ok)
{
textBox1.text = frm.GetSelectedItems();
}

Updating a textbox using no input controls

I have a windows form application which consists of a bunch of controls, but more specifically, two textBoxes. One of them is read only. The read only textBox value is supposed to be the same as the textBox that the user can type into.
So if the user types "Hello World" into textBox A, the value in textBox B should be automatically updated to "Hello World".
How do I go about doing this? I know I just need to set the text values, I'm just not sure where I place the code to get it done automatically rather than executed when a button is click or something along those lines.
TextChanged event:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
It sounds like you want something like:
writableTextBox.TextChanged += delegate {
readonlyTextBox.Text = writableTextBox.Text;
};
In other words, whenever the text in one textbox changes, update the other. This uses the Control.TextChanged event.
If you want textBoxB to be updated as soon as the text of textBoxA is changed (i.e immediately after the user press a key in textBoxA) the event is TextChanged:
this.textBoxA.TextChanged += new System.EventHandler(this.textBoxA_TextChanged);
private void textBoxA_TextChanged(object sender, EventArgs e)
{
textBoxB.Text = textBoxA.Text;
}
If you prefer to update the text in textBoxB only after the user has finished to edit textBoxA, you should use the Leave event:
this.textBoxA.Leave += new System.EventHandler(this.textBoxA_Leave);
private void textBoxA_Leave(object sender, EventArgs e)
{
textBoxB.Text = textBoxA.Text;
}
This should do what you need:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
Even shorter (better?) than the event approach is using winform's databinding. Just use this right after the InitializeComponents call:
readonlyTextBox.DataBindings.Add("Text", writableTextBox, "Text");

Determine source of event

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.

Categories