saving picture after 5 seconds when a linklabel clicked - c#

it is little bit hard to solve.. :D first you should solve my english :D :D
i want save a webbrowser snapshot like a this placement
//first : user clicks a linklabel
//
after : program navigates my browser
//
last : 5 seconds later program will takes snapshot of webbrowser.
i have this code for this.but..
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(linkLabel1.Text);
///how can i set after 5 seconds take snapshot
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb1.png", ImageFormat.Png);
}
sorry for bad english :( i cannot explain more detailed.
edit:
i will try explain more detailed...
here is my new tab function of my web browser and i want take favorites screenshot.after when user open new tab ,user will see favorites with small photo...
so, if favorite doesnt have any pic,it will be first time.when user click to the link.it will navigates to favorite and then when document completed,it will take screenshot..thats all

What I can comprehend from your post and comments, is that you have six LinkLabels (named linkLabel1 to linkLabel6). When the user clicks any of the links, your WebKitBrowser control has to navigate to the URL (which is the text of the linklabel) and when the document is loaded, it has to take automatically a screenshot.
Now, as you would like to do all the same with the different linklabels, you should only create one event handler to handle the clicks of the different linklabels. You can just attach the _LinkClicked() event to any of the linklabels. You'll also have to define in your class some private field that will contain the LinkLabel that was last clicked on.
private LinkLabel _lastClickedLinkLabel = null;
private void linkLabelX_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// Cast the sender to a LinkLabel object as you want to know which LinkLabel
// was clicked on
var senderLinkLabel = sender as LinkLabel;
if (senderLinkLabel != null)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(senderLinkLabel.Text);
_lastClickedLinkLabel = senderLinkLabel;
}
}
Now as you want to take the screenshot after the page is loaded, you'll have to create the DocumentCompleted event. (Don't forget to attach it to your tabControl!)
private void tabControl1_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Save the screenshot
// you might want to determine the filename dynamically, otherwise the file will be overwritten
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb1.png", ImageFormat.Png);
if (_lastClickedLinkLabel != null)
{
// Do something here based upon the _lastClickedLinkLabel
}
}
Note that I don't have actual experience with that exact control, I just used a search engine to find all information.

i did this below and that works i solved my problem but,last thing i cannot change picture while my application is working..
when document completed,,.......
void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (_lastClickedLinkLabel.Text == linkLabel1.Text)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Dispose();
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb1.png", ImageFormat.Png);
// Do something here based upon the _lastClickedLinkLabel
}
if (_lastClickedLinkLabel.Text == linkLabel2.Text)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb2.png", ImageFormat.Png);
// Do something here based upon the _lastClickedLinkLabel
}
if (_lastClickedLinkLabel.Text == linkLabel3.Text)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb3.png", ImageFormat.Png);
// Do something here based upon the _lastClickedLinkLabel
}
if (_lastClickedLinkLabel.Text == linkLabel4.Text)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb4.png", ImageFormat.Png);
// Do something here based upon the _lastClickedLinkLabel
}
if (_lastClickedLinkLabel.Text == linkLabel5.Text)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb5.png", ImageFormat.Png);
// Do something here based upon the _lastClickedLinkLabel
}
if (_lastClickedLinkLabel.Text == linkLabel6.Text)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).PageScreenshot.Save("thumb6.png", ImageFormat.Png);
// Do something here based upon the _lastClickedLinkLabel
}
}
and when links clicked,,.....
private LinkLabel _lastClickedLinkLabel = null;
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var senderLinkLabel = sender as LinkLabel;
if (senderLinkLabel != null)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(senderLinkLabel.Text);
_lastClickedLinkLabel = senderLinkLabel;
}
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var senderLinkLabel = sender as LinkLabel;
if (senderLinkLabel != null)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(senderLinkLabel.Text);
_lastClickedLinkLabel = senderLinkLabel;
}
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var senderLinkLabel = sender as LinkLabel;
if (senderLinkLabel != null)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(senderLinkLabel.Text);
_lastClickedLinkLabel = senderLinkLabel;
}
}
private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var senderLinkLabel = sender as LinkLabel;
if (senderLinkLabel != null)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(senderLinkLabel.Text);
_lastClickedLinkLabel = senderLinkLabel;
}
}
private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var senderLinkLabel = sender as LinkLabel;
if (senderLinkLabel != null)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(senderLinkLabel.Text);
_lastClickedLinkLabel = senderLinkLabel;
}
}
private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var senderLinkLabel = sender as LinkLabel;
if (senderLinkLabel != null)
{
((WebKitBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(senderLinkLabel.Text);
_lastClickedLinkLabel = senderLinkLabel;
}
}

Related

C# combobox if statement + continue button incorporation

This code doesn't work. I don't know what to fix.
public sealed partial class Home : Page
{
public Home()
{
this.InitializeComponent();
ComboBox1.Items.Add("Hindiiiii");
}
string selection = null;
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=1)
{
selection = ComboBox1.SelectedItem.ToString();
}
}
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
{
if (selection == "Hindiiiii")
this.Frame.Navigate(typeof(MainPage));
else if (selection == "English")
this.Frame.Navigate(typeof(Home));
}
}
When a user selects Hindiiiii on the main screen and clicks continues he is not redirected to the next page (MainPage).
Let's say your main page looks like this:
You can store the selection in a variable:
string selection = null;
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=-1)
{
selection = ComboBox1.SelectedItem.ToString();
}
}
Then in your click event you can pass parameters between your pages:
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
this.Frame.Navigate(typeof(SomePage), selection); //send the contents of the variable to another page
}
And let's say you had another page with a TextBox and a TextBlock:
In your other pages' OnNavigatedTo event, you can retrieve the parameters so you don't have to create a page for every selected language:
string selection = null;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
selection = e.Parameter.ToString();
languageTextBlock.Text = selection; //the textblox is now the selected language
//decide what the contents are based on the selection
if (selection == "English")
translation.Text = "Something in English";
else if (selection == "Hindi")
translation.Text = "Something in Hindi";
else if (selection == "German")
translation.Text = "Something in German";
//etc
}
When you go to the next page, this allows you to create your page based on the selected item. This image demonstrates this:
Alternatively, you can solve your problem by creating a page for every possible language:
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
{
if(selection == "English")
this.Frame.Navigate(typeof(EnglishPage));
else if(selection == "Hindi")
this.Frame.Navigate(typeof(HindiPage));
//and so on
}
}
I prefer to do it this way because it's a lot simpler.
Edit: I see Items in the property box but I'm not aware of how to use it to add combo box items. This is the way that I usually see it done:
Of course you'll need to replace MainPage with your page (if it's not already named MainPage).
Another edit:
If you added the items via the properties panel, you have to access the Content. Use this instead, if you want:
string selection = null;
private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ComboBox1.SelectedIndex != -1)
{
//selection = ComboBox1.SelectedItem.ToString();
selection = (ComboBox1.SelectedItem as ComboBoxItem).Content.ToString();
}
}

Button Background Image Check?

Pls i have a problem with trying to check the background Image of a button. I want to delete the button if its image matches an Image in the Resource Folder. I have Tried
private void DeleteCard(Button btn)
{
if (btn.BackgroundImage.Equals( FunWaysInc.Properties.Resources.Card_1))
{
playersCards.Remove(Properties.Resources.Card_1);
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
private void frmQuickSpark_Load(object sender, EventArgs e)
{
Button tstbtn = new Button();
tstbtn.BackgroundImage = Properties.Resources.Card_1;
DeleteCard(tstbtn);
}
but the message box displayed is the one that displays "NO"..
Pls what is happening????
when adding the button
button.Tag = "ToDelete";
then later
foreach (Button b in this.Controls.OfType<Button>())
{
if(b.Tag == "ToDelete")
{
//delete
}
}
Here what you have to do. You need to enumerate all your images and store pointer in Tag property of the button.
private Dictionary<string, Image> _table = new Dictionary<string, Image>();
private void frmQuickSpark_Load(object sender, EventArgs e)
{
_table.Add("Image1", Properties.Resources.Card_1);
_table.Add("Image2", Properties.Resources.Card_2);
Button btn = new Button();
SetButtonImage(btn);
DeleteCard(btn);
}
private void SetButtonImage(Button button)
{
button.BackgroundImage = _table["Image1"];
button.BackgroundImage.Tag = "Image1";
}
private void DeleteCard(Button btn)
{
if (btn.BackgroundImage.Tag == "Image1")
{
playersCards.Remove(btn); // not sure what your logic of removal
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
I already found an answer to my question.. I just modified my code
private void DeleteCard(Image img)
{
playersCards.Add(Properties.Resources.Card_1);
if (img == playersCards[0])
{
playersCards.Remove(Properties.Resources.Card_1);
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
private void frmQuickSpark_Load(object sender, EventArgs e)
{
Button tstbtn = new Button();
tstbtn.BackgroundImage = Properties.Resources.Card_1;
Image img = tstbtn.BackgroundImage;
DeleteCard(img);
}
It works perfectly.

How can I refactor this C# code below?

I have 12 buttons in my Form1, and each button has a textbox next to it. The button event calls a method called dialogueOpen which handles getting the an object from form2 and placing a string value in a textbox.
How can I place the value returned in a textbox depending on what button the user clicked on? So if it is button1 a user clicked on, then the text returned should be placed in textbox1 and if it is button2 the user clicked on then the text returned should be placed in textbox2. The point is avoid using a string name to check as the buttons can all be called "browse".
Right now my code below does that but it is quite repetitive is there is a better of doing this?
private void dailogueOpen(String btnName)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
switch (btnName)
{
case "btn1":
textBox1.Text = form2result.getValue();
break;
case "btn2":
textBox2.Text = form2result.getValue();
break;
case "btn3":
textBox3.Text = form2result.getValue();
break;
case "btn4":
textBox4.Text = form2result.getValue();
break;
case "btn5":
textBox5.Text = form2result.getValue();
break;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
String name = "btn1";
dailogueOpen(name);
}
private void button2_Click(object sender, EventArgs e)
{
String name = "btn2";
dailogueOpen(name);
}
private void button3_Click(object sender, EventArgs e)
{
String name = "btn3";
dailogueOpen(name);
}
private void button4_Click(object sender, EventArgs e)
{
String name = "btn4";
dailogueOpen(name);
}
private void button5_Click(object sender, EventArgs e)
{
String name = "btn5";
dailogueOpen(name);
}
EDIT: I just noticed your event handlers. More refactoring ensues:
Yes, there is. You need to somehow associate textboxes to buttons. For example, create a dictionary like so:
Dictionary<Button, TextBox> _dict;
_dict[button1] = textBox1;
_dict[button2] = textBox2;
...
Use one event handler for all events:
private void button_click(object sender, EventArgs e)
{
dialogeOpen((Button)sender);
}
Change dialogueOpen to accept a Button instead of a string and
_dict[btn].Text = form2Result.getValue();
replace your eventhandlers to
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
String name = button.Text;// Tag, name etc
dailogueOpen(name);
}
1 You use the same delegate on all button
Nota (Thank's to Marty) : When You're in the Form Designer, select all buttons, and then assing then "Generic_Click" for all of them, or you can use code below.
this.btn1.Click += new System.EventHandler(Generic_Click); //the same delegate
this.btn2.Click += new System.EventHandler(Generic_Click);
this.btn3.Click += new System.EventHandler(Generic_Click);
....
private void Generic_Click(object sender, EventArgs e)
{
var control = (Button)sender;
if( control.Name == "btn1")
{
....
}
else if( control.Name == "btn2")
{
....
}
else if( control.Name == "btn3")
{
....
}
}
I would first use just one event handler for the buttons, it would look like this:
protected void ButtonClick(object sender, EventArgs e)
{
Button clickedButton = (Button) sender;
string selectedId = clickedButton.ID;
string[] idParameters = selectedId.Split('_');
string textBoxId = "textbox" + idParameters[1];
dailogueOpen(textBoxId);
}
What I did here is use a pattern for the names of the textboxes, so for instance if you have buttons with ids like: button_1 ,button_2, ..., button_n, you can infer what the corresponding textbox is.
If you click button_1, by spliting its id you'll know that its corresponding textbox is the one whose id is textbox1.
Then the dialogueOpen function would look like this:
private void dailogueOpen(string textBoxId)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
TextBox textBox = (TextBox)this.Form.FindControl("MainContent").FindControl(textBoxId);
textBox.Text = resulOfForm2.getValue();
}
}
Where MainContent is the id of container where the textboxes are.
All in all:
I would use a pattern for button and texboxes id.
According to the button being clicked I infer its corresponding texbox id.
Then find the texbox and update its value.
You can have dictionary and one event method on all button clicks
Dictionary<Button, TextBox> dx = new Dictionary<Button, TextBox>;
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
dx[button].Text = form2result.getValue();
}
and constructor like this:
public ClassName()
{
dx.Add(button1, textBox1);
dx.Add(button2, textBox2);
dx.Add(button3, textBox3);
}
I think the first thing you can do is improve readability by removing the need for the switch statement:
private void dailogueOpen(TextBox textBox)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
textBox.Text = form2result.getValue();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
dailogueOpen(textBox1);
}
private void button2_Click(object sender, EventArgs e)
{
dailogueOpen(textBox2);
}
private void button3_Click(object sender, EventArgs e)
{
dailogueOpen(textBox3);
}
private void button4_Click(object sender, EventArgs e)
{
dailogueOpen(textBox4);
}
private void button5_Click(object sender, EventArgs e)
{
dailogueOpen(textBox5);
}
This then gives you a reasonable method signature to introduce the dictionary (suggested by two other people) to map Button to TextBox, which would in turn allow you to use a single event handler (suggested by two other people) for all buttons.
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null) return;
String name = button.Text;// Tag, name etc
dailogueOpen(name);
}
private void dailogueOpen(String btnName)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
SetTxt(btnName,form2result.getValue());
}
}
}
private void SetTxt(string btnName, string value)
{
int lenght = "Button".Length;
string index = btnName.Substring(lenght); //remove Button
TextBox t = (TextBox)this.Controls.Find("textBox" + index, true)[0];
if (t != null)
t.Text = value;
}

WP7 listbox binds to second observable collection autoselection error

I have a list box which is bound to a Service. It loads fine and allows me to select an item and moves through to second page.
The Second page loads a new observable collection from the same service and uses the selected Item from previous page to load a new listbox with a data filter.
This second listbox allows for a further selection to load a details page based on this selection. The error I have is that the second listbox loads and automatically selects the first item in the list. Upon pushing the back button it loads the second listbox with no selection made.
Can someone please tell me how to stop the automatic selection in my second listbox? Code details below.
First Listbox code including sender
void ServiceReference1Client_GetMensleaderListCompleted(object sender, GetMensLeaderListCompletedEventArgs e)
{
if (e.Error != null) { MensHeading.Text = "Connect to Data"; return; }
ObservableCollection<Mens_Leaders> mensLeaders = e.Result;
this.mensRankings.ItemsSource = mensLeaders;
}
private void mensRankings_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (mensRankings.SelectedItem != null)
{
Mens_Leaders SelectedPlayer = mensRankings.SelectedItem as Mens_Leaders;
_SelectedMensPlayer = (sender as ListBox).SelectedItem;
NavigationService.Navigate(new Uri("/Pages/PlayerProfile.xaml", UriKind.Relative));
FrameworkElement root2 = Application.Current.RootVisual as FrameworkElement;
root2.DataContext = _SelectedMensPlayer;
}
}
Page where second list loads
private void LoadPlayerList()
{
Service1Client ServiceReference1Client = new Service1Client();
ServiceReference1Client.GetTournamentListCompleted += new EventHandler<GetTournamentListCompletedEventArgs>(ServiceReference1Client_GetTournamentListCompleted);
ServiceReference1Client.GetTournamentListAsync();
FrameworkElement root2 = Application.Current.RootVisual as FrameworkElement;
var currentPlayer = root2.DataContext as ATP_Tennis_App.TennisService.Mens_Leaders;
_SelectedPlayer = currentPlayer;
_selectedPlayerTournamentsWon = currentPlayer.Name;
}
void ServiceReference1Client_GetTournamentListCompleted(object sender, GetTournamentListCompletedEventArgs e)
{
if (e.Error != null) { return; }
ObservableCollection<Tournaments> tournamentList = e.Result;
viewSource = new CollectionViewSource();
viewSource.Filter += TournamentWin_Filter;
viewSource.Source = tournamentList;
this.listBox1.ItemsSource = viewSource.View;
}
void TournamentWin_Filter(object sender, FilterEventArgs e)
{
if (e.Item != null)
e.Accepted = ((Tournaments)e.Item).Prev_Male_Winner.Contains(_selectedPlayerTournamentsWon);
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listBox1.SelectedItem != null)
{
Tournaments selectedTournament1 = listBox1.SelectedItem as Tournaments;
_SelectedItemTournament = (sender as ListBox).SelectedItem;
NavigationService.Navigate(new Uri("/Pages/TournamentDetailsPanorama.xaml", UriKind.Relative));
FrameworkElement root3 = Application.Current.RootVisual as FrameworkElement;
root3.DataContext = _SelectedItemTournament;
}
else
{
return;
}
}
On the listbox, set the IsSynchroziedWithCurrentItem to False. This fixed the issue.
I know this is a dirty solution, but you can set listBox1.SelectedIndex = -1

c# . if (image == Properties.Resources.image)

I want to do an if stament on an image
if (SortName.Image == Properties.Resources.RadioEmpty)
{
SortName.Image = Properties.Resources.Radio;
}
else
{
SortName.Image = Properties.Resources.RadioEmpty;
}
but its on working any idea what I'm doing wrong ?
o.k additional information
1.
//SortName = A picture box
//Properties.Resources.RadioEmpty = Resources\RadioEmpty.png
//Properties.Resources.Radio = Resources\Radio.png
2.
Nope no errors
3.I wanted to use a custom image for a radio button. A have a picture box with the above code on click. RadioEmpty is the default so I check to see if the picturebox's image is the same as image form the Resources folder is so do code.
i advise you use tag for this issue see this code
private void Form1_Load(object sender, EventArgs e)
{
//in form load the radio is checked or unckecked
//here my radio is unchecked at load
pictureBox1.Image = WindowsFormsApplication5.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//after pictiurebox clicked change the image and tag too
if (pictureBox1.Tag.ToString() == "Checked")
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
else
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Delete;
pictureBox1.Tag = "Checked";
}
}
compare the names. Something like this (unverified)
if (SortName.Image.Name.Equals(Properties.Resources.RadioEmpty.Name))
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bm1;
Bitmap bm2;
private void button1_Click(object sender, EventArgs e)
{
bm1 = new Bitmap(Properties.Resources.firegirl1);
bm2 = new Bitmap(Properties.Resources.Zemli2);
pictureBox1.Image = bm1;
pictureBox2.Image = bm2;
if (pictureBox1.Image==pictureBox2.Image)
{
MessageBox.Show("Some");
}
else
{
MessageBox.Show("Differ");
}
}
}

Categories

Resources