I did some research about this error, and all awnsers i found include removing static from the method or the property, but in my code there isnt any static, so i dont know whats happening, thanks for your help.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class textoTitular : Form
{
public textoTitular()
{
InitializeComponent();
}
private void textoTitular_Load(object sender, EventArgs e)
{
textoTitular.Text = "testing"; /// prints testing on the textbox
}
}
}
Your problem is in
private void textoTitular_Load(object sender, EventArgs e)
{
textoTitular.Text = "testing"; /// prints testing on the textbox
}
You are referencing the form class in a static way.
Rather try using this. Something like
private void textoTitular_Load(object sender, EventArgs e)
{
this.Text = "testing"; /// prints testing on the textbox
}
Added bonus, you can omit the this and use the object property
private void textoTitular_Load(object sender, EventArgs e)
{
Text = "testing"; /// prints testing on the textbox
}
Related
Hi I have 2 questions.
My timer stops immediately and if it doesn't it does not pick up the given interval.
CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClashRoyaleHack
{
public partial class GoldCMD : Form
{
public GoldCMD()
{
InitializeComponent();
timer2.Interval = 10000;
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Start();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void timer2_Tick(object sender, EventArgs e)
{
this.Close();
}
}
}
On creating the code I notices a slight difference. I am using almost identical forms with the same content but different names and different timer names etc. But one does work with the timer and same code (using ofcourse different names for the timer and form etc) but the other doesn't...
Really weird. Hopefully you guys can help me out. The code above does not have the private void GoldCMD_Load(object sender, EventArgs e) But just the normal private void GoldCMD(object sender, EventArgs e) maybe that has something to do with it.
Code of the one that does work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClashRoyaleHack
{
public partial class GemsCMD : Form
{
public GemsCMD()
{
InitializeComponent();
}
private void GemsCMD_Load(object sender, EventArgs e)
{
timer1.Interval = 7500;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Close();
}
}
}
The Solution:
It is conceivable that the timer may "tick" once upon starting and considering your code closes it immediately after a tick, it is likely the source of your issue. Otherwise if that is not the reasoning, then you are still killing it at its first tick regardless of time.
i am new to .net i am having trouble playing videos automatically. I would be showing different textboxes here but i want the video to autplay without any buttons
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WMPLib;
namespace ThinkQDisplay
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
AxWindowsMediaPlayer1.URL = "C:\Users\Ramrod\Documents\Visual Studio 2012\Projects\ThinkQDisplay\ThinkQDisplay\sample.avi";
}
}
}
It keeps telling it is an unrecognized escape sequence. Also I would like to have a separate form (form2). Where I can choose what to play here on form 1. Is it also possible to have it looped?
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = #"C:\Users\Ramrod\Documents\Visual Studio 2012\Projects\ThinkQDisplay\ThinkQDisplay\sampler.avi";
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.autoStart = true;
}
}
}
In this code Contents and Index are working but Search doesn't.I am not sure that HelpNavigator.Find is OK? Is there some other way to display Search from chm file?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Spomenik
{
public partial class Pomoc : Form
{
public Pomoc()
{
InitializeComponent();
}
private void Help_Click(object sender, EventArgs e)
{
System.Windows.Forms.Help.ShowHelp(this, "..\\..\\Slika\\SpomenikPomoc.chm");
//Help.Show(this, "..\\..\\Slika\\SpomenikPomoc.chm"); ne radi jer mi je Help naziv dugmeta
}
private void Index_Click(object sender, EventArgs e)
{
System.Windows.Forms.Help.ShowHelpIndex(this, "..\\..\\Slika\\SpomenikPomoc.chm");
}
private void Search_Click(object sender, EventArgs e)
{
**System.Windows.Forms.Help.ShowHelp(this, "..\\..\\Slika\\SpomenikPomoc.chm", HelpNavigator.Find);**
}
}
}
Specify an empty string ("") as the last parameter:
Help.ShowHelp(this, "..\\..\\Slika\\SpomenikPomoc.chm", HelpNavigator.Find, "");
From Help.ShowHelp Method (Control, String, HelpNavigator, Object):
If the value specified in the command parameter is TableOfContents, Index, or Find, this value should be an empty string.
Reflector shows that the ShowHelp version with 3 parameters uses null as the 4th parameter, rather than "". Probably that is why Find doesn't work. You need to use the "full" method version with all the needed parameters.
I'm trying to make a multiWindowsForm.
Just to try how it is working I started with a a simple form that I added a button to. When clicking on it, another window should pop up. But I can't get it to work. It crashes with error:
Object reference not set to an instance of an object!
I used Project → Add → Windows form and named it Mupp.cs
Here's my code for Form1.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MultiForm
{
public partial class tryout : Form
{
public tryout()
{
InitializeComponent();
}
Mupp theMupp;
private void Form1_Load(object sender, EventArgs e)
{
theMupp = new Mupp();
}
private void button1_Click(object sender, EventArgs e)
{
theMupp.Show();
}
}
}
What can I have missed out on?
It looks like the load event is not firing and thus not initilising your object. Make sure that the load event is hooked up.
Alternatively, initialise in the click event.
private void button1_Click(object sender, EventArgs e)
{
using (Mupp theMupp = new Mupp())
{
theMupp.ShowDialog();
}
}
I hope this helps.
public tryout()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
Basically what I'm trying to do is I have a string on the main form that pulls its value from a textbox.
I then generate a modal version of a second form and want to have that string (or the main forms textbox1.text value) usable in the second form for processes.
Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool{
public partial class MainForm : Form
{
public string hostname;
public MainForm()
{
InitializeComponent();
textBox1.Text = hostname;
}
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.ShowDialog();
}
}
}
'
Child Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool
{
public partial class SiteForm : Form
{
public string hostname {get; set; }
public SiteForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = this.hostname;
}
}
}
Any suggestions on how I can do this? I know there has to be a simpler way, sorry I'm still a bit of a noob and am trying to teach myself C# as I go.
The result is when I click the label on the child form it is blank, because of this I am able to deduce that the string isn't passing between the two forms correctly.
The simplest way is to pass it in the constructor of the Child form, for example:
private string _hostname = "";
...
public SiteForm(string hostname)
{
_hostname = hostname;
InitializeComponent();
}
Try hooking into your child form's Load event and set the value of its hostname property in an event handler on your main form.
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.Load += new EventHandler(frmsite_Load);
frmsite.ShowDialog();
}
public void frmsite_Load(object sender, EventArgs e)
{
SiteForm frmsite = sender as SiteForm;
frmsite.hostname = this.hostname;
}