I'm fairly new to WPF and I need your help with one object passing between more WPF windows.
Firstly I have my MainWindow with Button_Click event like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
Attribute ChooseYourAttr = new Attribute();
Application.Current.MainWindow.Close();
ChooseYourAttr.Show();
Character Player = new Character(firstTextbox.Text);
}
And Then I have my second window called Attribute with something like this:
private void attributeTopLabel_Initialized(object sender, EventArgs e)
{
String welcomeAttribute = "Ahh. I see! So ";
attributeTopLabel.Content = welcomeAttribute;
}
And I would like to have something like this: (Player.getName());
private void attributeTopLabel_Initialized(object sender, EventArgs e)
{
String welcomeAttribute = "Ahh. I see! So " + Player.getName();
attributeTopLabel.Content = welcomeAttribute;
}
Thanks for your answers!
Just pass the value through in the constructor:
private Character player = new Character();
public Attribute(Character player)
{
this.player = player;
}
...
Character player = new Character(firstTextbox.Text);
Attribute ChooseYourAttr = new Attribute(player);
...
private void attributeTopLabel_Initialized(object sender, EventArgs e)
{
String welcomeAttribute = "Ahh. I see! So " + player.GetName();
attributeTopLabel.Content = welcomeAttribute;
}
Related
I have this functions and I must use MediaPlayer because I have to play more sounds together. This code works, but the sounds doesn't stop on the keyup (I tried some code but didn't worked). How can I do the function stopSound?
Thank'you!
private void Form1_KeyDown(object sender, KeyPressEventArgs e)
{
[...] // Other code
playSound(key, name);
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
[...] // Other code
stopSound(key, name);
}
private void playSound(string name)
{
[...] // Other code
string url = Application.StartupPath + "\\notes\\" + name + ".wav";
var sound = new System.Windows.Media.MediaPlayer();
sound.Open(new Uri(url));
sound.play();
}
private void stopSound(string name)
{
???
}
If you store all references to the MediaPlayer instances that you create in a List<MediaPlayer>, you could access them later using this list and stop them. Something like this:
List<System.Windows.Media.MediaPlayer> sounds = new List<System.Windows.Media.MediaPlayer>();
private void playSound(string name)
{
string url = Application.StartupPath + "\\notes\\" + name + ".wav";
var sound = new System.Windows.Media.MediaPlayer();
sound.Open(new Uri(url));
sound.play();
sounds.Add(sound);
}
private void stopSound()
{
for (int i = sounds.Count - 1; i >= 0; i--)
{
sounds[i].Stop();
sounds.RemoveAt(i);
}
}
I need three functions: Copy, Paste, Cut ,
For a FastColoredTextBox.. so far with my methods, the job is done but afterwards,
the cursor's position get changed and I got no clue on how to keep it where it
was before.
Here's my methods:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var ePoint = rtbScript.SelectionLength;
var text = rtbScript.SelectedText;
rtbScript.Text = rtbScript.Text.Remove(sPoint, ePoint);
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.Text = rtbScript.Text.Insert(sPoint, text);
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.Text = rtbScript.Text.Insert(index, Clipboard.GetText());
}
Also, If there's a better way to do those functions, please post..
Thanks!
For a RichTextBox your code has more issues than loosing the Cursor position, It also looses all formatting! Here are versions that should work better:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var text = rtbScript.SelectedText;
rtbScript.Cut();
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.SelectionStart = sPoint;
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.SelectedText = Clipboard.GetText();
rtbScript.SelectionStart = index + Clipboard.GetText().Length;
}
Note: You must never change the Text property of a RTB or else you will mess up the formating!
Since you wrote that this also works with your FastColoredTextBox I have undeleted the solution..
In the current version of FCTB, these methods already exist inside the FCTB.cs file. They just need to be linked up.
I'm trying to find a way to get the string name of the method call which pops up a new window. I have three button click event handlers which will open the new window but I need to know which called the .Show();
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
I don't want to have to have three separate windows! is there an opening event handler parameter which I can fetch the caller from?
well, you can simply add a public variable at MobilityPortfolioSettings class and set its value in each method, ex: in buttonSettingsPortfolio1_Click add MobilityPortfolioSettings.Variable = 1 and so on.
Here
Console.write(triggeredBy); you can output the value by logging to file or some other way . This value will indicate which path your code took.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio1_Click");
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio2_Click");
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio3_Click");
}
private Open(string triggeredBy){
Console.write(triggeredBy); // You can write to file or output in some different way here.
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
Try this:
Cast sender as button and then get it's name.
Change the MobilityPortfolioSettings constructor so that it needs a string parameter.
Pass the button name to the constructor.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
string buttonName = "";
if (sender is Button)
buttonName = ((Button)sender).Name;
Window settingsWindow = new MobilityPortfolioSettings(buttonName);
settingsWindow.Show();
}
BTW use Window as variable type instead of var.
Cheers
I want my checkedlistbox to expand to a certain size when the mouse enters and then go back to a its original size after mouse leaves. Below is the code is have. However, I receive an error when i have another program selected and my mouse goes over the checkedlistbox while the application is not active.
Any suggestions on how to fix?
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;}
Error Code - Object Reference not set to an instance of an object.
Try this
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
checkedListBox1.Size = new Size(Width,Height);
}
This of course would work so that no exception is thrown, but I hope it's also what you want:
private void checkedListBox1_MouseEnter(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 552;
checkedListBox1.Height = 130;
}
private void checkedListBox1_MouseLeave(object sender, EventArgs e)
{
if(Search.ActiveForm == null) return;
Search.ActiveForm.Height = 452;
checkedListBox1.Height = 34;
}
I have the following piece of code that works great in all but one instance.
private void tbxLastName_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender)
{
var control = sender as TextEdit;
var maxChars = control.Properties.MaxLength;
tipCharacterCounter.Show(control.Text.Length + "/" + maxChars, this, control.Location.X, control.Location.Y - control.Height);
}
I just repeat this process from any textbox. Unfortunately, I have one control that is more complicated and I cannot get this to work. The Event portion looks like this -->
private void memDirectionsToAddress_Popup(object sender, EventArgs e)
{
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
What I don't understand is if I replace the tipCharacterCounter portion with, say updating a label, it works fine. It's like the ToolTip is hidden or something but I have tried feeding Show() different points to no avail.
Ideas?
Which version of DXPerience are you using? I've tried the following code using DXperience 10.1.5 and it works fine here:
private void memoExEdit1_Popup(object sender, EventArgs e) {
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) {
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender) {
TextEdit control = sender as TextEdit;
int maxChars = control.Properties.MaxLength;
tipCharacterCounter.ShowHint(control.Text.Length + "/" + maxChars, control, ToolTipLocation.RightBottom);
}