I have 4 panels each of one has their own TextBoxes, Buttons, and DataGridView. My problem is only two panels are showing the other is none. When I click the 1st Button I want to show the panel1 and hide the other panels. And when I click the 2nd Button I want to hide the panel1 and the other panel. How can I do it? Can somebody help me with my problem? It this possible to happen?
private void btnItems_Click(object sender, EventArgs e)
{
if (pnlItems.Visible != true)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
pnlDelivery.Visible = false;
}
}
private void btnCustomers_Click(object sender, EventArgs e)
{
if (pnlCustomer.Visible != true)
{
pnlCustomer.Visible = true;
pnlItems.Visible = false;
pnlPOS.Visible = false;
pnlDelivery.Visible = false;
}
}
private void btnPOS_Click(object sender, EventArgs e)
{
if (pnlPOS.Visible != true)
{
pnlPOS.Visible = true;
pnlCustomer.Visible = false;
pnlItems.Visible = false;
}
}
private void btnDelivery_Click(object sender, EventArgs e)
{
if (pnlDelivery.Visible != true)
{
pnlDelivery.Visible = true;
pnlPOS.Visible = false;
pnlCustomer.Visible = false;
pnlItems.Visible = false;
}
}
private void frmMain_Load(object sender, EventArgs e)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
pnlDelivery.Visible = false;
}
Let's extract a method:
private void MakePanelVisisble(Panel panel) {
Panel[] panels = new Panel[] {
pnlItems, pnlCustomer, pnlPOS, pnlDelivery,
};
foreach (var p in panels)
p.Visible = (p == panel);
}
Then
private void btnItems_Click(object sender, EventArgs e) {
MakePanelVisisble(pnlItems);
}
private void btnCustomers_Click(object sender, EventArgs e) {
MakePanelVisisble(pnlCustomer);
}
...
private void frmMain_Load(object sender, EventArgs e) {
MakePanelVisisble(pnlItems);
}
Just Give your Panels Tags 1,2,3,4;
the write a method like this:
private void ShowPanel(int id)
{
var panels = myform.Controls.OfType<Panel>();
foreach(Panel p in panels)
p.Visible = (int)p.Tag == id)
}
Then in your buttons use it like:
private void btnPOS_Click(object sender, EventArgs e)
{
ShowPanel(2);
}
Related
i have a problem where when i try to add a value trough the click_button event, the value will remain the same, could someone help me out with this?
int parallelgroepnummer = 0;
protected void Page_Load(object sender, EventArgs e)
{
CheckBox1.Checked = false;
checkparallel = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (checkparallel == true)
{
{
parallelgroepen.Add(parallelgroepnummer, "test");
parallelgroepnummer = parallelgroepnummer + 1;
ListBox1.Items.Add("parallele groep " + parallelgroepnummer);
}
}
else
{
ListBox1.Items.Add("notparallel");
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkparallel == false)
{
checkparallel = true;
}
else
{
checkparallel = false;
}
}
the answer always returns 1 here.
int parallelgroepnummer = 0;
change in:
private static int parallelgroepnummer = 0;
how i can make a loop timer that check if in main form topmost.enable is false until a label is visible and then set to true when the label deactive?
If tried this code but not work:
private void InitializeAlive()
{
alive = new System.Timers.Timer();
alive.Interval = 1000;
alive.AutoReset = true;
alive.Elapsed += Alive_Tick;
alive.Start();
}
private void Alive_Tick(object sender, EventArgs e)
{
if (lblPassword.Enabled)
{
this.TopMost = false;
}
else
{
this.TopMost = true;
alive.Dispose();
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
if (txtPassword.Text == pswd)
{
TopMost = false;
webPrintSetting.ShowPageSetupDialog();
InitializeAlive();
}
else
{
btnPrint.Enabled = false;
btnPrint.Visible = false;
lblPassword.Visible = false;
txtPassword.Enabled = false;
txtPassword.Visible = false;
txtPassword.Clear();
}
}
If you only need to do something when 'Enabled' property of the label changes, then you can simply add handler to the 'EnabledChanged' property, like this:
public Form1()
{
InitializeComponent();
lblPassword.EnabledChanged += new System.EventHandler(this.LblPassword_EnabledChanged);
}
And implement the handler like this:
private void LblPassword_EnabledChanged(object sender, EventArgs e)
{
TopMost = !lblPassword.Enabled;
}
I find a solution to toggle on/off topmost (off until a target process is running).
private Timer check;
public MyForm()
{
InitializeCheck();
}
private void InitializeCheck()
{
check = new Timer();
check.Interval = 5000;
check.Tick += Check_Tick;
check.Enabled = false;
}
private void Check_Tick(object sender, EventArgs e)
{
CheckProgram();
}
private void CheckProgram()
{
Process[] program = rocess.GetProcessesByName("notepad");
if (program.Length == 0)
{
check.Enabled = false;
TopMost = true;
}
private void button1_Click(object sender, EventArgs e)
{
TopMost = false;
check.Enabled = true;
}
i have two ListBoxAdv1 and ListBoxAdv2 and i want sync them scroll
i use this code and just scroll up or down but didn't update showing items in other ListBoxAdv
what should i do?
please help
i try this:
private void listBoxAdv1_Scroll(object sender, ScrollEventArgs e)
{
listBoxAdv2.VScrollBar.Value = listBoxAdv1.VScrollBar.Value;
}
private void listBoxAdv2_Scroll(object sender, ScrollEventArgs e)
{
listBoxAdv1.VScrollBar.Value = listBoxAdv2.VScrollBar.Value;
}
and this:
private void listBoxAdv1_Scroll(object sender, ScrollEventArgs e)
{
listBoxAdv2.Focus();
ScrollEventArgs scrollEventArgs = new ScrollEventArgs(ScrollEventType.SmallIncrement, e.OldValue, e.NewValue, ScrollOrientation.VerticalScroll);
listBoxAdv2_Scroll(listBoxAdv2, scrollEventArgs);
}
private void listBoxAdv2_Scroll(object sender, ScrollEventArgs e)
{
}
thanks for many responses!
i find solution:
bool Scrolling = true;
private void listBoxAdv1_Scroll(object sender, ScrollEventArgs e)
{
if (Scrolling == true)
{
Scrolling = false;
listBoxAdv2.BeginUpdate();
listBoxAdv2.AutoScrollPosition = new Point(listBoxAdv1.AutoScrollPosition.X, listBoxAdv1.AutoScrollPosition.Y);
listBoxAdv2_Scroll(sender, e);
listBoxAdv2.EndUpdate();
Scrolling = true;
}
}
private void listBoxAdv2_Scroll(object sender, ScrollEventArgs e)
{
if (Scrolling == true)
{
Scrolling = false;
listBoxAdv1.BeginUpdate();
listBoxAdv1.AutoScrollPosition = new Point(listBoxAdv2.AutoScrollPosition.X, listBoxAdv2.AutoScrollPosition.Y);
listBoxAdv1_Scroll(sender, e);
listBoxAdv1.EndUpdate();
Scrolling = true;
}
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 4 years ago.
I am trying to get read data from rs 485 communication interface and write in the textbox but i am not getting the data from this code. I am working on water measurement and I am beginner in c#. I have seen similar question like this but unable to get the answer. The data format is like this. D014802,+000.042,+000.082,003680,+000805.66,+025.25,0193FA,0.99697,0000,B7C9
Help me out .
public partial class MainForm : Form
{
SerialPort aSerialPort;
InputRegister mobjGlobalform2;
Form3 mobjGlobalform3;
LoginForm AdminLogin = new LoginForm();
//Form6 softwareVersion = new Form6();
bool isUserMode = true;
public MainForm()
{
InitializeComponent();
getAvailablePorts();
}
private void btn_close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
InputRegister mobjform2 = new InputRegister();
if (checkBox1.Checked)
{
mobjGlobalform2 = mobjform2;
mobjform2.Show();
if(isUserMode==true)
{
mobjform2.groupbox_form2Takuwa.Hide();
mobjform2.Height = 215;
mobjform2.Width = 575;
}
}
else
{
mobjGlobalform2.Close();
//mobjform2.Close();
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
Form3 mobjform3 = new Form3();
if (checkBox2.Checked)
{
mobjGlobalform3 = mobjform3;
mobjform3.Show();
if(isUserMode==true)
{
mobjform3.groupbx_form3takuwamode.Hide();
mobjform3.Height = 254;
mobjform3.Width = 407;
}
}
else
{
mobjGlobalform3.Close();
//mobjform2.Close();
}
}
private void timer1_Tick_1(object sender, EventArgs e)
{
label13.Text = DateTime.Now.ToString();
// splitContainer1.Panel2.Controls.Add(label13);
if(AdminLogin.isAdminMode)
{
lbl_AdminLogout.Show();
isUserMode = false;
}
}
private void btn_Clear_Click(object sender, EventArgs e)
{
txtbox_ShowData.Text = String.Empty; // clear the data from text box
}
private void MenuStrip_AdminLogin_Click(object sender, EventArgs e)
{
//Form5 mobjform5 = new Form5();
AdminLogin.Show();
}
private void lbl_AdminLogout_Click(object sender, EventArgs e)
{
AdminLogin.isAdminMode = false;
isUserMode = true;
lbl_AdminLogout.Hide();
MessageBox.Show("Logout Successful");
}
private void btn_SoftwareVersion_Click(object sender, EventArgs e)
{
//softwareVersion.Show();
Form6 Versionform6 = new Form6();
Versionform6.Height = 272;
Versionform6.Width = 507;
Versionform6.Show();
}
private void btn_connectaddress_Click(object sender, EventArgs e)
{
Setting addressconnectform6 = new Setting();
addressconnectform6.Show();
if(isUserMode == true)
{
addressconnectform6.groupbx_takuwaform7.Hide();
addressconnectform6.Height = 257;
addressconnectform6.Width = 657;
}
else
{
addressconnectform6.groupbx_takuwaform7.Show();
}
}
#region combox
// port show in combobox
private void getAvailablePorts()
{
string[] ports = SerialPort.GetPortNames();
cbbx_comport.Items.Clear();
foreach (string comport in ports)
{
cbbx_comport.Items.Add(comport);
}
}
#endregion
private void btn_Connect_Click(object sender, EventArgs e)
{
initializeSensor();
aSerialPort.DataReceived += new SerialDataReceivedEventHandler(Rs485DataReceivedEventHandler);
}
private void Rs485DataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sData = sender as SerialPort;
string recvData = sData.ReadLine();
this.Invoke((MethodInvoker)delegate { DataReceived(recvData); });
}
private void initializeSensor()
{
aSerialPort = new SerialPort(cbbx_comport.Text);
aSerialPort.BaudRate = 38400;
aSerialPort.Parity = Parity.None;
aSerialPort.StopBits = StopBits.One;
aSerialPort.DataBits = 8;
if (aSerialPort.IsOpen == false)
{
try
{
aSerialPort.Open();
//aSerialPort.WriteLine("c"); //clear
//aSerialPort.WriteLine("o");
}
catch { }
}
}
private void DataReceived(string recvData)
{
txtbx_sensorData.Text = recvData;
}
You are instantiating a local variable, not the field.
Instead of
SerialPort aSerialPort = new SerialPort(cbbx_comport.Text);
Try
aSerialPort = new SerialPort(cbbx_comport.Text);
How do I prevent firing CheckedChanged event when checking a control programmatically?
I usually do this the following way.
private bool isFrozen = false;
private void btn1_CheckedChanged(object sender, EventArgs e)
{
if (isFrozen)
return;
isFrozen = true;
btn2.Checked = false;
isFrozen = false;
// Do some stuff
}
private void btn2_CheckedChanged(object sender, EventArgs e)
{
if (isFrozen)
return;
isFrozen = true;
btn1.Checked = false;
isFrozen = false;
// Do another stuff
}
Is there a better or more common solution?
I think your way is fine.
The other way to do it is remove the EventHandler before the check, and then add it back again after the check. This way eliminates the need for the isFrozen variable.
private void btn1_CheckedChanged(object sender, EventArgs e)
{
btn2.CheckedChanged -= btn2_CheckedChanged;
btn2.Checked = false;
btn2.CheckedChanged += btn2_CheckedChanged;
// Do some staff
}
private void btn2_CheckedChanged(object sender, EventArgs e)
{
btn1.CheckedChanged -= btn1_CheckedChanged;
btn1.Checked = false;
btn1.CheckedChanged += btn1_CheckedChanged;
// Do another staff
}
In VB:
RemoveHandler btn2.CheckedChanged, AddressOf btn2_CheckedChanged
btn2.Checked = false
AddHandler btn2.CheckedChanged, AddressOf btn2_CheckedChanged
I came across this post after wanting to implement something like this for a while. I regularly use Measurement Studio from National Instruments, and their WinForms controls that have the event StateChanging or StateChanged pass a parameter of type ActionEventArgs, which has a property Action which can take three values: ByKeyboard, ByMouse and Programatic. This is very useful in determining what has caused the state of the control to change. I wanted to replicate this in a standard WinForms checkbox.
Here is my code:
public enum ControlSource
{
Programatic,
ByKeyboard,
ByMouse
}
public class AwareCheckBox : Checkbox
{
public AwareCheckBox()
: base()
{
this.MouseDown += AwareCheckbox_MouseDown;
this.KeyDown += AwareCheckbox_KeyDown;
}
private ControlSource controlSource = ControlSource.Programatic;
void AwareCheckbox_KeyDown(object sender, KeyEventArgs e)
{
controlSource = ControlSource.ByKeyboard;
}
void AwareCheckbox_MouseDown(object sender, MouseEventArgs e)
{
controlSource = ControlSource.ByMouse;
}
public new event AwareControlEventHandler CheckedChanged;
protected override void OnCheckedChanged(EventArgs e)
{
var handler = CheckedChanged;
if (handler != null)
handler(this, new AwareControlEventArgs(controlSource));
controlSource = ControlSource.Programatic;
}
}
public delegate void AwareControlEventHandler(object source, AwareControlEventArgs e);
public class AwareControlEventArgs : EventArgs
{
public ControlSource Source { get; private set; }
public AwareControlEventArgs(ControlSource s)
{
Source = s;
}
}
I'm sure there are improvements to make, but my rudimentary testing has demonstrated that it works. I have posted here simply in case others stumble across this issue and want a clearer way of distinguishing where the change was initiated. Any comments welcome.
Just have a counter value set and check for the value in the beginning of the event. It solved my problem in 10 minutes. I am using 5 slide buttons in Xamarin to make it as a radio button.
private void testtoggle1(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw2.IsToggled= false;
sw3.IsToggled = false;
sw4.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle2(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw3.IsToggled = false;
sw4.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle3(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw2.IsToggled = false;
sw4.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle4(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw2.IsToggled = false;
sw3.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle5(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw2.IsToggled = false;
sw3.IsToggled = false;
sw4.IsToggled = false;
chk_ctr = 0;
}