cannot invoke properties of control - c#

I'm trying to programmatically create a axWindowsMediaPlayer and show it:
private void button1_Click(object sender, EventArgs e)
{
AxWMPLib.AxWindowsMediaPlayer wmplayer = new AxWMPLib.AxWindowsMediaPlayer();
wmplayer.Size = new Size(200, 200);
wmplayer.enableContextMenu = false; //here it throws an exception
this.Controls.Add(wmplayer);
}
but it says {Property set of 'enableContextMenu' cannot be invoked at this time.}
why is that? why can I set Size but not enableContextMenu?

I found the solution:
It is important that you do changes and/or function calls after you added wmplayer to this.Controls.
I don't know why wmplayer.Size worked, but it is definitively the exception..
private void button1_Click(object sender, EventArgs e)
{
AxWMPLib.AxWindowsMediaPlayer wmplayer = new AxWMPLib.AxWindowsMediaPlayer();
this.Controls.Add(wmplayer);
wmplayer.Size = new Size(200, 200);
wmplayer.enableContextMenu = false; //here it throws an exception
}
works perfectly fine..

Related

Passing variables in C#

I'm new to C#. I thought I knew a bit of C# but clearly not.
As an example I'm using a very plain form with a button and a custom textbox. Clicking on the button should give me the content of the custom textbox, but I'm getting
Error CS0103 The name 'tb' does not exist in the current context
I have tried all possible options provided but with no luck.
When I use a static textbox (named tb) from the toolbox then it works without any errors. Below is my code:
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Dock = System.Windows.Forms.DockStyle.Fill;
tb.Location = new System.Drawing.Point(600, 430);
tb.Multiline = true;
panel2.Controls.Add(tb);
}
public void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(tb.Text);
}
I have tried to search Google and Stack Overflow, but I'm not sure what to search for.
This is a problem of scope. You are declaring tb in your method, so outside the method it doesn't exist. You want to declare tb outside the method in the class itself:
TextBox tb;
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
tb = new TextBox();
tb.Dock = System.Windows.Forms.DockStyle.Fill;
tb.Location = new System.Drawing.Point(600, 430);
tb.Multiline = true;
panel2.Controls.Add(tb);
}
public void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(tb.Text);
}
Your tb variable is defined in the context of Form_Load(). Then it is added to the panel, then it goes out of scope. You need to find another way to get access to your text box... for example by making it a member variable of the class.

Visual C# - Properties.Settings won't save correctly

I'm making a program that saves the Mouse X and Mouse Y coordinates from a NumericUpDown-Box into Settings.settings so the program launches with the last used values.
Both Input Boxes call the method "saveXY" when "ValueChanged" as seen here
My Problem is: the X coordinates get saved without problems, the Y coordinates don't get saved at all - but the code is the same:
private void Form1_Load(object sender, EventArgs e)
{
movetoX.Value = Settings.Default.mouseX;
movetoY.Value = Settings.Default.mouseY;
}
-
private void saveXY(object sender, EventArgs e)
{
Settings.Default.mouseX = (int)movetoX.Value;
Settings.Default.mouseY = (int)movetoY.Value;
Settings.Default.Save();
}
Theese are my Settings.settings.
The .exe is availeble here.
This article may be useful to you. http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx
Update 1:
Had to perform a Properties.Settings.Default.Upgrade() and then your saved settings get loaded.
Sample
public Form1()
{
InitializeComponent();
//Load saved settings
this.Location = Properties.Settings.Default.Form1Location;
this.Size = Properties.Settings.Default.Form1Size;
//Allow changes to be implemented
this.StartPosition = FormStartPosition.Manual;
//capture changes
this.LocationChanged += new EventHandler(Form1_LocationChanged);
this.SizeChanged += new EventHandler(Form1_SizeChanged);
//capture the closing form event to save your new settings
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
}
void Form1_LocationChanged(object sender, EventArgs e)
{
//Capture the new values
Properties.Settings.Default.Form1Location = this.Location;
}
void Form1_SizeChanged(object sender, EventArgs e)
{
//Capture the new values
Properties.Settings.Default.Form1Size = this.Size;
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//you can capture the new values here as well
//save the new values
Properties.Settings.Default.Save();
}
thanks to hamix, its working now
i deleted saveXY and wrote this:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default.mouseX = (int)movetoX.Value;
Settings.Default.mouseY = (int)movetoY.Value;
Settings.Default.Save();
}
it now saves X and Y

How to make a MessageBoxButton move?

I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You will need to create your own form and make it act like a messagebox. Instead of creating a MessageBox, you will instantiate your own form and so that you can handle the buttons on it.

Using a control created on Form_Load in another event

I have two controls created on Form_Load, a button and a combobox. I also have an event for the button, but the event should be able to see the newly created combobox.
When I try to call the combobox by it's name it says that it doesn't exist in this context.
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
ComboBox kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
kombo.Items.Add("Panel"); //just an example
}
Is there a way to make this work?
Only controls which are in used in markup with runat="server" will be class variables on your page. They are actually defined in the designer file.
What you'll want to do is in the class add something like the following where you have a class variable, then assign kombo in your page-load function. Then, it will exist in your click event handler.
// kombo is now scoped for use throughout this class
ComboBox kombo = null;
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
// Assign to our kombo instance
kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
// Using the kombo we created in form load, which is still referenced
// in the class
kombo.Items.Add("Panel"); //just an example
}
You will have to use the FindControl() method to find the object first.
private void przycisk_Click(object sender, EventArgs e)
{
ComboBox kombo = (ComboBox)FindControl("kombo");
kombo.Items.Add("Panel");
}

How to use name of mediaElement created by code and using its name in another method?

I'm using Visual Studio 2010, c# WPF. I have created a MediaElement control during runtime;
MediaElement video= new MediaElement();
video.Width = 400;
video.Height = 400;
video.Play();
video.Source = new Uri(path, UriKind.Relative);
In the XAML view I created 2 buttons, Play and Stop which I will show to be visible only if a mediaElement is created. But I need to add click_events to these 2 buttons. But when I write video.Play(); it does not know this name. Does anyone know how to get this name to be recognised in these seperate methods?
private void play_Click(object sender, RoutedEventArgs e)
{
video.Play(); //syntax error under video
}
private void stop_Click(object sender, RoutedEventArgs e)
{
video.Play(); //syntax error under video
}
You need to define video as a member variable. For example, say you're in a class called MediaWindow...
public class MediaWindow
{
private MediaElement video = new MediaElement { Width = 400, Height = 400};
public void SetVideoSource(string path)
{
video.Source = new Uri(path, UriKind.Relative);
}
private void play_Click(object sender, RoutedEventArgs e)
{
video.Play();
}
private void stop_Click(object sender, RoutedEventArgs e)
{
video.Stop();
}
}
Adjust for your actual class structure and setup.

Categories

Resources