I have a tabcontrol named tabControl1, which includes tabs. Button1 adds a tab to it when clicked and adds a browser and then navigate it to google.com.
Button2 should be able to navigate the browser in the selected tab to the text inside Text1.
I could do this easily if C# had pointers like this:
WebBrowser* thebrowser;
Button1_Click(object sender, EventArgs e){
TabPage newtab = new TabPage();
WebBrowser newbrowser = new WebBrowser();
newtab.Controls.Add(newbrowser);
newbrowser.Navigate("google.com");
newtab.Click+=delegate {thebrowser = &newbrowser;};
tabControl1.TabPages.Add(newtab);
}
Button2_Click(object sender, EventArgs e){
thebrowser->Navigate(Text1.Text);
}
That if C# had pointers. Now how can I achieve this with proper C# code?
I hope you understood my question and sorry for any errors.
Thanks in advance :)
Well, i'd probably go the Dictionary<int,Webbrowser> way. Something like:
Dictionary<int, WebBrowser> browsers = new Dictionary<int, WebBrowser>();
Button1_Click(object sender, EventArgs e){
TabPage newtab = new TabPage();
WebBrowser newbrowser = new WebBrowser();
newtab.Controls.Add(newbrowser);
newbrowser.Navigate("google.com");
tabControl1.TabPages.Add(newtab);
browsers.Add(tabControl1.TabCount-1, newbrowser);
}
Button2_Click(object sender, EventArgs e){
browsers[tabControl1.SelectedIndex].Navigate(Text1.Text);
}
For what I understand you need to have more than one tab, so more than one browser open at the same time, so a lobal variable will not work.
One way of doing it would be to search the browser on the selected tab.
Button2_Click(object sender, EventArgs e)
{
foreach (Control item in page.Controls)
{
if (item is WebBrowser)
{
((WebBrowser)item).Navigate(Text1.Text);
break;
}
}
}
Related
I have the following code in my WinForms C# app:
private void button2_Click(object sender, EventArgs e)
{
var txtbox2 = new System.Windows.Forms.RichTextBox();
TabPage createdtabpage = new TabPage("I am a tab");
tabControl1.TabPages.Add(createdtabpage);
createdtabpage.Controls.Add(txtbox2);
}
And I want to access the text of txtbox2 when a separate button is clicked. I have this code:
private void button1_Click(object sender, EventArgs e)
{
//Either this:
string text = txtbox2.Text;
//or maybe this:
string text = createdtabpage.Controls[txtbox2]
}
However, this code doesn't work because the variables are not accessible to outside functions.Does anybody have a good way to access these TabPage controls from an outside function?
Thanks for any help
From the control tab, the tab is selected and then the desired control is found by name.
use this code
private void button1_Click(object sender, EventArgs e)
{
var textBox = (RichTextBox)tabControl1.SelectedTab.Controls["txtbox2"];
MessageBox.Show(textbox.Text);
}
Without a name, you have to navigate through all the controls, and for example, if you have multiple richTextBox controls, know the order in which they are located.
foreach (Control control in tabControl1.SelectedTab.Controls)
{
if(control is RichTextBox)
{
MessageBox.Show(control.Text);
}
}
Is it possible to have a click event that displays the text of buttons in a label in c#.
I want to write single code that will work for:
public button1_Click(Object sender, EventArgs e){
label1.Text = button1.Text;
}
public button2_Click(Object sender, EventArgs e){
label2.Text = button2.Text;
}
Is this what you are looking for?
On a button click, you can handle setting text as below
you can add two buttons. Have same click events for both buttons.
Take help of parameter sender to get which button is clicked.
code (sender as Button) gives you all the details of clicked button.
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = (sender as Button).Text;
}
1) Add a Button and a Label to the blank Form in a new project.
2) Double-click the Button.
This will generate the following Click event method...
private void button1_Click(object sender, EventArgs e) {
}
3) Put the following line of code in the button1_Click method body:
label1.Text = button1.Text;
4) Profit.
I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.
I need to dynamically create buttons (one for loop) and add "onClick" and "doubuleClick" events on it.
I did it like this:
Button bt = new Button();
bt.Click += bt_Click;
bt.DoubleClick += bt_DoubleClick;
private void bt_Click(object sender, EventArgs e)
{
label1.Text = this.Text;
}
private void bt_DoubleClick(object sender, EventArgs e)
{
//some code
}
First: My "bt_Click" method gets "main form" text in "label1". In debugger I see that sender is a button. What is wrong with it?
Second: My "bt_DoubleClick" event do not react at all, am I doing something wrong here?
Any help is appreciated.
You should cast sender to Button to get the bt.Text:
Button bt = new Button();
bt.Click += bt_Click;
bt.Text = "click me";
bt.Location = new Point(100,100);
this.Controls.Add(bt);
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (sender as Button).Text;
}
Buttons doesn't react to double click event. You can read it here in detail.
In response to the first question, if I understand you correctly, in this.Text, this refers to the form because the method bt_Click is a member of the Main Form class. I think you might have meant to do:
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (Button)sender.Text;
}
Second: Is this just a case of the bt_Click handler firing twice?
The easiest way to do this it is to use "datagrid".
Datagread has the great support for all events and for organization of items (image, text and so on).
I have made "save" or "open" dialog form to browse content from remote SFTP server, very easy with datagrad, but I had a problem to do it with buttons or labels.
I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}