I'm using two buttons for "on" and "off". When I press "On" button, it passes a string value "1", and pressing the "Off" button passes the string value "2".
How can I do this within one button? By default button should be off, when I press it should on and when i press again it should off.
public partial class _Default : System.Web.UI.Page
{
SerialPort ardo;
protected void Page_Load(object sender, EventArgs e)
{
ardo = new SerialPort();
ardo.PortName = "COM5";
ardo.BaudRate = 9600;
}
protected void Button1_Click(object sender, EventArgs e)
{
string stop = "1";
ardo.Open();
ardo.Write(stop);
ardo.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string On = "2";
ardo.Open();
ardo.Write(On);
ardo.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//consider initially its stopped
if(Session[“currentState”] == null)
Session[“currentState”] = “1”;
if(Session[“currentState”].ToString() == “1”)
{
Session[“currentState”]= “2”;
}
else
{
Session[“currentState”]= “1”;
}
ardo.Open();
ardo.Write(Session[“currentState”].ToString());
ardo.Close();
}
it set to hidden text box.
then you can check if statement it is pressed or not
protected void Button1_Click(object sender, EventArgs e)
{
// hf means hidden textbox
if(hf.text==1){
// your on code
hf.text=0;
}
if(hf.text==0){
//your off code
hf.text=1;
}
}
Declare a string variable
private string isOn = "2";
and then on the button click event you can handle it like
protected void Button1_Click(object sender, EventArgs e)
{
if(isOn.Equals("2"))
isOn = "1";
else
isOn = "2";
}
Related
I designed a dashboard form that contains buttons. Some of these buttons have sub-menus. Since I'm planning to limit the access to some sub-menus, I want that every time a user clicks a restricted sub-menu, a form will appear that will prompt the user to enter the password before accessing the restricted page.
I already made a password form and programmed that when a sub-menu is clicked, the password form will appear. However, once the correct password is submitted, it will look like this:
I want that it will look like this when password is correct:
I want the overlapping form placed on the original position / panel. How to do that?
Here's the code for the dashboard:
public CFMenu()
{
InitializeComponent();
customizeDesign();
}
private void customizeDesign()
{
panelInventory.Visible = false;
panelSales.Visible = false;
}
private void hideSubMenu()
{
if(panelInventory.Visible == true)
panelInventory.Visible = false;
if(panelSales.Visible == true)
panelSales.Visible = false;
}
private void showSubMenu(Panel subMenu)
{
if(subMenu.Visible == false)
{
hideSubMenu();
subMenu.Visible = true;
}
else
subMenu.Visible = false;
}
private void CFMenu_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'casaFrancaDataSet.Sales' table. You can move, or remove it, as needed.
this.salesTableAdapter.Fill(this.casaFrancaDataSet.Sales);
timer1.Start();
label4.Text = DateTime.Now.ToLongTimeString();
label5.Text = DateTime.Now.ToLongDateString();
}
private void btnInventory_Click(object sender, EventArgs e)
{
showSubMenu(panelInventory);
}
private void btnSales_Click(object sender, EventArgs e)
{
showSubMenu(panelSales);
}
private void button1_Click(object sender, EventArgs e)
{
openPanelForm(new CFInventoryAdd());
hideSubMenu();
}
private void button2_Click(object sender, EventArgs e)
{
openPanelForm(new PasswordInventView());
hideSubMenu();
//string password = Interaction.InputBox("Enter Password: ", "Inventory View");
//if(password == "hello")
//{
// openPanelForm(new CFInventoryView());
// hideSubMenu();
//}
//else
//{
// MessageBox.Show("Incorrect Password!");
//}
}
private void button3_Click(object sender, EventArgs e)
{
openPanelForm(new CFInventoryUpdate());
hideSubMenu();
}
private void button7_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesAdd());
hideSubMenu();
}
private void button6_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesView());
hideSubMenu();
}
private void button8_Click(object sender, EventArgs e)
{
openPanelForm(new CFCalculate());
hideSubMenu();
}
private void button5_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesUpdate());
hideSubMenu();
}
private void button9_Click(object sender, EventArgs e)
{
openPanelForm(new CFReport());
hideSubMenu();
}
private void timer1_Tick(object sender, EventArgs e)
{
label4.Text = DateTime.Now.ToLongTimeString();
timer1.Start();
}
private Form activeForm = null;
private void openPanelForm(Form childForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelForm.Controls.Add(childForm);
panelForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
Here's a sub-menu that requires a password:
private void button2_Click(object sender, EventArgs e)
{
openPanelForm(new PasswordInventView());
hideSubMenu();
}
Here's the code for the Password Prompt form:
public partial class PasswordInventView : Form
{
public PasswordInventView()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var password = txtPassword.Text;
if (password == "1234")
{
CFInventoryView f2 = new CFInventoryView();
f2.Show();
this.Visible = false;
}
else
{
MessageBox.Show("Username or Password is incorrect!");
txtPassword.Clear();
}
}
Before making the password form, I tried using the InputDialog from VisualBasic by referencing it on my project. I liked it however I can't set the font of the input, and I wanted that my password will appear as ******** when typed. That's why I made my own password form.
public partial class PasswordInventView : Form
Change form to UserControl.
public partial class PasswordInventView : UserControl
And create user control in main form.
If this not works go create user control and copy PasswordInventView to UserControl.
Ctrl + Alt + L > Right click project > Add > New Item > Select User Control (Windows Forms) > Add. Copy chield form to user control. And create user control in main form.
Search about user control.
This is my code:
IN Parent form:
public void tokenform_Load(object sender, EventArgs e)
{
tbvarchecker.Text = HelpForm.code;
}
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
HelpForm help = new HelpForm();
if (e.KeyCode == Keys.F2)
{
help.Show();
}
}
public void setvalues(string cd)
{
tbvarchecker.Text = cd;
label10.Text = cd;
}
Child Form code:
private void dgv(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewRow row = dgvshowallfields.Rows[e.RowIndex];
code = row.Cells[0].Value.ToString();
code1 = row.Cells[0].Value.ToString();
this.Close();
}
private void HelpForm_FormClosed(object sender, FormClosedEventArgs e)
{
tokenform tkf = new tokenform();
tkf.setvalues(code);
tkf.setvalues(code1);
tkf.tokenform_Load(sender,e);
}
//private void HelpForm_FormClosing(object sender, FormClosingEventArgs e)
//{
// tkf.setvalues(code);
//}
This code is declared as public static string code, while the code1 is just string.
I have also checked through breakpoints and the value reaches the function in Parent form function set values but still, I couldn't show it in the textbox.
Guys can anyone help me out.....??
First, pass the parent form to your Show() call.
Change:
help.Show();
To:
help.Show(this);
Then, in the child form, you can cast the .Owner property to your parent form type and call its methods:
private void HelpForm_FormClosed(object sender, FormClosedEventArgs e)
{
tokenform tkf = this.Owner as tokenform;
if (tkf != null)
{
tkf.setvalues(code);
tkf.setvalues(code1);
tkf.tokenform_Load(sender,e);
}
}
MY CODE:
namespace textbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox2.Text += "1";
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
In your button1_Click method, the textBox1.Text is attributed the result of concatenating "1" to textBox2.Text. I think this is the cause of your problem.
I am creating a To Do List using Windows Forms.
This is what I have so far.
The code for this form is as follows
namespace To_Do_List
{
public partial class To_Do_List : Form
{
public To_Do_List()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) //this is the exit button on the toolstrip
{
Application.Exit(); //exits the program when the Exit button is clicked in dropdown menu
}
private void button4_Click(object sender, EventArgs e)//This creates the button to open the about form
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form
}
private void button3_Click(object sender, EventArgs e) //This creates the button to open the form which ammends tasks
{
AmmendItem CreateForm = new AmmendItem(this);
CreateForm.Show();
}
private void button1_Click(object sender, EventArgs e) // This creates the button to open the form which creates new tasks
{
AddItem CreateForm = new AddItem(this);
CreateForm.Show();
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e) // This creates the table
{
}
private void button2_Click(object sender, EventArgs e) //This Allows the user to delete entries
{
if (listView1.SelectedItems != null)
{
var confirmation = MessageBox.Show(
"Are you sure you want to delete this?",
"WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Question
);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.Items.Count - 1; i >= 0; i--)
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
i--;
}
}
}
}
}
}
}
To add a task, the form looks like this
And again, the code is as follows
namespace To_Do_List
{
public partial class AddItem : Form
{
To_Do_List home;
public AddItem(To_Do_List parent)
{
InitializeComponent();
home = parent;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void openListToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sortByDateToolStripMenuItem_Click(object sender, EventArgs e)
{
}
public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
public void textBox1_TextChanged(object sender, EventArgs e)//Title Box
{
}
public void /*Description of Task*/richTextBox1_TextChanged(object sender, EventArgs e)
{
}
public void /*Priority Box*/comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close(); //causes the window to close but keeps the application running
}
private void aboutToDoListToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form displaying copyright information
}
public void button1_Click(object sender, EventArgs e) //create task button
{
ListViewItem item1 = new ListViewItem(Title.Text);
item1.SubItems.Add(Description.Text);
item1.SubItems.Add(Priority.Text);
item1.SubItems.Add(Date.Text);
string value = "";
bool isChecked = Completed.Checked;
if (isChecked)
item1.SubItems.Add(Completed.Text);
else
value = null;
home.listView1.Items.Add(item1);
this.Close();
}
private void Date_ValueChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Closes the window
{
this.Close();
}
}
}
The Amend form is exactly the same as the New Task form. How would I go about being able to select a record, pressing the amend button and actually changing?
I'm pretty stumped.
Also, this isn't really part of this question but I'll ask it just in case someone knows.
How would I go about being able to actually save these records so that they are there when I open the application back up again?
Thank you very much for reading, I know that I have just dumped everything but I'm really new to Windows Forms so I didn't want to accidentally miss something important out.
Edit
Am I on the right road with something like this?
public void button1_Click(object sender, EventArgs e)
{
To_Do_List editform = new To_Do_List(Title.Text);
editform.Description.Text = listView1.SelectedItems[0].SubItems[0].Text;
Still can't get it to work :/
This code for popup window.
public partial class frmToDoDetails : Form
{
public string TaskTitle { get; set; }
public string Description { get; set; }
public bool EditMode { get; set; }
public frmToDoDetails()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
TaskTitle = txtTitle.Text;
Description = txtDesc.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void frmToDoDetails_Load(object sender, EventArgs e)
{
if (EditMode)
{
txtTitle.Text = TaskTitle;
txtDesc.Text = Description;
}
else {
txtTitle.Text = string.Empty;
txtDesc.Text = string.Empty;
}
txtTitle.Focus();
}
}
And this for list
public partial class frmToDo : Form
{
public frmToDo()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
frmToDoDetails frm = new frmToDoDetails();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] arr = new string[2];
ListViewItem itm;
arr[0] = frm.TaskTitle;
arr[1] = frm.Description;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
private void frmToDo_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
//Add column header
listView1.Columns.Add("Title", 100);
listView1.Columns.Add("Desc", 70);
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
ListViewItem currentItem= listView1.SelectedItems[0];
frmToDoDetails frm = new frmToDoDetails();
frm.TaskTitle = currentItem.SubItems[0].Text;
frm.Description = currentItem.SubItems[1].Text;
frm.EditMode = true;
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentItem.SubItems[0].Text=frm.TaskTitle;
currentItem.SubItems[1].Text=frm.Description;
}
}
}
I am not added your complete fields(priority, date,etc..).
I hope it will help you.
When I attempt to have CheckBoxList1.SelectedItem.Value, it gives me errors. I am a bit confused and looking through this Murach ASP.Net book but I do not see any answers. I am wanting to have what is selected in my ListBox and what is selected in my CheckBox and have them added. Any help?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int ucost = int.Parse(DropDownList1.SelectedItem.Value);
int ccost = int.Parse(//what does in here???);
ttbox.Text = ccost.ToString();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
ttbox.Text = "";
}
protected void ttbox_TextChanged(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
This is a an update. They say a picture is worth a thousand words.
If you want to get all selected checkbox, you can use:
var selected = CheckBoxList1.Items.Cast<ListItem>().Where(x => x.Selected).ToList();
If you want to get only count of selected:
var count = CheckBoxList1.Items.Cast<ListItem>().Count(x => x.Selected)
If you want to get last or first element, you can use First or Last linq methods.