On the form1 there are buttons with this code. There is a button to go to the disk partition "C", "D", "E", "F" and so on. If the computer has such a disk partition - the button is visible else the button is hidden. How to do it?
private void button10_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(#"C:\");
}
You could use the follwoing code on your Page_Load()
foreach (System.IO.DriveInfo item in System.IO.DriveInfo.GetDrives())
{
if(item.Name == "C:\\")
{
button10.Visible = true;
}
else
{
button10.Visible = false;
}
}
You can use visibility property of a button in this kind of scenario.
for example:
if(condition) {
button.Visible = true;
}
Well since your buttons are hidden by default, you can write a
refresh() method. Inside you can ask if a specific drive exists.
string drive = #"C:\";
if (Directory.Exists(drive))
{
button.Visible = true;
}
Where do I add that code?
You should add the method call or code at a place that will:
be called everytime the Form/Control/Site is refreshed or intialized. Maybe in your Form1.Load Event.
when the buttons get initialized or they need to be seen.
A example for maybe a method you can use:
private void CheckForDisks()
{
if (Directory.Exists(#"C:\"))
{
buttonC.Visible = true;
}
if (Directory.Exists(#"D:\"))
{
buttonD.Visible = true;
}
if (Directory.Exists(#"E:\"))
{
buttonE.Visible = true;
}
// and so on... you can also do this with a loop, look up Adarsh Ravi answer for this
}
You can call this method either in your Form1.Load event like:
privat void Form1_Load(object sender, EventArgs e)
{
this.CheckForDisks();
}
button10_Click() this is a button event of your Form, inside that you can write like this
if(System.IO.DriveInfo.Contains == yourDrive)
{
button10.visible = true;
}
else
{
button10.visible = false;
}
Related
Im wondering how I would reveal a label, and a textbox once a checkbox has been checked.
I know in my project, everything below initialize component starts once the form is visible, so i try this.
public Form2()
{
InitializeComponent();
if (checkBox1.Checked == true)
{
label9.Visible = true;
textBox4.Visible = true;
}
else
{
label9.Visible = false;
textBox4.Visible = false;
}
}
but that doesnt work, and so i put it in a while(true) loop, which is just an infinite loop
public Form2()
{
while (true)
{
InitializeComponent();
if (checkBox1.Checked == true)
{
label9.Visible = true;
textBox4.Visible = true;
}
else
{
label9.Visible = false;
textBox4.Visible = false;
}
}
}
but that doesnt work either, if someone knows the answer, please let me know :).
You just need to use the checked changed event of the checkbox.
And call it from the constructor to initialize the state of the controls.
Also you only need to assign the Checked property to the Visible properties.
public Form2()
{
InitializeComponent();
checkBox1_CheckedChanged(checkBox1, EventArgs.Empty);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
label9.Visible = checkBox1.Checked;
textBox4.Visible = checkBox1.Checked;
}
Or you can use the form load to leave the constructor as is:
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1_CheckedChanged(checkBox1, EventArgs.Empty);
}
Order of Events in Windows Forms
Control.Created Property
But never put an infinite loop in the constructor or in form load or shown!
And never put such infinite loop without break somewhere...
That will freeze the application in it.
StackOverflow may not be the correct place to as a why question, but I am looking for a because answer rather than a how to answer. I have already worked around the problem by disabling the handler in the hander.
The application has a DataGridView that displays inventory information during incoming inspection. The data grid is too wide for the screen and requires horizontal scrolling. To make the data easier to see and edit a modal editor has been added. There are 2 buttons to close the modal editor, either Save or Cancel. Using the close button at the top right corner of the modal editor form should perform the same action as the cancel button.
When the cancel button is clicked everything works fine. When the close button is clicked the modal editor FormClosed event fire twice. Why is the modal editor FormClosed event firing twice? Do I have a bug in my code?
private bool CancelModalEditor()
{
bool cancelled = false;
string cancelMsg = (_cancelClicked) ? "Canceling" : "Closing";
cancelMsg += " the editor will delete the Record with Serial Number: " + SerialNumber + " from the Audit Session. Is this what you want to do?";
DialogResult dlg = MessageBox.Show(cancelMsg, "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dlg == DialogResult.Yes)
{
SaveClicked = false;
}
else
{
cancelled = true;
}
return cancelled;
}
private void AEMEBtn_Cancel_Click(object sender, EventArgs e)
{
_cancelClicked = true;
if (!CancelModalEditor())
{
Close();
}
else
{
_cancelClicked = false;
}
}
private void AEModalEditor_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!_cancelClicked && !SaveClicked)
{
if (CancelModalEditor())
{
e.Cancel = true;
}
else
{
_cancelClicked = true; // Prevent Infite Loop
Close();
}
}
}
File where modal editor is invoked.
private void ModalEditorForm_Closed(object sender, FormClosedEventArgs e)
{
AEModalEditor modalEditor = (AEModalEditor)sender;
int currentRow = modalEditor.RowID - 1;
if (modalEditor.SaveClicked)
{
UpdateDataGridRowWithModalEditorValues(dgAssetDetails, currentRow, modalEditor.AssetControlsValues);
updateAuditDetailsDataGridRow(currentRow, modalEditor.AuditControlsValues);
UpdateAuditTextFields(modalEditor);
SelectAllCellsInRow(currentRow);
}
else
{
DeleteRowFromAllDataGridViews(modalEditor.SerialNumber, currentRow);
_previouslySelectedRow = -1;
}
// Save all records in either case so that session data isn't lost.
save(false);
_currentlySelectedDataGrid = DataGrids.None;
_modalEditorOpen = false;
txtSerialNumber.Focus();
}
Don't call close in the closing-event again. The form is already closing and don't need to be closed a second time.
private void AEModalEditor_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!_cancelClicked && !SaveClicked)
{
if (CancelModalEditor())
{
e.Cancel = true;
}
else
{
_cancelClicked = true;
// You called Close here again
Close();
}
}
}
When I click a check box on a Windows Form, it enables a text box and sets the cursor in it ready for input. Code is relatively simple:
private void chkLatte_CheckedChanged(object sender, EventArgs e)
{
if(chkLatte.Checked)
{
txtLatte.Enabled = true;
txtLatte.Focus();
}
else
{
txtLatte.Enabled = false;
txtLatte.Text = "0";
}
}
Now, here's the rub. I have lots of these check boxes so what I want is something like this:
public void setCheckBox(string chkName, string txtName)
{
if (chkName.Checked)
{
txtName.Enabled = true;
txtName.Focus();
}
else
{
txtName.Enabled = false;
txtName.Text = "0";
}
}
Now, I can just call the method and pass the appropriate parameters like this:
private void chkMocha_CheckedChanged(object sender, EventArgs e)
{
setCheckBox(chkMocha,txtMocha);
}
Of course, this won't work: .Checked .Enabled .Focus() etc only work with a check box object and I define chkName as a string
How should I re-write the procedure setCheckBox to overcome this problem?
And why don't you pass the object sender as it is?
I mean something like this:
public void setCheckBox(CheckBox chk, TextBox txt)
{
if (chk.Checked)
{
txt.Enabled = true;
txt.Focus();
}
else
{
txt.Enabled = false;
txt.Text = "0";
}
}
And casting of course:
In the designer you have something like:
private System.Windows.Forms.TextBox txtMocha;
And by this reason you will solve a lot problems.
private void chkMocha_CheckedChanged(object sender, EventArgs e)
{
setCheckBox((CheckBox)sender, txtMocha);
}
Also, I have to say, that the code you give doesn't work... You have supposed it.
If you want pass the parameters as strings, use this:
Get a Windows Forms control by name in C#
One way to solve this is to assign the same handler to all checkboxes even
checkbox1.Check += chk_CheckedChanged;
checkbox2.Check += chk_CheckedChanged;
private void chk_CheckedChanged(object sender, EventArgs e)
{
// do your logic here
}
How can i check if tabcontrol1 is null(no pages-tabs inside)??
I want this code for setting up my tab control when is null to visible=false;
and when its not null to visible=true;
I'm using this code in selection changed but nothing is happened.
private void TabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (TabControl==null)
{
TabControl.Visible = false;
}
else
{
TabControl.Visible = true;
}
But nothing it doesnt work.
I found this way which it works. But please check if it is the right way.
private void TabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (TabControl.SelectedTab == TabControl.TabPages[""])//
{
TabControl.Visible = false;
}
else
{
TabControl.Visible = true;
}
}
What you need to is to check the TabPages property or the TabCount of the TabControl:
this.tabControl.Visible = !this.tabControl.TabCount == 0;
This code will set the Visible property to false if there are no tab pages.
I'm trying to make it in my program, if you click on a text box it will select it all. And then if you click it again it deselects it all.
I've tried making it like this..
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
url.SelectAll();
url.DeselectAll();
}
I know the url.DeselectAll(); is in the wrong spot.
Any help? Thanks in advance!
Clicking the textbox itself clears a selection, so you'd have to do something like this;
bool selected;
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
if (!selected)
{
selected = true;
url.SelectAll();
}
else
{
selected = false;
url.DeselectAll();
}
}
Your current code first calls
url.SelectAll();
and then immediately calls
url.DeselectAll();
Instead, check the current state of the item you are trying to toggle. It's not clear to me from the question exactly what that is, so in pseudocode:
private bool isSelected = false;
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
if (isSelected)
{
url.DeselectAll();
}
else
{
url.SelectAll();
}
isSelected = !isSelected;
}
Replace IsDeselected with something that checks whether the current state is deselected or not.
Your code always selects and then deselects again, so your text will always be deselected after mouse down.
Try this instead:
private void url_MouseDown(object sender, MouseEventArgs e)
{
url.ReadOnly = false;
if (url.SelectedText.Length < url.Text.Length) {
url.SelectAll();
} else {
url.DeselectAll();
}
}