Disable Space key on Windows Phone - c#

I have a text box which the user shouldn't be allowed to write spaces in.
So far I have this code:
private void SearchCriteria_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key.ToString() == "Space")
{
DelLast = SearchCriteria.Text;
NeedsToDelete = true;
}
}
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
NeedsToDelete = false;
}
}
It works, but the cursor is being placed in front of the text. Is there another way to do this?

Save the SelectionStart in your OnKeyDown event and then set it again within the OnKeyUpEvent
private void SearchCriteria_KeyDown(object sender, System.Windows.Input.KeyEventArgs e
{
if (e.Key.ToString() == "Space")
{
DelLast = SearchCriteria.Text;
NeedsToDelete = true;
_selectionStart = SearchCriteria.SelectionStart;
}
}
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
SearchCriteria.SelectionStart = _selectionStart;
NeedsToDelete = false;
}
}

try this :
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
NeedsToDelete = false;
SearchCriteria.Select(SearchCriteria.Text.Length, 0);
}
}

Related

How to catch key states when I am dragging an object over a control? - C#

I need to change drag&drop effects with some keys while I am dragging some text inside a listbox.
bool ctrlD = false;
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.D)
ctrlD = true;
}
// KeyUp
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
textBox.DoDragDrop(textBox.Text, DragDropEffects.All);
}
private void listBox_DragOver(object sender, DragEventArgs e)
{
if (ctrlD) e.Effect = DragDropEffects.Copy;
else e.Effect = DragDropEffects.Move;
}
The problem is DragOver method doesn't see when any keys are pressed. Effects don't change. What can I do to this?
bool ctrlD = false
Use object sender
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.DoDragDrop(sender, DragDropEffects.All);
}
}
Make sure to set AllowDrop to True
Add (MyContol) (_DragEnter) and (_DragDrop) events
private void MyControl_DragEnter(object sender, DragEventArgs e)
{
/*
DragDropEffects
*/
e.Effect = DragDropEffects.Copy;
}
private void MyControl_DragDrop(object sender, DragEventArgs e)
{
TextBox tb = e.Data.GetData(typeof(TextBox)) as TextBox;
/*
MyControl.Controls.Add(tb);
* Your code here
*/
}
You need to read the DragEventArgs within your listBox_DragOver event to know what buttons and keys have been used for the drag operation:
if ((e.KeyState & 32) == 32) { bool alt = true; }
if ((e.KeyState & 16) == 16) { bool mousebuttonmiddle = true; }
if ((e.KeyState & 8) == 8) { bool control = true; }
if ((e.KeyState & 4) == 4) { bool shift = true; }
if ((e.KeyState & 2) == 2) { bool mousebutton2 = true; }
if ((e.KeyState & 1) == 1) { bool mousebutton1 = true; }

Sounds play with delay

here is my code:
MediaPlayer player = new System.Windows.Media.MediaPlayer();
bool playing = false;
bool _bKeyIsDown = false;
protected override void OnKeyDown(KeyEventArgs e)
{
if (_bKeyIsDown) return;
_bKeyIsDown = true;
// play sound;
base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
_bKeyIsDown = false;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
if (label5.Text == "Waiting 01.wav")
{
MessageBox.Show("No beat loaded");
return;
}
pictureBox6.Image = Form1.Properties.Resources.white_square_button;
try
{
playing = true;
player.Open(new Uri(label37.Text));
player.Volume = (double)trackBar4.Value / 100;
player.Play();
}
catch (FileNotFoundException)
{
MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
}
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
pictureBox6.Image = Form1.Properties.Resources.black_square_button;
player1.Stop();
player1.Close();
playing = false;
}
This what makes is to play a sound while key is down, but the problem is that when you release that key and press another one the sound delays and if you press 2 keys at the same time it only play the first one you pressed.
If you remove if (_bKeyIsDown) return; it does the trick but the sound won't play at its full length.
Is there a way to fix this problem? Thanks!
If its just play and stop 1 sound just do KeyDown event and use bool to toggle:
bool playing = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
if (!playing)
{
player.Open(new Uri(yourURI));
player.Play();
playing = true;
}
else
{
player.Stop();
playing = false;
}
}
}
EDIT:
then may i suggest you create a dictionary with all the sounds you need like this:
Dictionary<Keys, Uri> sounds = new Dictionary<Keys, Uri>()
{
{Keys.A,new Uri(#"C:\..\..\youraudiofile.mp3")},
{Keys.B,new Uri(#"C:\..\yourotheraudiofile.wma")}
};
and create a list to keep references to the mediaplayers:
List<MediaPlayer> myplayers = new List<MediaPlayer>();
then only in KeyDown event(remove the onKeyDown,and keyup also):
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
foreach (MediaPlayer m in myplayers)
{
m.Stop();
}
return;
}
if (sounds.ContainsKey(e.KeyCode))
{
MediaPlayer mp = new MediaPlayer();
mp.Open(sounds[e.KeyCode]);
mp.Play();
}
}

How do I combine these three sets of code

I have three radio buttons. The code behind their change events is as follows:
private void uxRajRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (uxRajRadioButton.Checked == true)
{
uxPersonBettingLabel.Text = "Raj";
GuySelected = 0;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
private void uxPaulRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (uxPaulRadioButton.Checked == true)
{
uxPersonBettingLabel.Text = "Paul";
GuySelected = 1;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
private void uxMikeRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (uxMikeRadioButton.Checked == true)
{
uxPersonBettingLabel.Text = "Mike";
GuySelected = 2;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
With just three radio buttons this is acceptable but if I had say 7 radio buttons each with 20 lines of code behind its change event then it would be a lot of (needless?) code. I've attempted to shorten the above and ended up with the following. Is this correct?
private void uxRajRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode(this.uxRajRadioButton, 0);
}
private void uxPaulRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode(this.uxPaulRadioButton,1);
}
private void uxMikeRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode(this.uxMikeRadioButton, 2);
}
int GuySelected=0;
public void radioButtonCode(RadioButton myRadio, int mybettorIndex)
{
if (myRadio.Checked == true)
{
GuySelected = mybettorIndex;
uxPersonBettingLabel.Text = Guys[GuySelected].Name;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
Could you have the same event handler for all the radio buttons? Something like
private void uxRadioButton_CheckedChanged(object sender, EventArgs e)
{
radioButtonCode((RadioButton)sender);
}
public void radioButtonCode(RadioButton myRadio)
{
if (myRadio.Checked == true)
{
int guySelected = getGuySelectedIndex(myRadio);
uxPersonBettingLabel.Text = Guys[guySelected].Name;
uxBetNumericUpDown.Maximum = Guys[guySelected].Cash;
}
}
public int getGuySelectedIndex(RadioButton myRadio)
{
int index = 0;
if (myRadio == this.uxRajRadioButton) index = 0;
else if (myRadio == this.uxPaulRadioButton) index = 1;
else if (myRadio == this.uxMikeRadioButton) index = 2;
return index;
}
Your second code sample, where you have extracted the commonalities out to a function looks like the way I would have done this refactoring.
It is about as good as you can make it, barring the name (UpdateUserInfo might be slightly better).
There is a better way. Declare an array of radio buttons and bind them to above event at run time. This binding code won't be inside designer page. This will result in single ArrayRadio_checkedChange event. In this method you can use sender property to figure out the proper radio button's index and take action accordingly.
you can use
uxRajRadioButton.CheckedChanged += new EventHandler(rb_CheckedChanged);
uxPaulRadioButton.CheckedChanged += new EventHandler(rb_CheckedChanged);
...
uxRajRadioButton.Tag =new KeyValuePair<string,int>("Raj",0);
uxPaulRadioButton.Tag =new KeyValuePair<string,int>("Paul",1);
....
private void rb_CheckedChanged(object sender, EventArgs e)
{
if(!(sender is RadioButton))
return;
RadioButton myRadio= sender as RadioButton;
if (myRadio.Checked == true)
{
myRadio.Text = (myRadio.Tag as KeyValuePair<string,int>).Key;
GuySelected = (myRadio.Tag as KeyValuePair<string,int>).Value;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}
What I usually do is I put the index in the tag attribute. This way you can bind this event to every RadioButton
public void uxRadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton myRadio = (RadioButton) sender;
if (myRadio.Checked)
{
GuySelected = (int)myRadio.Tag;
uxPersonBettingLabel.Text = Guys[GuySelected].Name;
uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
}
}

Panel not displaying when it should be

So in my very small program I am generating a random percentage and based off that percentage it should display one of two panels. However, it is only ever displaying one.
Here is my code:
Random random = new Random();
private void button1_Click(object sender, EventArgs e)
{
bool button1Clicked = true;
if (button1Clicked == true) { ITIpanel.Visible = true; }
}
private void ITIpanel_Paint(object sender, PaintEventArgs e)
{
ITItimer.Enabled = true;
}
private void ITItimer_Tick(object sender, EventArgs e)
{
double rand = random.NextDouble();
if (rand <= .50) { bluestimPanel.Visible = true; }
if (rand >= .50) { redstimPanel.Visible = true; }
ITItimer.Enabled = false;
}
private void bluestimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void redstimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void Trialtimer_Tick(object sender, EventArgs e)
{
bluestimPanel.Visible = false;
redstimPanel.Visible = false;
Trialtimer.Enabled = false;
ITIpanel.Visible = true;
}
I think what is happening here is that in the TrialTimer_Tick method setting ITIPanel.Visible to true is not causing ITIPanel to repaint and hence ITITimer never restarts.
You can put a breakpoint in the ITITimer_Click method and see if it is ever invoked again after the first invocation.

How can I prevent the raising of Correlative events?

I have two text box and i want to calculate each text box when the other changed.but my program fall into a loop.
private bool suspendEvents = false;
private void txtYourPercent_ValueChanged(object sender, EventArgs e)
{
if (suspendEvents)
return;
suspendEvents = true;
txtMyPercent.value = 100 - txtYourPercent.value;
suspendEvents = false;
}
private void txtMyPercent_ValueChanged(object sender, EventArgs e)
{
if (suspendEvents)
return;
suspendEvents = true;
txtYourPercent.value = 100 - txtMyPercent.value;
suspendEvents = false;
}
It's my way.This method is disgusting.Any better solution? ("WinForms")
If there is not another solution tell me to delete this question and use my (beep) solution.
You could assign both textboxes the same chenge event handler:
this.txtMyPercent.TextChanged += new System.EventHandler(this.txtPercent_TextChanged);
this.txtYourPercent.TextChanged += new System.EventHandler(this.txtPercent_TextChanged);
and then:
private void txtPercent_TextChanged(object sender, EventArgs e)
{
if (sender == txtMyPercent)
{
txtYourPercent.Text = (100 - int.Parse(txtMyPercent.Text)).ToString();
}
else if (sender == txtYourPercent)
{
txtMyPercent.Text = (100 - int.Parse(txtYourPercent.Text)).ToString();
}
}

Categories

Resources