Select all in a textbox unless clicked again. c# Confused - c#

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();
}
}

Related

Create a procedure to reduce redundant code in C#

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
}

Hiding / displaying buttons under certain conditions

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;
}

Check if tabControl1 is null?

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.

C# Infinite checkbox loop

I have an infinite loop caused by what I believe is an event being raised. However I don't understand why the event would be raised.
There is a listbox of items and another checkbox list that accompanies it. The checkboxlist shows child items from each item in the listbox.
private void checkedListBoxSignIn_ItemCheck(object sender, ItemCheckEventArgs e)
{
int userIndex = listBoxSignIn.SelectedIndex;
UpdateUserList();
listBoxSignIn.SelectedIndex = userIndex;
}
private void UpdateUserList()
{
listBoxSignIn.Items.Clear();
foreach (User u in _userList)
{
listBoxSignIn.Items.Add(u.Name);
}
}
private void listBoxSignIn_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listBoxSignIn.SelectedIndex;
UpdateCheckListBox(index);
}
private void UpdateCheckListBox(int index)
{
checkedListBoxSignIn.Items.Clear();
foreach (Item i in _userList.Items)
{
checkedListBoxKartSignIn.Items.Add(i, i.status);
}
}
Once you tick a checkbox a loop starts:
-> checkedListBox_ItemCheck()
-> listbox_SelectedIndexChanged()
-> checkedListBox_ItemCheck()
...
It seems that adding an item to the checked listbox counts as a "Item Check" despite the "Item Check" occuring on construction.
Is this correct behaviour? If so, how do I avoid the loop?
I am using Winforms
When dealing with controls events that will load or modify other controls I usually had a flag to stop infinite loops like the one you might be having.
It is not an ideal solution but it was the simplest I could think at the time.
I haven't run the code below, just is just as illustration:
New variables:
private bool _updatingCheckList = false;
private bool _updatingList = false;
Then:
private void checkedListBoxSignIn_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (_updatingList) return; //if it is already doing it, don't do it again
int userIndex = listBoxSignIn.SelectedIndex;
try {
_updatingList = true;
UpdateUserList();
} finally {
_updatingList = false;
}
listBoxSignIn.SelectedIndex = userIndex;
}
private void UpdateUserList()
{
listBoxSignIn.Items.Clear();
foreach (User u in _userList)
{
listBoxSignIn.Items.Add(u.Name);
}
}
private void listBoxSignIn_SelectedIndexChanged(object sender, EventArgs e)
{
if (_updatingCheckList) return; //if it is already doing it, don't do it again
int index = listBoxSignIn.SelectedIndex;
try {
_updatingCheckList = true;
UpdateCheckListBox(index);
finally { _updatingCheckList = false; }
}
private void UpdateCheckListBox(int index)
{
checkedListBoxSignIn.Items.Clear();
foreach (Item i in _userList.Items)
{
checkedListBoxKartSignIn.Items.Add(i, i.status);
}
}
Another alternative is to remove the handlers before doing the operation and readding.
I hope this helps.
I have managed to fix the issue. The main problem here was that the listbox was being updated everytime anything was done to the checkedbox list. The fix was to ensure that some user interaction has actually taken place. The easiest way for me to do this was a simple if statement checking that both lists have been selected.
private void checkedListBoxSignIn_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(listbox.SelectedIndex > -1 && checkedListBox.SelectedIndex > -1){
int userIndex = listBoxSignIn.SelectedIndex;
UpdateUserList();
listBoxSignIn.SelectedIndex = userIndex;
}
}

Timer, click, mousedown, mouseup events not working together

Looking for some help on a problem Im having
sorry if this question has already been asked, I can not find anything similar.
The idea is when a picturebox is clicked changed the image to ON.
If the picture box is held for more than 2 seconds to open a new form and leave the picturebox as OFF.
However if the picturebox is clicked ON and then held for 2 seconds and then returns i need the picturebox state to remain ON.
Here is what I have tried so far.
I believe for this to work correctly I need to stop MouseUp event from occuring.
Is there a way I can stop MouseUp when Tick occurs?
Is there a easier / better way to do this?
Any help would be appreciated.
private void time_HoldDownInternal_Tick(object sender, EventArgs e)
{
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
form1show.Visible = true;
}
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
time_HoldDownInternal.Enabled = true;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
//MessageBox.Show("mouse up");
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
}
private void pb_pictureBoxTest_Click(object sender, EventArgs e)
{
if (mainMenuVariables.mousedown == true)
{
if (mainMenuVariables.pictureBox == false)
{
mainMenuVariables.pictureBox = true;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOn);
return;
}
if (mainMenuVariables.pictureBox == true)
{
mainMenuVariables.pictureBox = false;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOff);
return;
}
}
if (mainMenuVariables.mousedown == false)
{
//nothing
}
}
Rather than starting a timer, just record the current time on mouse down. Then in mouse up, check if it has been 2 seconds. e.g:
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
mainMenuVariables.mousedowntime = DateTime.Now;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
var clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;
if ( clickDuration > TimeSpan.FromSeconds(2))
{
// Do 'hold' logic (e.g. open dialog, etc)
}
else
{
// Do normal click logic (e.g. toggle 'On'/'Off' image)
}
}

Categories