how can disable a textbox in class asp.net - c#

private void txtenable (Boolean txtenable)
{
if(txtenable== false)
{
txtname.Enabled = false;
txtTel.Enabled = false;
txtmobile.Enabled = false;
txtAdress.Enabled = false;
}
else
{
txtname.Enabled = true;
txtTel.Enabled = true;
txtmobile.Enabled = true;
txtAdress.Enabled = true;
}
}
I want use this class but i can not call textboxes. How can call textbox in class?

First you need to be able to accept the TextBox objects within the class, then you can manipulate them how you see fit. I haven't actually tried this, but this is how I would go about setting it up.
public class YourClass
{
TextBox txtName;
TextBox txtTel;
TextBox txtMobile;
TextBox txtAddress;
private void txtenable (Boolean txtenable, TextBox txtName, TextBox txtTel, TextBox txtMobile, TextBox txtAddress)
{
if(txtenable== false)
{
txtName.Enabled = false;
txtTel.Enabled = false;
txtMobile.Enabled = false;
txtAddress.Enabled = false;
}
else
{
txtName.Enabled = true;
txtTel.Enabled = true;
txtMobile.Enabled = true;
txtAddress.Enabled = true;
}
}
In order for you to access the textboxes from within your class you will need to pass them such as:
public class OtherClassContainingTextBoxes
{
private void SomeEvent(object sender, EventArgs e){
txtenable(true, txtName, txtTel, txtMobile, txtAddress);
}
However, based on the example provided, I am unsure why you wouldn't do this in a method within the class you have your textboxes.
You could do something on pageload:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["enable"] == false){
txtenable(false);
}else{
txtenable(true);
}
}
private void txtenable (Boolean txtenable)
{
if(txtenable== false)
{
txtName.Enabled = false;
txtTel.Enabled = false;
txtMobile.Enabled = false;
txtAddress.Enabled = false;
}
else
{
txtName.Enabled = true;
txtTel.Enabled = true;
txtMobile.Enabled = true;
txtAddress.Enabled = true;
}
}

Related

Enable and Disable buttons, labels based on some conditions

I have 5 buttons and 5 labels next to each button. When i run the app i expect the first button to be enabled and the the rest disabled and greyed out with the labels. after i click the first button it should disable with the label and enable the second button, and so forth with all the other buttons.
this way is to long, is there a better way of doing this?
private void Form1_Load(object sender, EventArgs e)
{
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
private void btn1_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = true;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = true;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
private void btn2_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = true;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = true;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
}
private void btn3_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = true;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = true;
lblStep5.Enabled = false;
}
private void btn4_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = true;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = true;
}
private void btn5_Click(object sender, EventArgs e)
{
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
Let all these buttons and labels are inside a container(if it doesn't mean that you can use this.Controls as well if the form contains these buttons and labels only). Let it be pnlContainer, Now you can try something like this:
public void ButtonController(Button buttonToEnable, Label labelToenable)
{
foreach (Control ctrl in panel1.Controls)
{
if (ctrl == buttonToEnable || ctrl == labelToenable)
{
ctrl.Enabled = true;
}
else
{
ctrl.Enabled = false;
}
}
}
So in Form1_Load you want to enable btn1 and lblStep1 so the call should be :
ButtonController(btn1,lblStep1);
For btn1_Click the method call will be like ButtonController(btn2,lblStep2);. in short, you can pass the button and label that you want to enable to this method, which will disable rest of controls in the container.

clear each thing after doing achieving a specific task

i am working on an software in which i want to clear each thing after doing achieving a specific task, like clearing all the fields, list view etc etc and i've to write the a lot of things to be cleared off, like the code of my new button is:
private void btnNew_Click(object sender, EventArgs e)
{
txtProductCode1.ReadOnly = false;
txtInvoiceNo.ReadOnly = false;
cmbCustoemerType.Enabled = false;
cmbCustomerName.Enabled = false;
button1.Enabled = true;
txtProductCode1.Text = null;
txtWageName.Text = null;
txtWageCost.Text = null;
btnFirst.Visible = false;
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
cmbProductName.Enabled = true;
txtQty.ReadOnly = false;
txtPercant.ReadOnly = false;
txtDiscount.ReadOnly = false;
cmbCustoemerType.Enabled = true;
cmbCustomerName.Enabled = true;
button5.Enabled = true;
btnDelete.Enabled = true;
dtp1.Enabled = true;
btnSave.Text = "&Save";
cmbCustoemerType.Text = null;
cmbCustomerName.Text = null;
txtInvoiceNo.Clear();
txtDiscount.Clear();
txtGrandTotal.Clear();
txtProductName.Clear();
txtSalePrice.Clear();
txtQty.Clear();
txtTotal.Clear();
txtExtraWages.Text = null;
lvExtraWages.Items.Clear();
lvTransaction.Items.Clear();
lblCount.Text = null;
lblNetTotal.Text = null;
txtPercant.Text = null;
txtInvoiceNo.Text = invoice.ToString();
}
is there any way to get rid of writing the code again n again, like i need to enable one thing which i've disabled here on another button so i have to write reverse code of this to achieve the task and it's really annoying! is there any other way to do this?
EDIT:
tried following code:
private void btnNew_Click(object sender, EventArgs e)
{
//using setboxdefault function
SetTextBoxDefaults();
cmbCustoemerType.Text = null;
cmbCustomerName.Text = null;
cmbCustoemerType.Enabled = false;
cmbCustomerName.Enabled = false;
cmbProductName.Enabled = true;
cmbCustoemerType.Enabled = true;
cmbCustomerName.Enabled = true;
button5.Enabled = true;
btnDelete.Enabled = true;
button1.Enabled = true;
btnFirst.Visible = false;
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
btnSave.Text = "&Save";
lvExtraWages.Items.Clear();
lvTransaction.Items.Clear();
lblCount.Text = null;
lblNetTotal.Text = null;
dtp1.Enabled = true;
txtInvoiceNo.Text = invoice.ToString();
}
private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
TextBox.Clear();
}
}
public IEnumerable<Control> GetControls(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControls(ctrl, type)).Concat(controls).Where(c => c.GetType() == type);
}
but it's giving error on TextBox.Clear(); , error is Error 4
An object reference is required for the non-static field, method, or property 'System.Windows.Forms.TextBoxBase.Clear()'
Please let me know where i am mistaking..!!
As Tim has suggested, you can iterate though the controls and set each one accordingly..
public IEnumerable<Control> GetControls(Control control,Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControls(ctrl,type)).Concat(controls).Where(c => c.GetType() == type);
}
private void btnNew_Click(object sender, EventArgs e)
{
SetComboBoxDefaults();
SetTextBoxDefaults();
SetButtonDefaults();
}
private void SetComboBoxDefaults()
{
var comboBoxes = GetControls(this, typeof(ComboBox));
foreach (var comboBox in comboBoxes)
{
((ComboBox)comboBox).Enabled = false;
((ComboBox)comboBox).Text = null;
}
}
private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
((TextBox)textBox).Clear();
}
}
private void SetButtonDefaults()
{
var buttons = GetControls(this, typeof(Button));
foreach (var button in buttons)
{
((Button)button).Visible = false;
if (button.Name == "btnSave") ((Button)button).Text = "&Save";
}
}
You can also have separate GetControl methods so that it returns a specific type of Control, reducing the need to cast to derived types.
update for edit:
You are using the TextBox type instead of the variable name in the foreach loop. Update it to use textBox (lowercase), not TextBox (pascal) see below:
private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
((TextBox)textBox).Clear();
}
}
Remember to stay DRY
Don't
Repeat
Yourself
Partition the reset logic into suitable, small methods that reset a logical grouping of controls. Then invoke those methods where and as needed.
What I have done in several such cases is this:
void SetTextBoxReadOnly(bool val, param TextBox[] textBoxes)
{
if(textBoxes != null && textBoxes.Length > 0)
{
foreach(TextBox textBox in textBoxes)
{
if(textBox != null)
{
textBox.ReadOnly = val;
}
}
}
}
void SetControlEnabled(bool val, param Control[] ctrls)
{
if(ctrls != null && ctrls.Length > 0)
{
foreach(Control ctrl in ctrls)
{
if(ctrl != null)
{
ctrl.Enabled = val;
}
}
}
}
// etc set of methods for similar situations.
// there methods can be invoked as
...
...
...
SetTextBoxReadOnly(true, txtProductCode1,
txtInvoiceNo,
txtQty,
txtPercant,
txtDiscount);

shivery effect of the GUI prepared using C# windows forms

For file transfer between the client and server systems we have a GUI designed on the client system using C# windows forms. We have added a lot of background images and text boxes to make the GUI impressive. But whenever we execute the client code, the GUI takes a few seconds to load itself and be stable , before a choice from the GUI can be made. This happens after every request made from the client side GUI.(its like all the form elements vanish and appear again in a short span of time ). Please help with this. May i know the reason for it.
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.Diagnostics;
using System.IO;
namespace Client405
{
public partial class RequestForm : Form
{
#region Variables
internal static ListBox FileListBox;
internal static byte[] RecievedFile = new byte[46000000];
internal static string StoragePath = string.Empty;
internal static bool CacheSelected = false;
internal static string ChosenTech = "Default";
internal static bool EnableCompression = false;
//internal static string FileType;
internal static string SelectedFile = string.Empty;
internal static Stopwatch watch = new Stopwatch();
#endregion
public RequestForm()
{
InitializeComponent();
this.Load += new EventHandler(RequestForm_Load);
this.DoubleBuffered = true;
}
void RequestForm_Load(object sender, EventArgs e)
{
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
FileListBox = new ListBox();
FileName_textBox.Enabled = true;
FileName_textBox.ReadOnly = true;
if (!Connections.SocketConnected())
{
Connections.EstablishConnectionWithServer();
}
if (LoginAuthenticator.registered)
{
EnableCompression = true;
CacheSelected = true;
RegisterlinkLabel.Visible = false;
HighRB.Enabled = true;
LowRB.Enabled = true;
MediumRB.Enabled = true;
CacheMsg_label.Text = "Caching Enabled";
}
else if (!LoginAuthenticator.registered)
{
RegisterlinkLabel.Visible = true;
RegisterlinkLabel.Show();
Logout_Label.Text = "Home Page";
Cache_checkBox.CheckState = CheckState.Unchecked;
Cache_checkBox.Enabled = false;
EnableCompr_checkBox.Enabled = false;
HighRB.Enabled = false;
LowRB.Enabled = false;
MediumRB.Enabled = false;
DefaultRB.Visible = false;
NoneRB.Visible = false;
DefaultRB.Enabled = false;
NoneRB.Enabled = false;
CacheMsg_label.Text = "Caching Disabled";
}
ChosenTech = "None";
LogUser_Label.Text = LogUser_Label.Text + LoginAuthenticator.myCurrentUser;
SetClientInfo();
}
private void SetClientInfo()
{
//LogUser_Label.Text = LoginAuthenticator.myCurrentUser;
HostValue_Label.Text = HelperInfo.HostNameValue;
//Add port info
Port_Value.Text = HelperInfo.PortNumber;
//Add IP address
IPAddr_value.Text = HelperInfo.IPAddress_client;
//Add Gateway info
Gateway_value.Text = HelperInfo.Default_Gateway;
//Add the ip address of server connected to
ServerIP_Value_Label.Text = HelperInfo.Connected_To(Connections.myClientSocket);
if (Connections.myClientSocket.Connected)
{
ONOFF_radio.Text = "Online";
}
else
{
ONOFF_radio.Text = "Offline";
}
}
private void GetFile_Button_Click(object sender, EventArgs e)
{
if ((AudioRB.Checked == false) && (ImageRB.Checked == false) && (TextRB.Checked == false))
{
MessageBox.Show("Please select a File Type");
return;
}
if (SelectedFile == string.Empty)
{
MessageBox.Show("Please Select a File");
return;
}
if (!LoginAuthenticator.registered)
{
FileTransfer.UnRegisteredTransfer();
this.Refresh();
MessageBox.Show("Transaction Completed");
}
if (LoginAuthenticator.registered)
{
DialogResult trans_reqd = DialogResult.No;
//check Cache
if (Cache_checkBox.CheckState == CheckState.Checked)
{
trans_reqd = FileCache.TransactionQuery(RequestForm.SelectedFile);
if (trans_reqd == DialogResult.No)
{
string[] files = { System.IO.Path.Combine(#"D:\\FT\\Cache", RequestForm.SelectedFile), System.IO.Path.Combine(#"D:\\FT\\Downloads", RequestForm.SelectedFile) };
if (System.IO.File.Exists(files[1]))
{
System.IO.File.Delete(files[1]);
}
System.IO.File.Copy(files[0], files[1]);
watch.Stop();
}
else //if not in cacheF
{
FileTransfer.RegisteredTransfer();
}
}
else //if caching not enabled
{
FileTransfer.RegisteredTransfer();
}
this.Refresh();
MessageBox.Show("Transaction Completed");
}
}
private void AudioRB_CheckedChanged(object sender, EventArgs e)
{
ChosenTech = "Default";
if (AudioRB.Checked == true)
{
ImageRB.Checked = false;
TextRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Visible = false;
EnableCompr_checkBox.Enabled = false;
FileListBox.Hide();
DisplayRadioButtons();
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
FileTransfer.FileType = "Audio";
//ViewFiles_button.Visible = true;
ViewFiles();
}
else if (AudioRB.Checked == false)
{
//ViewFiles_button.Visible = false;
EnableCompr_checkBox.Visible = true;
EnableCompr_checkBox.Enabled = true;
FileListBox.Hide();
}
}
private void ViewFiles()
{
FileTransfer.GetFileNames();
this.Controls.Add(RequestForm.FileListBox);
RequestForm.FileListBox.BringToFront();
RequestForm.FileListBox.Visible = true;
RequestForm.FileListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
//RequestForm.SelectedFile = FileTransfer.FileListView.SelectedItems[0].Text;
//FileName_textBox.Text = RequestForm.SelectedFile;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FileName_textBox.Text = FileListBox.SelectedItem.ToString();
RequestForm.SelectedFile = FileName_textBox.Text;
}
private void ImageRB_CheckedChanged(object sender, EventArgs e)
{
ChosenTech = "Default";
if (ImageRB.Checked == true && LoginAuthenticator.registered)
{
AudioRB.Checked = false;
TextRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = true;
EnableCompr_checkBox.Visible = true;
FileListBox.Hide();
FileTransfer.FileType = "Image";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
DisplayRadioButtons();
ViewFiles();
//ViewFiles_button.Visible = true;
}
else if (ImageRB.Checked == true && !LoginAuthenticator.registered)
{
AudioRB.Checked = false;
TextRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = false;
EnableCompr_checkBox.Visible = true;
FileListBox.Hide();
FileTransfer.FileType = "Image";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
DisplayRadioButtons();
ViewFiles();
}
else if (ImageRB.Checked == false)
{
//ViewFiles_button.Visible = false;
RequestForm.FileListBox.Hide();
}
}
private void TextRB_CheckedChanged(object sender, EventArgs e)
{
ChosenTech = "Default";
if (TextRB.Checked == true && LoginAuthenticator.registered)
{
ImageRB.Checked = false;
AudioRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = true;
EnableCompr_checkBox.Visible = true;
RequestForm.FileListBox.Hide();
DisplayRadioButtons();
FileTransfer.FileType = "Text";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
ViewFiles();
//ViewFiles_button.Visible = true;
}
else if (TextRB.Checked == true && !LoginAuthenticator.registered)
{
ImageRB.Checked = false;
AudioRB.Checked = false;
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
EnableCompr_checkBox.Enabled = false;
EnableCompr_checkBox.Visible = true;
RequestForm.FileListBox.Hide();
DisplayRadioButtons();
FileTransfer.FileType = "Text";
SelectedFile = string.Empty;
FileName_textBox.Text = string.Empty;
ViewFiles();
}
else if (TextRB.Checked == false)
{
//ViewFiles_button.Visible = false;
RequestForm.FileListBox.Hide();
}
}
private void Cache_checkBox_CheckedChanged(object sender, EventArgs e)
{
if (Cache_checkBox.Checked)
{
CacheMsg_label.Text = "Caching Enabled";
CacheSelected = true;
}
else
{
CacheMsg_label.Text = "Caching Disabled";
CacheSelected = false;
}
}
private void EnableCompr_checkBox_CheckedChanged(object sender, EventArgs e)
{
DisplayRadioButtons();
}
internal void DisplayRadioButtons()
{
if (EnableCompr_checkBox.CheckState == CheckState.Unchecked)
{
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
DefaultRB.Enabled = false;
DefaultRB.Visible = false;
NoneRB.Enabled = false;
NoneRB.Visible = false;
}
if (EnableCompr_checkBox.Checked && FileTransfer.FileType == null)
{
MessageBox.Show("Please select a FileType.\nAssociated Compression techniques available will be shown.");
EnableCompr_checkBox.CheckState = CheckState.Unchecked;
}
if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Text" && LoginAuthenticator.registered)
{
DefaultRB.Enabled = true;
DefaultRB.Visible = true;
NoneRB.Visible = true;
NoneRB.Enabled = true;
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
ComprMsgLabel.Text = "Chosen Compression technique : Default";
}
else if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Text" && !LoginAuthenticator.registered)
{
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
NoneRB.Visible = false;
DefaultRB.Visible = false;
ComprMsgLabel.Text = "Chosen Compression technique : Default";
}
else if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Image" && LoginAuthenticator.registered)
{
DefaultRB.Enabled = true;
DefaultRB.Visible = true;
NoneRB.Visible = true;
NoneRB.Enabled = true;
HighRB.Enabled = true;
MediumRB.Enabled = true;
LowRB.Enabled = true;
HighRB.Visible = true;
MediumRB.Visible = true;
LowRB.Visible = true;
ChosenTech = "Default";
ComprMsgLabel.Text = "Chosen Compression technique : Default";
}
else if (EnableCompr_checkBox.Checked && FileTransfer.FileType == "Image" && !LoginAuthenticator.registered)
{
HighRB.Enabled = false;
MediumRB.Enabled = false;
LowRB.Enabled = false;
HighRB.Visible = false;
MediumRB.Visible = false;
LowRB.Visible = false;
DefaultRB.Visible = false;
NoneRB.Visible = false;
DefaultRB.Enabled = false;
NoneRB.Enabled = false;
ChosenTech = "VLOW";
ComprMsgLabel.Text = "Chosen Compression technique : " + ChosenTech;
}
}
private void NoneRB_CheckedChanged(object sender, EventArgs e)
{
if (FileTransfer.FileType == "Text" && NoneRB.Checked == true)
{
ChosenTech = "None";
DefaultRB.Checked = false;
}
else if (FileTransfer.FileType == "Image" && NoneRB.Checked == true)
{
ChosenTech = "None";
DefaultRB.Checked = false;
HighRB.Checked = false;
LowRB.Checked = false;
MediumRB.Checked = false;
}
else if (NoneRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void Logout_Label_Click(object sender, EventArgs e)
{
Connections.EndConnectionWithServer();
Form1 newForm = new Form1();
LoginAuthenticator.myCurrentUser = string.Empty;
ChosenTech = string.Empty;
FileTransfer.FileType = null;
this.Hide();
newForm.Show();
MessageBox.Show("LogOut successful");
}
private void MediumRB_CheckedChanged(object sender, EventArgs e)
{
if (MediumRB.Checked == true)
{
ChosenTech = "MEDIUM";
HighRB.Checked = false;
LowRB.Checked = false;
NoneRB.Checked = false;
}
else if (MediumRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void LowRB_CheckedChanged(object sender, EventArgs e)
{
if (LowRB.Checked == true)
{
ChosenTech = "LOW";
HighRB.Checked = false;
MediumRB.Checked = false;
NoneRB.Checked = false;
}
else if (LowRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void HighRB_CheckedChanged(object sender, EventArgs e)
{
if (HighRB.Checked == true)
{
ChosenTech = "HIGH";
LowRB.Checked = false;
MediumRB.Checked = false;
NoneRB.Checked = false;
}
else if (HighRB.Checked == false)
{
ChosenTech = "Default";
}
ComprMsgLabel.Text = "Compression Technique chosen is :" + ChosenTech;
}
private void statistics_button_Click(object sender, EventArgs e)
{
Form2 statForm = new Form2();
statForm.fileType_label.Text = statForm.fileType_label.Text + FileTransfer.FileType;
statForm.fnstat_label.Text = statForm.fnstat_label.Text + SelectedFile;
statForm.ASize_label.Text = statForm.ASize_label.Text + " " + FileTransfer.actualSize + " bytes";
statForm.recSize_label.Text = statForm.recSize_label.Text + " " + FileTransfer.recSize + " bytes";
statForm.rtt_label.Text = statForm.rtt_label.Text + " " + watch.ElapsedMilliseconds + " ms";
long percentage = ((FileTransfer.actualSize - FileTransfer.recSize) * 100) / FileTransfer.actualSize;
statForm.BWsave.Text = statForm.BWsave.Text + " " + percentage + " %";
statForm.Show();
if (watch.ElapsedMilliseconds > 0)
{
MessageBox.Show("The time taken for the transaction is : " + watch.ElapsedMilliseconds);
}
else
{
MessageBox.Show("The time taken for the transaction is : 1000 ");
}
}
private void Folder_Select_Button_Click(object sender, EventArgs e)
{
SaveFileDialog sd = new SaveFileDialog();
sd.InitialDirectory = #"D:\FT\";
sd.Filter = "Text files (*.txt)|*.txt|ImageFiles (*.jpg)|*.jpg | Audio Files (*.mp3)|*.mp3";
sd.ShowDialog();
if (!File.Exists(sd.FileName))
{
FileStream fs = File.Create(sd.FileName);
fs.Close();
}
else
{
FileStream fd = File.Open(sd.FileName, FileMode.OpenOrCreate);
fd.Close();
}
StoragePath = sd.FileName;
}
private void RegisterlinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Connections.EndConnectionWithServer();
//MessageBox.Show("Please Enter the Registration Details. ");
Register regForm = new Register();
regForm.Show();
this.Hide();
}
private void refresh_Click(object sender, EventArgs e)
{
if (FileTransfer.FileType == null)
{
MessageBox.Show("Select a File Type");
}
else
{
ViewFiles();
}
}
private void Setting_Label_Click(object sender, EventArgs e)
{
Process myprocess = new Process();
try
{
myprocess.StartInfo.UseShellExecute = true;
myprocess.StartInfo.FileName = "http://" + HelperInfo.Default_Gateway + "//WANem";
myprocess.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void FileName_textBox_TextChanged(object sender, EventArgs e)
{
}
}
}
Try using below code in your form constructor.
this.DoubleBuffered = true;
It should improve some performance. Windows forms are not made to display lot of images overlapped on each other.

cannot see the label content when a item is selected from dynamically added dropdownlist

I have a Dropdownlist (DDL1) when I select any item from this dropdownlist(DDL1), results in creation of another dropdownlist(DDL2), This contains some of the items.When I select other Item from DDL1 , Items will change in DDL2, this happens for the each different item selected in DDL1.
when I select a item from DDL2, label content must be shown, intially I'm making Label invisibe and in the code I changed the visibility to true and added content to it. But the label content is not shown when I select a item from DDL2.
Here is my Code
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "Abe Books")
{
DropDownSeller.Visible = true;
lnkUsdBooks.Visible = true;
lnkUsdBooks.Text = "usedbooks#abe.com";
lnkUsdBooks.NavigateUrl = "mailto:usedbook#abe.com";
DropDownSeller.Visible = true;
DropDownSeller.Items.Remove("Chacha Choudary");
DropDownSeller.Items.Remove("SpiderMan");
DropDownSeller.Items.Remove("Amar chitra Katha");
DropDownSeller.Items.Remove("Chandamama");
DropDownSeller.Items.Remove("Mahabharata");
DropDownSeller.Items.Add("Amar chitra Katha");
DropDownSeller.Items.Add("Chandamama");
DropDownSeller.Items.Add("Mahabharata");
DropDownSeller.DataBind();
if (DropDownSeller.SelectedValue == "Amar chitra Katha")
{
lblPrice.Visible = true;
lblPrice.Text = "$69.99";
}
else if (DropDownSeller.SelectedValue == "Chandamama")
{
lblPrice.Visible = true;
lblPrice.Text = "$59.99";
}
else if (DropDownSeller.SelectedValue == "Mahabharata")
{
lblPrice.Visible = true;
lblPrice.Text = "$49.99";
}
else
{
lblPrice.Visible = false;
}
}
Any ideas on this are appreciated
Thanks,
Remove if (!Page.IsPostBack) from the DropDownList1_SelectedIndexChanged because when the page postbacks this condition will be false. Because your page is posting back to the server that's why it is not visible and not showing.
In short your DropDownList1_SelectedIndexChanged should be like..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "Abe Books")
{
DropDownSeller.Visible = true;
lnkUsdBooks.Visible = true;
lnkUsdBooks.Text = "usedbooks#abe.com";
lnkUsdBooks.NavigateUrl = "mailto:usedbook#abe.com";
DropDownSeller.Visible = true;
DropDownSeller.Items.Clear(); // it will clear all the items, instead you are removing one by one
DropDownSeller.Items.Add("Amar chitra Katha");
DropDownSeller.Items.Add("Chandamama");
DropDownSeller.Items.Add("Mahabharata");
DropDownSeller.DataBind();
}
protected void DropDownSeller_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownSeller.SelectedValue == "Amar chitra Katha")
{
lblPrice.Visible = true;
lblPrice.Text = "$69.99";
}
else if (DropDownSeller.SelectedValue == "Chandamama")
{
lblPrice.Visible = true;
lblPrice.Text = "$59.99";
}
else if (DropDownSeller.SelectedValue == "Mahabharata")
{
lblPrice.Visible = true;
lblPrice.Text = "$49.99";
}
else
{
lblPrice.Visible = false;
}
}

organized workingspace in c# form

it's my first time in C# form and I dont know if what I am doing is correct.
below is my form workspace in C# and you can see that there's a lot of things there and it's messy...
i use the this.BackgroundImage = image; to change the background image of the form and i just turn the visible property of each control on whenever they are needed so that it look nice when i run the app (im still not finish with it though, it's hard to work in a form that's messy)
is there anyway i can work in an organized way like i could work with a lot of forms, instead of one, and just interconnect them like in powerpoint where you could have many slides and just use a hyperlink to point to other slides... please help...
and here's my code (sorry for the noob coding style)
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.Data.OleDb;
using Microsoft.VisualBasic;
namespace PProj1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backfromaboutandhow.Visible = false;
nextbutton.Visible = false;
backfromreserve.Visible = false;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
finish.Visible = false;
backtoreserve.Visible = false;
N1.Visible = false;
N2.Visible = false;
N3.Visible = false;
N4.Visible = false;
N5.Visible = false;
N6.Visible = false;
N7.Visible = false;
N8.Visible = false;
S1.Visible = false;
S2.Visible = false;
S3.Visible = false;
S4.Visible = false;
S5.Visible = false;
S6.Visible = false;
S7.Visible = false;
S8.Visible = false;
E1.Visible = false;
E2.Visible = false;
E3.Visible = false;
E4.Visible = false;
W1.Visible = false;
W2.Visible = false;
W3.Visible = false;
NW1.Visible = false;
NW2.Visible = false;
NE1.Visible = false;
NE2.Visible = false;
SW.Visible = false;
SE.Visible = false;
}
OleDbConnection con;
OleDbCommand cmd;
OleDbDataAdapter adapter;
DataSet ds;
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
aboutoldtrafford.Location = new Point(16, 9);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
aboutoldtrafford.Location = new Point(9, 9);
}
private void pictureBox2_MouseHover(object sender, EventArgs e)
{
howtogetthere.Location = new Point(16, 62);
}
private void pictureBox2_MouseLeave(object sender, EventArgs e)
{
howtogetthere.Location = new Point(9, 62);
}
private void pictureBox3_MouseHover(object sender, EventArgs e)
{
reserveaticket.Location = new Point(16, 113);
}
private void pictureBox3_MouseLeave(object sender, EventArgs e)
{
reserveaticket.Location = new Point(9, 113);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.about;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = true;
}
private void pictureBox4_MouseHover(object sender, EventArgs e)
{
backfromaboutandhow.Location = new Point(566, 511);
}
private void pictureBox4_MouseLeave(object sender, EventArgs e)
{
backfromaboutandhow.Location = new Point(559, 511);
}
private void pictureBox4_MouseClick(object sender, MouseEventArgs e)
{
backfromaboutandhow.Visible = false;
Image image = Properties.Resources.oldtraffordwelcome1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = true;
howtogetthere.Visible = true;
reserveaticket.Visible = true;
}
private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.howto;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = true;
}
private void pictureBox3_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.reg1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = false;
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
nextbutton.Visible = true;
backfromreserve.Visible = true;
}
private void pictureBox5_MouseHover(object sender, EventArgs e)
{
nextbutton.Location = new Point(545, 463);
}
private void pictureBox5_MouseLeave(object sender, EventArgs e)
{
nextbutton.Location = new Point(539, 463);
}
private void pictureBox6_MouseHover(object sender, EventArgs e)
{
backfromreserve.Location = new Point(30, 463);
}
private void pictureBox6_MouseLeave(object sender, EventArgs e)
{
backfromreserve.Location = new Point(36, 463);
}
private void pictureBox6_MouseClick(object sender, MouseEventArgs e)
{
nextbutton.Visible = false;
backfromreserve.Visible = false;
Image image = Properties.Resources.oldtraffordwelcome1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = true;
howtogetthere.Visible = true;
reserveaticket.Visible = true;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
finish.Visible = false;
backtoreserve.Visible = false;
}
private void pictureBox5_MouseClick(object sender, MouseEventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
{
Image image = Properties.Resources.reg21;
this.BackgroundImage = image;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
nextbutton.Visible = false;
backfromreserve.Visible = false;
finish.Visible = true;
backtoreserve.Visible = true;
N1.Visible = true;
N2.Visible = true;
N3.Visible = true;
N4.Visible = true;
N5.Visible = true;
N6.Visible = true;
N7.Visible = true;
N8.Visible = true;
S1.Visible = true;
S2.Visible = true;
S3.Visible = true;
S4.Visible = true;
S5.Visible = true;
S6.Visible = true;
S7.Visible = true;
S8.Visible = true;
E1.Visible = true;
E2.Visible = true;
E3.Visible = true;
E4.Visible = true;
W1.Visible = true;
W2.Visible = true;
W3.Visible = true;
NW1.Visible = true;
NW2.Visible = true;
NE1.Visible = true;
NE2.Visible = true;
SW.Visible = true;
SE.Visible = true;
}
else
{
label5.Visible = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
con = new OleDbConnection(#" provider=Microsoft.ace.Oledb.12.0; data source=C:\OldTrafford.accdb; Persist Security Info=False");
loaddata();
}
void loaddata()
{
adapter = new OleDbDataAdapter("select * from oldtraff", con);
ds = new DataSet(); //student-> table name in stud.accdb file
adapter.Fill(ds, "oldtraff");
ds.Tables[0].Constraints.Add("pk_ID", ds.Tables[0].Columns[0], true);//creating primary key for Tables[0] in dataset
//dataGridView1.DataSource = ds.Tables[0];
}
private void finish_MouseHover(object sender, EventArgs e)
{
finish.Location = new Point(545, 553);
}
private void finish_MouseLeave(object sender, EventArgs e)
{
finish.Location = new Point(539, 553);
}
private void backtoreserve_MouseHover(object sender, EventArgs e)
{
backtoreserve.Location = new Point(30, 553);
}
private void backtoreserve_MouseLeave(object sender, EventArgs e)
{
backtoreserve.Location = new Point(36, 553);
}
private void backtoreserve_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.reg1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = false;
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
nextbutton.Visible = true;
backfromreserve.Visible = true;
finish.Visible = false;
backtoreserve.Visible = false;
N1.Visible = false;
N2.Visible = false;
N3.Visible = false;
N4.Visible = false;
N5.Visible = false;
N6.Visible = false;
N7.Visible = false;
N8.Visible = false;
S1.Visible = false;
S2.Visible = false;
S3.Visible = false;
S4.Visible = false;
S5.Visible = false;
S6.Visible = false;
S7.Visible = false;
S8.Visible = false;
E1.Visible = false;
E2.Visible = false;
E3.Visible = false;
E4.Visible = false;
W1.Visible = false;
W2.Visible = false;
W3.Visible = false;
NW1.Visible = false;
NW2.Visible = false;
NE1.Visible = false;
NE2.Visible = false;
SW.Visible = false;
SE.Visible = false;
}
}
}
You should probably consider using "User Controls" to separate your screens. Each of these controls should manage it's own interactions with the user. If you need to let you main form know what's going on with one of these controls, you can use an event (just like any other type of control).
For the example you've shown, you'd have 5 controls (one for each screen), and maybe a few events on each control which indicate the user has pressed the "Next" button (or whatever).
The main control should change which control is displayed at any given time... and it'll probably be easiest to do this through code instead of the GUI designer.
EDIT:
This link is super old, but it'll give you an idea of what I mean - and save me from making a bunch of screen shots and pasting them in here:
http://msdn.microsoft.com/en-us/library/aa302342.aspx
http://knol.google.com/k/creating-custom-controls-with-c-net#
I think you could make wizard like your application. Also as others mentioned in their answers use user controls to "group" controls to manage them easily.
You could make a usercontrol for each and simply hide/show that usercontrol.

Categories

Resources