If I start my form, write something in my textbox and then try to get the textbox value, then I get null. How can I get the textbox value? If I add a button and try to get the value, then it works. But I'm trying to do it without the button. I am using a WPF form. This is my code:
public partial class _2band : Page
{
public _2band()
{
InitializeComponent();
UpdateLayout();
RFIDI();
vardasBox1.Text = "";
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
// MainWondow mainas = new MainWondow();
DB db = new DB();
string pavad;
string vardas;
int status;
string s = "";
string s0 = "";
RfidApi Api = new RfidApi();
private System.Windows.Threading.DispatcherTimer dispatcherTimer;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
Api.nuskaitymas(out s0);
if (s0 != null)
{
s = s0;
vardas = vardasBox1.Text;
}
}
Related
Created the following code to workout some bugs in a bigger program. That said, when I click the button to add a row, the next row is added, but the previously created ComboBox (cmboPhoneType) disappears with each new click. What am I missing how to correct? Does this maybe have something to do with the name of the control?
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace AddNewRowTest
{
public partial class Form1 : Form
{
int leftControl = 1;
ComboBox cmboPhoneType = new ComboBox();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
addLabels();
btn_AddRow.PerformClick();
}
private void btn_AddRow_Click(object sender, EventArgs e)
{
//Connection Information redacted
reConn.Open();
MySqlCommand phoneTypeCommand = new MySqlCommand("Select * from lutable_phonetype order by Type;", reConn);
MySqlDataAdapter phoneTypeAdapter = new MySqlDataAdapter();
DataTable phoneTypeTable = new DataTable();
phoneTypeAdapter.SelectCommand = phoneTypeCommand;
phoneTypeAdapter.Fill(phoneTypeTable);
this.Controls.Add(cmboPhoneType);
cmboPhoneType.Top = leftControl * 36;
cmboPhoneType.Left = 12;
cmboPhoneType.Size = new Size(100, 20);
cmboPhoneType.DataSource = phoneTypeTable;
cmboPhoneType.DisplayMember = "Type";
cmboPhoneType.ValueMember = "TypeID";
cmboPhoneType.DataBindings.Add("SelectedValue", phoneTypeTable, "TypeID");
cmboPhoneType.SelectedIndexChanged += new System.EventHandler(cmboPhoneType_SelectedIndexChanged);
MaskedTextBox msktxtPhone = new MaskedTextBox();
this.Controls.Add(msktxtPhone);
msktxtPhone.Top = leftControl * 36;
msktxtPhone.Left = 116;
msktxtPhone.Size = new Size(100, 20);
msktxtPhone.Mask = "(999) 000-0000";
reConn.Close();
leftControl++;
}
private void addLabels()
{
Label lblPhoneType = new Label();
this.Controls.Add(lblPhoneType);
lblPhoneType.Top = leftControl * 12;
lblPhoneType.Left = 12;
lblPhoneType.Text = "Phone Type";
Label lblPhoneNumber = new Label();
this.Controls.Add(lblPhoneNumber);
lblPhoneNumber.Top = leftControl * 12;
lblPhoneNumber.Left = 116;
lblPhoneNumber.Text = "Phone Number";
}
private void cmboPhoneType_SelectedIndexChanged(object sender, EventArgs e)
{
cmboPhoneType.DataBindings.Clear();
}
}
}
Your Adding a "New" Masked Text Box in your Click Event but no "New" Combo Box. Then after you Declare a new cmbophonetype you will need to move, this.Controls.Add(cmboPhoneType); down, to just before you add the SelectedIndexChanged event handler. That will keep the Combobox visible but you may have some more problems separating all of the events.
I'm using WPF application and I have reminder every hour from 7 AM tell 3 PM every day, it is running every hour even if the user closed the window it still show up after one hour using DispatcherTimer.
my issue is after 3 PM it closed and it keep checking tell next day to count one hour after 7 AM to display the window but it is not.
it display directly after 7 AM. how I can fixed it, please assist
my code:
public partial class MainWindow : Window
{
public int _mCollapsed;
public Window1 WindowLink1;
public ReminderWin ReminderWin1;
public TimeSpan start = new TimeSpan(7, 0, 0);
public TimeSpan end = new TimeSpan(15, 0, 0);
public MainWindow()
{
InitializeComponent();
_mCollapsed = 1;
WindowLink1 = new Window1();
ReminderWin1 = new ReminderWin();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (now > start)
{
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Interval = new TimeSpan(1, 0, 0);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if ((now > start) && (now < end))
{
BitmapImage _image = new BitmapImage();
_image.BeginInit();
_image.UriSource = new Uri(#"test123.jpg", UriKind.RelativeOrAbsolute);
_image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
_image.CacheOption = BitmapCacheOption.OnLoad;
_image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
_image.EndInit();
ReminderWin1.image1.Source = _image;
System.Diagnostics.Debug.WriteLine(ReminderWin1.WindowState);
if (ReminderWin1.Visibility == Visibility.Collapsed || ReminderWin1.Visibility == Visibility.Hidden)
{
ReminderWin1.Visibility = System.Windows.Visibility.Visible;
}
}
else if (now > end)
{
if (ReminderWin1.Visibility == Visibility.Visible || ReminderWin1.Visibility == Visibility.Hidden)
{
ReminderWin1.Close();
}
}
}
}
questions:
1、i wanna get template elements when calling the constructor, but return null, any way?
2、i found it, get not null obj after loaded event, but i don't want this way.
snippet code:(to see my comments)
using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Input;
namespace WpfPropertyGrid_Demo
{
public class MyButton : Control
{
public static readonly DependencyProperty IsMouseDownProperty;
static MyButton()
{
IsMouseDownProperty = DependencyProperty.Register(
"IsMouseDown",
typeof(bool),
typeof(MyButton),
new FrameworkPropertyMetadata(false)
);
}
public bool IsMouseDown
{
get { return (bool)GetValue(IsMouseDownProperty); }
set { SetValue(IsMouseDownProperty, value); }
}
private BitmapImage _normalImg;
private BitmapImage _overImg;
private BitmapImage _clickImg;
private BitmapImage _disabledImg;
public MyButton()
{
_normalImg = new BitmapImage(new Uri("../../Images/ScrollerBtnBg.png", UriKind.RelativeOrAbsolute));
_overImg = new BitmapImage(new Uri("../../Images/ScrollerThumbnailBtnBg.png", UriKind.RelativeOrAbsolute));
_clickImg = new BitmapImage(new Uri("../../Images/ScrollerThumbnailBg.png", UriKind.RelativeOrAbsolute));
_disabledImg = _clickImg;
var style = new Style(typeof(MyButton));
var controlTemplate = new ControlTemplate();
var gridFactory = new FrameworkElementFactory(typeof(Grid));
var imgFacotry = new FrameworkElementFactory(typeof(Image));
imgFacotry.Name = "Image";
imgFacotry.SetValue(Image.SourceProperty, _normalImg);
gridFactory.AppendChild(imgFacotry);
controlTemplate.VisualTree = gridFactory;
var overTrigger = new Trigger();
overTrigger.Property = UIElement.IsMouseOverProperty;
overTrigger.Value = true;
overTrigger.Setters.Add(new Setter(Image.SourceProperty, _overImg, "Image"));
var disabledTriiger = new Trigger();
disabledTriiger.Property = UIElement.IsEnabledProperty;
disabledTriiger.Value = false;
disabledTriiger.Setters.Add(new Setter(Image.SourceProperty, _disabledImg, "Image"));
var downTrigger = new Trigger();
downTrigger.Property = MyButton.IsMouseDownProperty;
downTrigger.Value = true;
downTrigger.Setters.Add(new Setter(Image.SourceProperty, _clickImg, "Image"));
controlTemplate.Triggers.Add(overTrigger);
controlTemplate.Triggers.Add(disabledTriiger);
controlTemplate.Triggers.Add(downTrigger);
style.Setters.Add(new Setter(Control.TemplateProperty, controlTemplate));
this.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(MyButton_PreviewMouseDown);
this.PreviewMouseUp += new System.Windows.Input.MouseButtonEventHandler(MyButton_PreviewMouseUp);
this.MouseLeave += new System.Windows.Input.MouseEventHandler(MyButton_MouseLeave);
this.MouseEnter += new MouseEventHandler(MyButton_MouseEnter);
Style = style;
// 1、why cann't find "Image" element, it return null obj, i wanna get it immediately, any way?
// 2、return not null after loaded event
// var image = controlTemplate.FindName("Image", this );
this.Loaded += new RoutedEventHandler(MyButton_Loaded);
}
void MyButton_MouseEnter(object sender, MouseEventArgs e)
{
IsMouseDown = e.LeftButton == MouseButtonState.Pressed;
}
void MyButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
IsMouseDown = false;
}
void MyButton_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
IsMouseDown = false;
}
void MyButton_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
IsMouseDown = true;
}
void MyButton_Loaded(object sender, RoutedEventArgs e)
{
// return not null after loaded event
var image = this.Template.FindName("Image", this) as Image;
}
}
}
Try Visual Helper Class here I hope this will help
I've downloaded a spammer program from YouTube, and decompiled it. Here's the code:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace SimpleSpammer
{
public class Form1 : Form
{
private IContainer components;
private TextBox textBox1;
private Button button1;
private Button button2;
private Label label1;
private LinkLabel linkLabel1;
private Label label2;
private LinkLabel linkLabel2;
private Label label3;
private Timer timer1;
private Label label4;
private TextBox textBox2;
private Label label5;
private CheckBox checkBox1;
public Form1()
{
this.components = (IContainer) null;
base.\u002Ector();
this.InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && this.components != null)
this.components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.components = (IContainer) new Container();
this.textBox1 = new TextBox();
this.button1 = new Button();
this.button2 = new Button();
this.label1 = new Label();
this.linkLabel1 = new LinkLabel();
this.label2 = new Label();
this.linkLabel2 = new LinkLabel();
this.label3 = new Label();
this.timer1 = new Timer(this.components);
this.label4 = new Label();
this.textBox2 = new TextBox();
this.label5 = new Label();
this.checkBox1 = new CheckBox();
this.SuspendLayout();
this.textBox1.Location = new Point(13, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new Size(259, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "SPAM !";
this.textBox1.TextAlign = HorizontalAlignment.Center;
this.button1.Location = new Point(13, 39);
this.button1.Name = "button1";
this.button1.Size = new Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
// ISSUE: method pointer
this.button1.Click += new EventHandler((object) this, __methodptr(button1_Click));
this.button2.Location = new Point(197, 39);
this.button2.Name = "button2";
this.button2.Size = new Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "Stop";
this.button2.UseVisualStyleBackColor = true;
// ISSUE: method pointer
this.button2.Click += new EventHandler((object) this, __methodptr(button2_Click));
this.label1.AutoSize = true;
this.label1.Font = new Font("Arial", 8.25f, FontStyle.Italic, GraphicsUnit.Point, (byte) 0);
this.label1.ForeColor = SystemColors.WindowFrame;
this.label1.Location = new Point(7, 216);
this.label1.Name = "label1";
this.label1.Size = new Size(275, 14);
this.label1.TabIndex = 3;
this.label1.Text = "Copyright © 2010 TheDarkJoker94. This is a freeware.";
this.linkLabel1.AutoSize = true;
this.linkLabel1.Location = new Point(30, 190);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new Size(162, 13);
this.linkLabel1.TabIndex = 4;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "http://thedarkjoker94.cer33.com";
// ISSUE: method pointer
this.linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler((object) this, __methodptr(linkLabel1_LinkClicked));
this.label2.AutoSize = true;
this.label2.Location = new Point(10, 135);
this.label2.Name = "label2";
this.label2.Size = new Size(172, 13);
this.label2.TabIndex = 5;
this.label2.Text = "Check my C# Tutorials on Youtube";
this.linkLabel2.AutoSize = true;
this.linkLabel2.Location = new Point(30, 151);
this.linkLabel2.Name = "linkLabel2";
this.linkLabel2.Size = new Size(242, 13);
this.linkLabel2.TabIndex = 6;
this.linkLabel2.TabStop = true;
this.linkLabel2.Text = "http://www.youtube.com/user/TheDarkJoker094";
// ISSUE: method pointer
this.linkLabel2.LinkClicked += new LinkLabelLinkClickedEventHandler((object) this, __methodptr(linkLabel2_LinkClicked));
this.label3.AutoSize = true;
this.label3.Location = new Point(10, 171);
this.label3.Name = "label3";
this.label3.Size = new Size(91, 13);
this.label3.TabIndex = 7;
this.label3.Text = "More Software on";
this.timer1.Interval = 500;
// ISSUE: method pointer
this.timer1.Tick += new EventHandler((object) this, __methodptr(timer1_Tick));
this.label4.AutoSize = true;
this.label4.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
this.label4.Location = new Point(13, 73);
this.label4.Name = "label4";
this.label4.Size = new Size(88, 16);
this.label4.TabIndex = 8;
this.label4.Text = "Timer Interval:";
this.textBox2.Location = new Point(102, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new Size(89, 20);
this.textBox2.TabIndex = 9;
this.textBox2.Text = "100";
this.label5.AutoSize = true;
this.label5.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
this.label5.Location = new Point(197, 73);
this.label5.Name = "label5";
this.label5.Size = new Size(77, 16);
this.label5.TabIndex = 10;
this.label5.Text = "miliseconds";
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = CheckState.Checked;
this.checkBox1.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
this.checkBox1.Location = new Point(13, 103);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new Size(151, 20);
this.checkBox1.TabIndex = 12;
this.checkBox1.Text = "Send the ENTER key";
this.checkBox1.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new SizeF(6f, 13f);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(284, 239);
this.Controls.Add((Control) this.checkBox1);
this.Controls.Add((Control) this.label5);
this.Controls.Add((Control) this.textBox2);
this.Controls.Add((Control) this.label4);
this.Controls.Add((Control) this.label3);
this.Controls.Add((Control) this.linkLabel2);
this.Controls.Add((Control) this.label2);
this.Controls.Add((Control) this.linkLabel1);
this.Controls.Add((Control) this.label1);
this.Controls.Add((Control) this.button2);
this.Controls.Add((Control) this.button1);
this.Controls.Add((Control) this.textBox1);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.ShowIcon = false;
this.Text = "SimpleSpammer";
this.ResumeLayout(false);
this.PerformLayout();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://thedarkjoker94.cer33.com/");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.timer1.Interval = Convert.ToInt32(this.textBox2.Text);
this.timer1.Start();
}
catch (Exception ex)
{
int num = (int) MessageBox.Show("An Exception was thrown!\n" + ex.Message, "ExceptionThrown");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Focused)
return;
SendKeys.Send(this.textBox1.Text);
if (this.checkBox1.CheckState == CheckState.Checked)
SendKeys.Send("{ENTER}");
}
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Stop();
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://www.youtube.com/user/TheDarkJoker094");
}
}
}
Now, as you can see his code contains a delay field, a text field, and "Start"/"Stop" buttons.
So I've tried to make something similar, and here's the code:
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.Threading;
namespace Spammer
{
public partial class Form1 : Form
{
int delay, y = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
timer1.Interval = delay;
timer1.Start();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void send()
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Focused)
{
return;
}
SendKeys.Send(textBox1.Text);
}
}
}
Now, I've posted a similar question yesterday but this one is far more accurate.
In his program, I've entered some text, chose a delay of 10MS and started it. After a few seconds of sending, I've clicked the "Stop" button in his program and it stopped sending keys immediately.
Now... When I ran my program, and started sending keys (with a 10MS delay), I let it run for a few seconds, and then I pressed "Stop". Nothing happened, and "Stop" didn't even click. It just has shown a blue frame around it and I wasn't able to click it.
And of course, because of this, it didn't stop sending keys.
So, why did it work perfectly in his program but it doesn't work at all in my program?
How can I create System.Windows.Forms.WebBrowser in background STA thread? I try use some code like this:
var tr = new Thread(wbThread);
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
private void wbThread()
{
CWebBrowser browser = new CWebBrowser();
var text = browser.Navigate("http://site.com", CWebBrowser.EventType.loadCompleted).Body.InnerHtml;
}
CWebBrowser - custom class, wich delegate System.Windows.Forms.WebBrowser object Navigate method and wait until page completed loads. The problem is LoadCompleted event on System.Windows.Forms.WebBrowser object never raises. I found some solution here, but it does not work (can't find method Application.Run() on my WPF app).
public class CWebBrowser : ContentControl
{
public readonly System.Windows.Forms.WebBrowser innerWebBrowser;
private readonly AutoResetEvent loadCompletedEvent;
private readonly AutoResetEvent navigatedEvent;
public enum EventType
{
navigated, loadCompleted
}
public CWebBrowser()
{
innerWebBrowser = new System.Windows.Forms.WebBrowser();
loadCompletedEvent = new AutoResetEvent(false);
navigatedEvent = new AutoResetEvent(false);
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
host.Child = innerWebBrowser;
Content = host;
innerWebBrowser.DocumentCompleted +=new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(innerWebBrowser_DocumentCompleted);
innerWebBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(innerWebBrowser_Navigated);
}
void innerWebBrowser_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e)
{
navigatedEvent.Set();
}
void innerWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if (((sender as System.Windows.Forms.WebBrowser).ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) || innerWebBrowser.IsBusy)
return;
var doc = innerWebBrowser.Document;
loadCompletedEvent.Set();
}
public System.Windows.Forms.HtmlDocument Navigate(string url, EventType etype)
{
if (etype == EventType.loadCompleted)
loadCompletedEvent.Reset();
else if (etype == EventType.navigated)
navigatedEvent.Reset();
innerWebBrowser.Navigate(url);
if (etype == EventType.loadCompleted)
loadCompletedEvent.WaitOne();
else if (etype == EventType.navigated)
navigatedEvent.WaitOne();
System.Windows.Forms.HtmlDocument doc = null;
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(
delegate
{
doc = innerWebBrowser.Document;
}));
return doc;
}
}
Thansk for all advices and sorry for my bad english :o(
Why don't you use the default WebBrowser control like this?
public MainPage()
{
InitializeComponent();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(startNavigate);
}
void startNavigate()
{
WebBrowser wb = new WebBrowser();
wb.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);
wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(wb_Navigated);
wb.Navigate(new Uri("http://www.google.com"));
}
void wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// e.Content
}
void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
// e.Content when the document finished loading.
}
Edit: You are using old System.Windows.Forms.WebBrowser control, instead System.Windows.Controls.WebBrowser which is part of WPF.