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();
}
Related
I have a splitcontainer. In Panel 1 I have 7 toolstripbuttons that opens a User Control in Panel 2 when I click on them. The user control consists of different textboxes that the user can fill in. The code looks like this:
private void toolStripButtonBaseLayers_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
BaseLayers BL = new BaseLayers();
splitContainer1.Panel2.Controls.Add(BL);
}
private void toolStripButtonSpatialCoverage_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
SpatialCoverage SC = new SpatialCoverage();
splitContainer1.Panel2.Controls.Add(SC);
}
private void toolStripButtonFloodMaps_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
FloodMaps FM = new FloodMaps();
splitContainer1.Panel2.Controls.Add(FM);
}
Now everytime I open another user control with the click event the user control will be empty because I clear the panel and open a new User Control. This is not what I want to do!
My question is how it is possible to save everything you filled in in the textboxes? And how it is possible that all the user control values will be still filled in when switching between them? For example I fill the user control BaseLayers in, I switch to the user control Spatial Coverage and Flood Maps and when I swith back to the user control BaseLayers everything still have to be filled in.
Create all your controls and hide all of them(Visible=false). Then in Click-Events just set Visible=true for the appropriate control.
Example:
private void toolStripButtonFloodMaps_Click(object sender, EventArgs e)
{
SetChildVisibleForPanel(typeof(FloodMaps), splitContainer1.Panel2);
}
static void SetChildVisibleForPanel(Type visChildType, Panel pnl)
{
if (pnl.Controls.Count==0)
{
//Create your controls with Visible=false;
}
foreach (Control ctl in pnl.Controls)
{
ctl.Visible = (ctl.GetType() == visChildType);
}
}
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;
}
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 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();
}
Please, what is wrong with this:
Form2_Closing:
Form1.DataGridView1.Rows[0].Cells[1].Value = "323";
Error: Index was out of range. Must be non-negative and less than the
size of the collection. Parameter name: index
DGV on Form1 has 10 Rows and 14 Columns
Create a new Winforms Project and add a button & its click handler and a TextBox [make it accessible, such that the child can set value. I have made it public in the designer for now] too. Then add the following code on this Form. Additionally, add a new Form (Form2) in the Project.
private void button1_Click(object sender, EventArgs e)
{
var child = new Form2();
child.FormClosing += new FormClosingEventHandler(ChildFormClosing);
this.Enabled = false;
child.Show(this);
}
void ChildFormClosing(object sender, FormClosingEventArgs e)
{
var child = sender as Form2;
if (child != null)
{
if (child.DialogResult == DialogResult.None)
{
// do data grid view manipulation here
// for ex:
(child.Owner as Form1).textBox1.Text = "Hi";
}
}
Enabled = true;
}
From your comments it looks like you are trying to create a custom Dialog that will manipulate a particular value within a DataGridView on the calling form. I suggest looking at this example of creating a custom message box.
You'll be able to return say the value that you want the DataGridViewCell updated to, then set it on your Form1.