Problem with appendText in c# - c#

I have an app that has 3 text boxes, (users name, what company they are from, and who they are visiting) A button to print, and a keyboard on the screen (monitor is touch screen). I have everything working and functioning...
BUT, the one thing that does not work is when the user points to previous character in the text box that has already been typed, the buttons that "AppendText" (keyboard) do not start typing where the user pointed but it continues typing at the end of what has been typed.
Is this because of "AppendText" or some other issue that I have in my code?
I also am trying to get the first text box (Name_Box) to be sent to form one, which then will be split into two labels (1, first name| 2, last name) right now I have it being sent to one label But I want to split it so the first name is stacked above the second name in the next form (which is printed out).
Thank you so much.
Here is my code: First Form
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
namespace SMART
{
public partial class Form1 : Form
{
private TextBox tbSelected; // Last focused TextBox
private int posCaret; // Caret position
private int selLength; // Selection length
public Form1()
{
InitializeComponent();
// We will use leave event for textboxes
Name_Box.Leave += new System.EventHandler(textBox_Leave);
Company_Box.Leave += new System.EventHandler(textBox_Leave);
Visiting_Box.Leave += new System.EventHandler(textBox_Leave);
// Set initial selection to the first textbox
Name_Box.Select();
tbSelected = Name_Box;
posCaret = 0;
selLength = 0;
}
// Leave event handler
private void textBox_Leave(object sender, EventArgs e)
{
// Remember the last focused thextbox,
// the caret position in it and the selection length
tbSelected = (TextBox)sender;
posCaret = tbSelected.SelectionStart;
selLength = tbSelected.SelectionLength;
}
// Helper method to restore selection
private void RestoreLastSelection()
{
tbSelected.Select();
posCaret = tbSelected.SelectionStart;
selLength = tbSelected.SelectionLength;
}
private void Form1_Load(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToString();
Form2 frm = new Form2(Name_Box.Text);
frm.Show();
frm.Close();
StreamWriter sw;
sw = File.AppendText ("C:\\SignIn.txt");
sw.WriteLine ("Date and Time: " + label5.Text + " | Name: " + Name_Box.Text + " | Company: " + Company_Box.Text + " | Visiting: " + Visiting_Box.Text + " |");
sw.Close ();
Name_Box.Clear();
Company_Box.Clear();
Visiting_Box.Clear();
}
private void button42_Click(object sender, EventArgs e)
{
//SPACE BAR
tbSelected.AppendText(" ");
}
private void button24_Click(object sender, EventArgs e)
{
//DELETE
string t = tbSelected.Text;
if (t.Length > 0)
{
tbSelected.Text = t.Remove(t.Length - 1);
}
}
private void button12_Click(object sender, EventArgs e)
{
tbSelected.AppendText("-");
}
private void button13_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Q");
}
private void button14_Click(object sender, EventArgs e)
{
tbSelected.AppendText("W");
}
private void button15_Click(object sender, EventArgs e)
{
tbSelected.AppendText("E");
}
private void button16_Click(object sender, EventArgs e)
{
tbSelected.AppendText("R");
}
private void button17_Click(object sender, EventArgs e)
{
tbSelected.AppendText("T");
}
private void button18_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Y");
}
private void button19_Click(object sender, EventArgs e)
{
tbSelected.AppendText("U");
}
private void button20_Click(object sender, EventArgs e)
{
tbSelected.AppendText("I");
}
private void button21_Click(object sender, EventArgs e)
{
tbSelected.AppendText("O");
}
private void button22_Click(object sender, EventArgs e)
{
tbSelected.AppendText("P");
}
private void button25_Click(object sender, EventArgs e)
{
tbSelected.AppendText("A");
}
private void button26_Click(object sender, EventArgs e)
{
tbSelected.AppendText("S");
}
private void button27_Click(object sender, EventArgs e)
{
tbSelected.AppendText("D");
}
private void button28_Click(object sender, EventArgs e)
{
tbSelected.AppendText("F");
}
private void button29_Click(object sender, EventArgs e)
{
tbSelected.AppendText("G");
}
private void button30_Click(object sender, EventArgs e)
{
tbSelected.AppendText("H");
}
private void button31_Click(object sender, EventArgs e)
{
tbSelected.AppendText("J");
}
private void button32_Click(object sender, EventArgs e)
{
tbSelected.AppendText("K");
}
private void button33_Click(object sender, EventArgs e)
{
tbSelected.AppendText("L");
}
private void button35_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Z");
}
private void button36_Click(object sender, EventArgs e)
{
tbSelected.AppendText("X");
}
private void button37_Click(object sender, EventArgs e)
{
tbSelected.AppendText("C");
}
private void button38_Click(object sender, EventArgs e)
{
tbSelected.AppendText("V");
}
private void button39_Click(object sender, EventArgs e)
{
tbSelected.AppendText("B");
}
private void button40_Click(object sender, EventArgs e)
{
tbSelected.AppendText("N");
}
private void button41_Click(object sender, EventArgs e)
{
tbSelected.AppendText("M");
}
private void button2_Click_1(object sender, EventArgs e)
{
tbSelected.AppendText("'");
}
private void button3_Click(object sender, EventArgs e)
{
tbSelected.Clear();
}
}
}
Heres is my code: Second 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.Drawing.Printing;
namespace SMART
{
public partial class Form2 : Form
{
public Form2(string strTextBox)
{
InitializeComponent();
label3.Text = strTextBox;
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
PrintDocument pd = new PrintDocument();
Margins margins = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.Margins = margins;
pd.PrintPage += new PrintPageEventHandler(PrintImage);
pd.Print();
/*
//My sad attempt at splitting the Name
var fullname = strTextBox;
var names = fullname.Split (" ");
label3.Text = names[0];
label5.Text = names[1];
*/
}
void PrintImage(object o, PrintPageEventArgs e)
{
int x = SystemInformation.WorkingArea.X;
int y = SystemInformation.WorkingArea.Y;
int width = this.Width;
int height = this.Height;
Rectangle bounds = new Rectangle(x, y, width, height);
Bitmap img = new Bitmap(width, height);
this.DrawToBitmap(img, bounds);
Point p = new Point(0, 0);
e.Graphics.DrawImage(img, p);
}
}
}

You're right in that your problem is the use of AppendText, which always appends to the end (that's what append means).
You need to Insert the character at the current carat position.
You might do better to post a message that simulates a keypress from a physical keyboard.

If you want to insert text at the user's current position, you can use SelectedText. This will replace the current selection (if the user has selected characters):
tbSelected.SelectedText = "V";
Edit: The problem is in here:
private void button24_Click(object sender, EventArgs e)
{
//DELETE
string t = tbSelected.Text;
if (t.Length > 0)
{
tbSelected.Text = t.Remove(t.Length - 1);
}
}
You set the text, which returns the cursor to the beginning of the textbox. You should set tbSelected.SelectionStart after you clear the text.

Related

Bunifu cannot access non-static property Visible

I have been trying to make a menu which has 3 tabs. Login, News and website. I created user control and named it LoginTab.cs now I put the login panels in there LoginTab.cs, NewsTab. Now I am trying to make it visible and invisible depending on what tab people click. So if someone clicks news then the LoginTab goes away and shows the News tab. My problem is that I can't used LoginTab.Visible because its showing me "Cannot access non-static property 'Visible' in static context. How would I do this?
Main.cs
using System;
using System.Windows.Forms;
using UHWID;
namespace AnixLoader
{
public partial class Main : Form
{
bool username;
bool usergroup;
String SimpleUID = UHWIDEngine.SimpleUid;
String AdvancedUID = UHWIDEngine.AdvancedUid;
public Main()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Sdads_Click(object sender, EventArgs e)
{
}
private void PictureBox2_Click(object sender, EventArgs e)
{
}
private void CloseMenu_Click(object sender, EventArgs e)
{
Environment.Exit(0); // Closes Menu
}
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
LoginTab.Visible = true;
NewsTab.Visible = false;
}
private void News_Click(object sender, EventArgs e)
{
LoginTab.Visible = false;
NewsTab.Visible = true;
}
private void BtnMenu_Click(object sender, EventArgs e)
{
if (sidemenu.Width == 60)
{
//EXPAND
// 1. Expand the panel , w = 300
// 2. Show Logo
sidemenu.Visible = false;
sidemenu.Width = 300;
PanelAnimator.ShowSync(sidemenu);
LogoAnimator.ShowSync(Anix_Logo);
}
else
{
//MINIMIZE
//Using Bunifu Animator
// 1. Hide the logo
// 2. Slide the panel, w = 60
LogoAnimator.Hide(Anix_Logo);
sidemenu.Visible = false;
sidemenu.Width = 60;
PanelAnimator.ShowSync(sidemenu);
}
}
private void MainPanel_Paint(object sender, PaintEventArgs e)
{
}
}
}
LoginTab.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnixLoader
{
public partial class LoginTab : UserControl
{
public LoginTab()
{
InitializeComponent();
}
private void LoginTab_Load(object sender, EventArgs e)
{
}
private void LoginText_Click(object sender, EventArgs e)
{
}
private void bunifuImageButton1_Click(object sender, EventArgs e)
{
}
private void Login_Click(object sender, EventArgs e)
{
}
private void Password_Click(object sender, EventArgs e)
{
}
}
}
I have resolved the problem by changing from this
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
LoginTab.Visible = true;
NewsTab.Visible = false;
}
private void News_Click(object sender, EventArgs e)
{
LoginTab.Visible = false;
NewsTab.Visible = true;
}
to this
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{ // Log in button
this.MainPanel.Controls.Clear();
this.MainPanel.Controls.Add(new LoginTab());
}
private void News_Click(object sender, EventArgs e)
{ // News button
this.MainPanel.Controls.Clear();
this.MainPanel.Controls.Add(new NewsTab());
}

c# display the value to listview if 3 button is clicked

can someone help me on my problem I'm making an ordering system for our thesis and I don't know what to do next if 3 button is clicked
Things I want to happen:
user need to pick an item first then he/she will pick a quantity for the item(I have 10 buttons for quantity: 1,2,3,4,5,6,7,8,9,0)
so I add a button0 so the user need to pick item and then click button1 then button0 to have a quantity of 10 that will display in my listview
1-9 button is working now but my problem is what if the user wants the item for a quantity of 10 or more
so here I have my code for an item button1
private void button1_Click(object sender, EventArgs e)
{
ms = 1;
}
and the quantity button
private void button1_Click(object sender, EventArgs e)
{
if(ms == 1)
{
ListViewItem item = new ListViewItem(btnms1.Text);
item.SubItems.Add("1");
item.SubItems.Add("118");
listView1.Items.Add(item);
ms = 0;
}
}
private void button2_Click(object sender, EventArgs e)
{
//working
}
private void button3_Click(object sender, EventArgs e)
{
//working
}
private void button4_Click(object sender, EventArgs e)
{
//working
}
private void button5_Click(object sender, EventArgs e)
{
//working
}
private void button6_Click(object sender, EventArgs e)
{
//working
}
private void button7_Click(object sender, EventArgs e)
{
//working
}
private void button8_Click(object sender, EventArgs e)
{
//working
}
private void button9_Click(object sender, EventArgs e)
{
//working
}
here is the 0 button
private void button0_Click(object sender, EventArgs e)
{
// magic please
}
I'm not really sure what you want to achieve but it might be something like this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void button11_Click(object sender, EventArgs e)
{
int quantity = 0;
bool quantityParse = int.TryParse(textBox1.Text, out quantity);
string productName = "Product Name"; // Sample Name
double productPrice = 300; // Sample Price
if (quantityParse)
{
ListViewItem lvi = new ListViewItem(productName); // Name
lvi.SubItems.Add(quantity.ToString()); // Quantity
lvi.SubItems.Add(productPrice.ToString()); // Price
lvi.SubItems.Add((productPrice * quantity).ToString()); // Subtotal
listView1.Items.Add(lvi);
}
}
}
Firstly, please start picking more meaningful names for variables as they help with code comprehension, especially when your code gets too complex for you or when you are sharing with others.
Secondly, how you're doing this at the moment is not the most intuitive way to go about things, always aim for the least amount of code as possible.
What I would be doing is calling the same event handler for each of the quantity buttons, but parsing the text in the buttons as a integer and adding that to a total string.
Ie:
private string QuantityText = string.Empty;
private void QtyButton_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Button theButton = (Button)sender;
string qtyText = theButton.Content.ToString();
QuantityText += qtyText;
}
}
Then call something like this method when you want to process the quantity string:
Private Void ProcessQuantity()
{
int qtyAmount = -1;
int.TryParse(QuantityText, out qtyAmount)
if (qtyAmount > -1)
{
//Do Processing here
}
else
{
throw new InvalidArgumentException("Quantity is invalid");
}
}

How can I make a reset button in my C# Windows Form?

I am trying to make a reset button my my C# Windows Form. But, when I have it clear the textboxes with a code like this:
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
then, it gives me the following error: "Input string was not in a correct format"
Here is my 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;
using System.Collections;
namespace MidTermPizzas
{
public partial class Form1 : Form
{
pizzaOrder aOrder = new pizzaOrder();
public Form1()
{
InitializeComponent();
}
private void formTitle_Click(object sender, EventArgs e)
{
}
//click File, Exit
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Enjoy your pizza!");
this.Close();
}
//click View, Summary of Orders Placed
private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced();
myForm.Show();
}
//amount of Pizzas label
private void label1_Click(object sender, EventArgs e)
{
}
//form load
private void Form1_Load(object sender, EventArgs e)
{
}
//sales tax label
private void label3_Click(object sender, EventArgs e)
{
}
//text in box to the right of "Amount of Pizzas"
private void textBox1_TextChanged(object sender, EventArgs e)
{
aOrder.numberOfPizzas = int.Parse(textBox1.Text);
}
//text in box to the right of "Amount of Cokes"
private void textBox2_TextChanged(object sender, EventArgs e)
{
aOrder.numberOfCokes = int.Parse(textBox2.Text);
}
//text in box to the right of "Sales Tax"
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
//File Tool Strip Menu
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
//amount of cokes label
private void label2_Click(object sender, EventArgs e)
{
}
//reset button
private void button1_Click_1(object sender, EventArgs e)
{
//textBox1.Clear();
//textBox2.Clear();
//textBox3.Clear();
//textBox4.Clear();
//textBox5.Clear();
//textBox6.Clear();
}
//text in box to the right of "Amount Due"
private void textBox3_TextChanged_1(object sender, EventArgs e)
{
}
//text in box to the right of "Amount Paid"
private void textBox5_TextChanged(object sender, EventArgs e)
{
aOrder.getAmountPaid = double.Parse(textBox5.Text);
}
//click Calculate Change Due button
private void calculateChangeDue_Click(object sender, EventArgs e)
{
textBox6.Text = Convert.ToString(aOrder.GetChangeDue());
}
//text in box to right of Change Due
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
//amount due label
private void label4_Click(object sender, EventArgs e)
{
}
//amount paid label
private void label5_Click(object sender, EventArgs e)
{
}
//change due label
private void label6_Click(object sender, EventArgs e)
{
}
//click Calculate Amount Due
private void calculateAmountDue_Click(object sender, EventArgs e)
{
textBox3.Text = Convert.ToString(aOrder.GetAmountDue());
}
//click Calculate Sales Tax
private void button2_Click(object sender, EventArgs e)
{
textBox4.Text = Convert.ToString(aOrder.TaxDue());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace MidTermPizzas
{
class pizzaOrder
{
public int numberOfCokes
{
get;
set;
}
public int numberOfPizzas
{
get;
set;
}
public double InputOrder()
{
const double COKE_PRICE = 1.49;
const double PIZZA_PRICE = 7.99;
double inputOrder = (numberOfCokes * COKE_PRICE) + (numberOfPizzas * PIZZA_PRICE);
return inputOrder;
}
public double TaxDue()
{
const double TAX = .073;
double taxDue = (this.InputOrder() * TAX);
return taxDue;
}
public double GetAmountDue()
{
double getAmountDue = this.InputOrder() + this.TaxDue();
return getAmountDue;
}
public double getAmountPaid
{ get; set; }
public double GetChangeDue()
{
double getChangeDue = this.getAmountPaid - this.GetAmountDue();
return getChangeDue;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e){
aOrder.numberOfPizzas = textBox1.Text == "" ? 0 : int.Parse(textBox1.Text);
}
Do the same for other textBoxes, you should use TryParse and also use 1 TextChanged event handler for all the textBoxes.
The following code uses TryParse and suppose if the parsing fails, the default value will be 0:
private void textBox1_TextChanged(object sender, EventArgs e){
int v;
if(int.TryParse(textBox1.Text, out v)){
aOrder.numberOfPizzas = v;
} else aOrder.numberOfPizzas = 0;
}
Either use TryParse as suggested by other answers or place 0 for text boxes used for numeric input in your clear method. Another approach is to use MaskedTextBox for text boxes used for numeric input.
Here you can use the manual method as well. Write the following code inside your button click function,
if (textBox1.Text != string.Empty || textBox2.Text != string.Empty || textBox3.Text != string.Empty || textBox4.Text != string.Empty || textBox5.Text != string.Empty || textBox6.Text != string.Empty)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}

How control opened tab in my web browser application?

Hi I'm new on C# and I made some simple Web Browser with tabs.
My problem is I don't know how to control selected tab. I mind if i click to back it go back on the selected tab.
Sorry for my bad English. This is my 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;
namespace HardRam
{
public partial class Form1 : Form
{
int i = 1;
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
}
private void button3_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/search?&q=" + textBox2.Text);
}
private void label2_Click(object sender, EventArgs e) { }
private void toolStripLabel1_Click(object sender, EventArgs e) { }
private void toolStripButton7_Click(object sender, EventArgs e)
{
TabPage newTp = new TabPage();
WebBrowser newWB = new WebBrowser();
newWB.Name = "Page" + tabControl1.TabPages.Count + 1;
newWB.Dock = DockStyle.Fill;
newWB.Url = new Uri(#"http://www.google.com");
newTp.Controls.Add(newWB);
tabControl1.TabPages.Add(newTp);
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/search?&q=" + textBox2.Text );
}
}
}
You make an instance of web-browser for each tab,so you have to get current tab and its web-browser control , so in.see my following example:
private void btnaddtab_Click(object sender, EventArgs e)
{
TabPage newTp = new TabPage();
WebBrowser newWB = new WebBrowser();
newWB.Name = "Page" + tabControl1.TabPages.Count + 1;
newWB.Dock = DockStyle.Fill;
newWB.Url = new Uri(#"http://www.bing.com");
newTp.Controls.Add(newWB);
tabControl1.TabPages.Add(newTp);
}
private void btnback_Click(object sender, EventArgs e)
{
(tabControl1.TabPages[tabControl1.SelectedIndex].Controls[0] as WebBrowser).GoBack();
}
private void btnforward_Click(object sender, EventArgs e)
{
(tabControl1.TabPages[tabControl1.SelectedIndex].Controls[0] as WebBrowser).GoForward();
}

Update Tab's Name in C# Web Browser

I'm working on a Web browser in Visual Studio 2010, but I can't update the tab's name to the website's name. For example, when you visit a website like CNN.Com, I want the tab to also say, "cnn.com". The project isn't using the default WebBrowser form, by the way. Please explain it in the simplest way possible since I'm new to C#(Just moved from C++ and Java) so I'm not familiar with working with Windows forms. Thanks. Any help is appreciated.
Here's an image of the problem: http://postimage.org/image/5ym4yx0pt/
....
public Form1()
{
InitializeComponent();
}
int i = 1;
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
//Load a tab when loading form
tabControl1.TabPages.Add("Tab");//problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("www.google.com");
}
private void button1_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab"); //problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i- 1;
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void toolStripButton7_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void yahooSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = yahooSearchToolStripMenuItem.Text;
}
private void youtubeSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = youtubeSearchToolStripMenuItem.Text;
}
private void googleSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = googleSearchToolStripMenuItem.Text;
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//add KeyUp event for detecting 'Enter' key
//navigate to specified URL withoud pressing the 'Go' button
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
}
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
}
}
private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab");
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i - 1;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
}
private void printPreviewDialog1_Load(object sender, EventArgs e)
{
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
//Associate PrintPreviewDialog with PrintDocument.
printPreviewDialog1.Document = printDocument1;
// Show PrintPreview Dialog
printPreviewDialog1.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
// Bring up 'Print Dialog'
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetupDialog pageSetup = new PageSetupDialog();
pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();
pageSetup.EnableMetric = false;
pageSetup.ShowDialog();
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 about = new Form2();
about.Show();
}
private void calenderToolStripMenuItem_Click(object sender, EventArgs e)
{
calenForm cal = new calenForm();
cal.Show();
}
}
}
...........
Assuming WebBrowser is the built-in WebBrowser, you can fire the OnDocumentTitleChanged event to change the tab text every time the WebBrowser document title is changed.
to do this, in the form load event, after declaring browse, start typing browse.DocumentTitleChanged += and a tooltip should come up saying 'tab to insert this code' or something along those lines. Just tab twice and Visual Studio will insert a new method for you, with a throw new NotImplementedException(); line. Delete that line and replace it with the code changing your tab's text to the browser's DocumentTitle.
If you need any more information, I suggest you check the documentation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx
Though I am confident that using events is the best solution. Events are designed to execute upon certain significant programming 'events' happening, and changing a webpage is one example of such an event. (Events are roughly C#'s equivalent of C++'s function pointers if that helps your understanding at all. Though they are more akin to a std::vector of function pointers.)
Set the HTML title tag for the page text contained within the two tags will show up in the tab that the web page is displayed in.
See the following for more about setting the title in the code behind
How to use Eval in codebehind to set Page.Title
And this link as well
http://www.asprobot.com/ASP.NET/ASPNET-Title-Tag-and-Meta-Tags

Categories

Resources