I have a listbox on my website, which contains some elements.
I made the event, OnSelectedIndexChanged, so when the user presses an element, this value will be put into a textbox
protected void Page_Load(object sender, EventArgs e)
{
listbox = new Listbox();
// Add to page etc.
listbox.SelectedIndexChanged += new EventHandler(listbox_SelectedIndexChanged);
}
void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
textbox_name.Text = listbox.SelectedItem.ToString();
}
catch
{
textbox_info.Text = "Choose employee";
}
}
It works in c# windows forms, but not in web forms for some reason.
Is it possible to get it to work?
Thank you
Set autopostback property of the listbox true
Related
I have the following code in my WinForms C# app:
private void button2_Click(object sender, EventArgs e)
{
var txtbox2 = new System.Windows.Forms.RichTextBox();
TabPage createdtabpage = new TabPage("I am a tab");
tabControl1.TabPages.Add(createdtabpage);
createdtabpage.Controls.Add(txtbox2);
}
And I want to access the text of txtbox2 when a separate button is clicked. I have this code:
private void button1_Click(object sender, EventArgs e)
{
//Either this:
string text = txtbox2.Text;
//or maybe this:
string text = createdtabpage.Controls[txtbox2]
}
However, this code doesn't work because the variables are not accessible to outside functions.Does anybody have a good way to access these TabPage controls from an outside function?
Thanks for any help
From the control tab, the tab is selected and then the desired control is found by name.
use this code
private void button1_Click(object sender, EventArgs e)
{
var textBox = (RichTextBox)tabControl1.SelectedTab.Controls["txtbox2"];
MessageBox.Show(textbox.Text);
}
Without a name, you have to navigate through all the controls, and for example, if you have multiple richTextBox controls, know the order in which they are located.
foreach (Control control in tabControl1.SelectedTab.Controls)
{
if(control is RichTextBox)
{
MessageBox.Show(control.Text);
}
}
I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.
Im using windowsForms c# have a listbox which is bound with data items ...So depending on double click of the listbox item i want the corressponding form open...thnx in adv
ListBox has an DoubleClick event. You can access it if you select object, open Events tab in a Properties window.
Double click it and Visual Studio will create an event handler for you like this:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
// here is your code
}
Now, you just need to enter your code.
If you want to open a form with a corresponding item then it will be something like:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
string text = listBox1.Text; // Don't forget to manipulate with it
Form1 form = new Form1();
form.Show();
}
Subscribe to DoubleClick event on ListBox
listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// logic here
}
or
listBox1.DoubleClick += (s,e) => { /*logic here */};
I would use the MouseDoubleClick Event, it provides the cursor position in MouseEventArgs so you can easily detect which item got doubleclicked.
void Listbox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = Listbox1.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
object clickedItem = Listbox1.Items[index];
// open your form here
}
}
Here is the complete code. I want to display a radiogroup when I select 1 in the dropdown list box. I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'.
namespace HostelRoomManagement
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "1")
{
RadioButton rb1 = new RadioButton();
rb1.ID = "RadioButton1";
rb1.Text = "C block";
rb1.GroupName = "BlockGroup";
RadioButton rb2 = new RadioButton();
rb2.ID = "RadioButton2";
rb2.Text = "C block";
rb2.GroupName = "BlockGroup";
Page.Controls.Add(rb1);
Page.Controls.Add(rb2);
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
}
}
}
I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'
You can add the radio buttons you want the user to see based on his choice to the page and set the Visibility of them to false. Then, once the user choose a value change the visibility of the radio buttons you want to true. It might be easier.
I suspect your problem is that you are referencing to SelectedValue whereas you want to be refering to selectedindex.
Hope this helps.
You have created two radio button but where have you added them on the page?
Start by creating a place holder for you radio-button lists and add these controls over there.
The dynamically created control will be lost on the post back. This means you will have to manage you dynamically created controls.
Here is a good [example]: 1 ASP.NET dynamically created controls and Postback.
Is there any way to prevent changing the selected item in a ComboBox only if for certain conditions? I want to allow update the selected item's displayValue in the ComboBox. But I don't want user to change the selected item when it's being updated. This is a windows application.
Inside your class:
private int _selectedIndex = 0;
Inside your form load method:
comboBox1.Enter += new EventHandler(comboBox1_Enter);
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
Then the rest of the code:
protected void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (true) { // Add your validation or certain condition here.
(sender as ComboBox).SelectedIndex = _selectedIndex;
}
}
protected void comboBox1_Enter(object sender, EventArgs e) {
_selectedIndex = (sender as ComboBox).SelectedIndex;
}
Try setting set the Enabled property to false. (Or some third-party toolkits like Telerik have a ComboBox with a ReadOnly property.)