C#: Change DropDownList value when Enter key is pressed - c#

I have a windows form with a DropDownList with a fixed number of items. How do I make the DropDownList increment to the next item when I press Enter and when it reaches the end of the items, return to the first item.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.DataSource = CreateItems();
}
private List<string> CreateItems()
{
List<string> lst = new List<string>();
lst.Add("One");
lst.Add("Two");
lst.Add("Three");
lst.Add("Four");
return lst;
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
if (comboBox1.SelectedIndex == comboBox1.Items.Count-1)
{
comboBox1.SelectedIndex = 0;
return;
}
if (comboBox1.SelectedIndex >=0 & comboBox1.SelectedIndex< comboBox1.Items.Count-1)
{
comboBox1.SelectedIndex = comboBox1.SelectedIndex+1;
}
}
}
}
}

You need to handle the KeyDown event and change the SelectedIndex property.

Related

Showing Combobox 2 List on the selection of items on Combobox 1 (C#. NET)

I have done the below code to display the list corresponding to selection in the first combo box.
But My second list is not getting loaded on the selection of items in first list :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
if (comboBox1.SelectedItem == "weekdays")
{
comboBox2.Items.Add("Sunday");
comboBox2.Items.Add("Monday");
comboBox2.Items.Add("Tuesday");
}
else if (comboBox1.SelectedItem == "year")
{
comboBox2.Items.Add("2012");
comboBox2.Items.Add("2013");
comboBox2.Items.Add("2014");
}
comboBox2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("weekdays");
comboBox1.Items.Add("year");
}
private void Form2_Load(object sender, EventArgs e)
{
comboBox2.Show();
}
}
}
WHat I have done wrong?
i am not sure why do you have two forms, please try following code and see if that works for you.
if (comboBox1.Text == "weekdays")
{
comboBox2.Items.Add("Sunday");
comboBox2.Items.Add("Monday");
comboBox2.Items.Add("Tuesday");
}
else if (comboBox1.Text == "year")
{
comboBox2.Items.Add("2012");
comboBox2.Items.Add("2013");
comboBox2.Items.Add("2014");
}

Trying to use another form's function but not working

I want to disable my form when another opened, and enabled when closed. But another forms may close itselves and open new forms so I wite a function which gets boolean value and enables and disables forms and keys. When opening new for it works but when closed that form not working.
Here is main form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Automation_v1_0_0
{
public partial class FormMain : Form
{
bool keysActivated = true;
public FormMain()
{
InitializeComponent();
}
public void anotherForm(bool anotherform)
{
if (anotherform)
{
this.Enabled = false;
keysActivated = false;
}
else
{
this.Enabled = true;
keysActivated = true;
}
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (keysActivated)
{
if (e.KeyCode == Keys.F7)
{
FormSettings settings = new FormSettings();
anotherForm(true); // Enters and working.
settings .Show();
}
}
}
}
}
This is my settings form:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Automation_v1_0_0
{
public partial class FormAyarlarMenu : Form
{
public FormSettings()
{
InitializeComponent();
}
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
FormMain fm = new FormMain();
fm.anotherForm(false); // Enters but not working.
this.Close();
}
}
}
}
you are making this complex while it can be simple
in your mainform do this :
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F7)
{
using (FormSettings settings = new FormSettings())
{
settings.ShowDialog();
}
}
}
and just drop all other code you dont need that.
Drop the method anotherform, you dont need that.
in your settings form just do this
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
this.Close();
}
}
Main form:
public partial class FormMain : Form
{
bool keysActivated = true;
public FormMain()
{
InitializeComponent();
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (keysActivated)
{
if (e.KeyCode == Keys.F7)
{
this.Hide();
FormSettings settings = new FormSettings(this);
settings.Show();
}
}
}
}
Setting form:
public partial class FormSettings : Form
{
private FormMain formMain;
public FormSettings(FormMain formMain)
{
InitializeComponent();
this.formMain = formMain;
}
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
this.Close();
formMain.Show();
}
}
}
Your problem is that you instantiated new FormMain object instead of using existing one! The solution is that you pass the current instance of FormMain to setting form to use it.
I have solved problem. Here is my code:
namespace Automation_v1_0_0
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F7)
{
FormSettings settings = new FormSettings();
settings.ShowDialog();
}
}
}
}

facing problems with password form in c#

i have two different forms with one form dedicated to password input and if the password is correct open a dialog box for loading file from pc and if wrong a message box appears stating wrong password.
the problem with this is if the program is started and i clicked the button and enter the correct password and succesfully loaded the file. but if again i press the button to enter the password and i close the popup manually by X on the top, i get access to the dialog box window. i am unable to get how to stop this.
my codes are as follows
form 1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Windows;
using System.Threading;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formpopup popup = new formpopup();
popup.ShowDialog();
if (formpopup.j == 1)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
}
}
}
}
another form of password is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class formpopup : Form
{
public formpopup()
{
InitializeComponent();
}
public static int j = 0;
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
if (a == "1234")
{
j = 1;
textBox1.Text = string.Empty;
this.Close();
}
else
{
j = 0;
textBox1.Text = string.Empty;
this.Close();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
yes i also tried to used form.Dispose() command but nothing happens.
You solution want redesigning: what on Earth "j" means?
// Make the class name readable, use upper case (FormPopup instead of formpopup):
public partial class FormPopup : Form {
public FormPopup() {
InitializeComponent();
}
//TODO: rename the button as well as the textbox
private void button1_Click(object sender, EventArgs e) {
if (textBox1.Text == "1234")
DialogResult = System.Windows.Forms.DialogResult.OK;
else
DialogResult = System.Windows.Forms.DialogResult.Cancel;
// In case form was open as non-dialog
Close();
}
}
....
public partial class Form1 : Form {
...
private void button1_Click(object sender, EventArgs e) {
// Wrap IDisposable into using
using (FormPopup dialog = new FormPopup()) {
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return; // wrong password
}
// Wrap IDisposable into using
using (OpenFileDialog fileDialog = new OpenFileDialog()) {
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
//TODO: put relevant code here
}
}
}
}
The problem is that your j is static. Make it non-static, so it would refer to an instance of your form
public int j = 0;
And then you should refer to your form in the way something like that
if (popup.j == 1)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
}

how to add selected item from one listBox to another listBox

i have 16 Items in listBox1 and one button "button1", i need to to be able to move the selected Item from listBox1 to listBox2 when a button is pressed. currently my code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace courseworkmodule
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
String workingDir = Directory.GetCurrentDirectory();
XmlTextReader textReader = new XmlTextReader(workingDir + #"\modules.xml");
Console.WriteLine("BaseURI:" + textReader.BaseURI);
textReader.Read();
while (textReader.Read())
{
textReader.MoveToElement();
if (textReader.Name == "Name")
{
textReader.Read();
XmlNodeType nType = textReader.NodeType;
if (nType == XmlNodeType.Text)
{
listBoxAllModules.Items.Add(textReader.Value);
}
}
}
Console.ReadLine();
textReader.Close();
}
public void button1_Click(object sender, EventArgs e)
{
listBoxStudentModules.Items.Add(listBoxAllModules.SelectedItem);
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void listBoxAllModules_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
where listBoxAllModule is listBox1 and listBoxStudentModule is listBox2
thanks in advance for any help
You can make it explicit to see what is going on:
string value = listBoxAllModules.SelectedItem.Value;
string text = listBoxAllModules.SelectedItem.Text;
ListItem item = new ListItem ();
item.Text = text;
item.Value = value;
listBoxStudentModules.Items.Add(item);
listBoxAllModules.Items is a ListBox.ObjectCollection. You are trying to use it as a method:
listBoxAllModules.Items( listBoxAllModules.SelectedItem )
This will not work. You are missing the Add call. Should be .Items.Add(). You should be able to just add the SelectedItem as TechnologRich shows:
listBoxStudentModules.Items.Add(listBoxAllModules.SelectedItem);

Copy text from listview into another winform

I am really stuck with this so I hope someone can help. I have 2 winforms, one has a listview and another has a textbox. I want to check which item is checked in the listview and copy this text into the second form. I have tried this code but it won't work, any help is greatly appreciated!
// 1st form
private void button5_Click(object sender, EventArgs e) // Brings up the second form
{
Form4 editItem = new Form4();
editItem.Show();
}
public string GetItemValue()
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
return listView1.Items[i].Text;
}
}
return "Error";
}
// 2nd form
private void Form4_Load(object sender, EventArgs e)
{
Form1 main = new Form1();
textBox1.Text = main.GetItemValue();
}
You are creating a new Form1 inside of Form4 after it has been loaded. You need a reference to the original Form1. This can be accomplished in several ways, probably the easiest is passing a reference into the Form4 constructor.
// Form 1
// This button creates a new "Form4" and shows it
private void button5_Click(object sender, EventArgs e)
{
Form4 editItem = new Form4(this);
editItem.Show();
}
public string GetItemValue()
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
return listView1.Items[i].Text;
}
}
return "Error";
}
--
// Form 2 (Form4)
// Private member variable / reference to a Form1
private Form1 _form;
// Form4 Constructor: Assign the passed-in "Form1" to the member "Form1"
public Form4(Form1 form)
{
this._form = form;
}
// Take the member "Form1," get the item value, and write it in the text box
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = this._form.GetItemValue();
}
You only need a couple of changes. No need to add your own way of storing the owner form as this functionality already exists.
private void button5_Click(object sender, EventArgs e) // Brings up the second form
{
Form4 editItem = new Form4();
editItem.Show(this); //passes a reference to this form to be stored in owner
}
Then reference it on the other form.
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = ((Form1)owner).GetItemValue();
}
Try thisway
FORM 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
//Fields
public List<string> itemTexts;
public Form1()
{
InitializeComponent();
//Generate some items
for (int i = 0; i < 10; i++)
{
ListViewItem item = new ListViewItem();
item.Text = "item number #" + i;
listView1.Items.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
if (item.Checked)
{
itemTexts.Add(item.Text);
}
}
Form2 TextBoxForm = new Form2(itemTexts);
TextBoxForm.Show();
}
}
}
FORM 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
//Fields
List<string> itemTexts;
public Form2(List<string> itemTexts)
{
InitializeComponent();
this.itemTexts = itemTexts;
foreach (string text in itemTexts)
{
textBox1.Text += text + Environment.NewLine;
}
}
}
}

Categories