Open new form after selecting item on listbox - c#

I have a listbox and a button on my form. Listbox contains of 3 elements: House, People, Outdoor.
I have also created 3 forms to represent the values from the listbox.
I would like to user to highlight the item on the listbox and after clicking the button I would like to open the form selected by the user.
How can I achieve this? I have tried this link: Calling new Form by clicking an item on the ListBox but without any success.
I have tried:
public Select()
{
InitializeComponent();
listBox1.Click += OnListBoxItemClick;
}
private void OnListBoxItemClick(object sender, EventArgs e)
{
var form2 = new House();
House.ShowDialog();
}
This would only allow me to open one form. How can I assigned different forms to be open with different values from listbox?
I would like the form to open after I click on the button, not the value in the listbox, how to achieve it?

You will need to use the ListBox's SelectedItem Property:
private void button1_Click(object sender, EventArgs e)
{
switch (listBox1.SelectedItem.ToString())
{
case "House":
House h = new House();
h.ShowDialog();
break;
case "People":
People p = new People();
p.ShowDialog();
break;
case "Outdoor":
Outdoor o = new Outdoor();
o.ShowDialog();
break;
}
}

You can use a Dictionary to map the values in your selected items to forms, or perhaps better yet functions that can create a form (to ensure lazy loading).
private Dictionary<string, Func<Form>> formMapping
= new Dictionary<string, Func<Form>>()
{
{"House", ()=> new House()},
{"People", ()=> new People()},
{"Outdoor", ()=> new Outdoor()},
};
Then you can use that mapping in the click event handler to translate the selected value into a form:
private void button1_Click(object sender, EventArgs e)
{
Form newForm = formMapping[listBox1.SelectedValue]();
newForm.ShowDialog();
}

Related

Get a value from another Form c#

I am trying to get a value from another form in a text box. When I click on a text box, another form appears that is a numeric keypad and I can enter a number.
What I want is for the textbox I click to get the value of that new form.
How could I do this?
Example:
I have the next textbox:
When I do click on this textbox appears the following form to introduce some numbers:
When I click on the enter button of the second form, I would like to close this form and get the value "78552" into the first textbox.
I am trying putting the textbox of the second form as a public static but is not working.
What could I do?
EDIt:
This is what I am trying:
private void micrasmin_Click(object sender, EventArgs e)
{
Tecladojm t = new Tecladojm();
t.Show();
using (var form = new Tecladojm())
{
//var result = form.ShowDialog();
if (Tecladojm.buttonEnterClicked == true)
{
string val = form.DevolverNumeroMarcado();
micrasmin.Text = val;
}
}
}
There are a number of ways to accomplish this, however I usually do the following.
1) Update your constructor in Form2 to include a parameter as so:
public Form2(string form1Text1, string form1Text2, string form1Text3)
{
InitializeComponent();
this.label1.Text = form1Text;
}
2) In your button click that opens the second form, simply do the following:
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text);
form2.Show();
}
That should work for you. Let me know if you have any questions.

Datagridview shall disappear when clicking on the Form in the background

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}

passing the datagrid row value to the next form label

hi i am working on winform and i have a datadrid view, i have a context menu strip. on that edit is written. when i click on datadrid, right click a context menu is open with edit. when clicked it should pass the value to a new form, i have written the code for transfer but it is not passing i dont know whats the problem here
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
Form6 f = new Form6();
f.label1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
}
is the code correct?
If Form6 is not already opened then you will need to show it after assigning text to its label.
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
Form6 f = new Form6();
f.label1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
f.Show();
}
If Form6 is already opened, you need to use the instance of Form6 and not to create new instance, you can use Application.OpenForms to get already opened forms.
Form6 f = (Form6)Application.OpenForms["form6"];
f.label1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

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();
}

Add listview items from other form (using objects)

I want to get some data to fill a listview control, but this data it's determined in other form. This is what I code in form1 (Nuevo_Credito):
private void combo_cliente_SelectionChangeCommitted(object sender, EventArgs e)
{
Credito_Grupo ventana = new Credito_Grupo(combo_cliente.SelectedItem);
ventana.ShowDialog();
}
public void AgregaIntegrantes(string id, string nombre, string monto)
{
ListViewItem elem = new ListViewItem(id);
elem.SubItems.Add(nombre);
elem.SubItems.Add(monto);
listView_integrantes.Items.Add(elem);
}
I'm invoking form2 (Credito_grupo) as show dialog window, then I want to retrieve some values and pass them to Form1 using the public method "AgregaIntegrantes". So in form2 I did the following:
public Credito_Grupo(dynamic item)
{
this.id = item.IDCliente;
this.nombre = item.NomComp;
InitializeComponent();
}
private void Credito_Grupo_Load(object sender, EventArgs e)
{
text_nombre.Text = this.nombre;
}
private void button_AgregaCliente_Click(object sender, EventArgs e)
{
Nuevo_Credito obj = new Nuevo_Credito();
obj.AgregaIntegrantes(id.ToString(), nombre, text_monto.Text);
this.Close();
}
When the event button_AgregaCliente_click is triggered I need to add the data to listview in form1 using the method described above, but none data is added. I found a solution using delegates here 3077677, is there an approach using objects?
You have an error in button_AgregaCliente_Click method (the last one in the listing). You create a new Nuevo_Credito form there, and pass the data to listview. It looks OK. But this newly created Nuevo_Credito form does exist only in the local variable, so then you throw it away without displaying it when button_AgregaCliente_Click finishes.
I think you need to delete this line: Nuevo_Credito obj = new Nuevo_Credito();
You need to get your real Nuevo_Credito form, not create a new one here.
You can send this from your Nuevo_Credito to the constructor of the Credito_Grupo form. Then you can use it to call back to the original Nuevo_Credito. This approach is based only on objects, and not delegates. As you wanted. :-)

Categories