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);
}
}
Related
I'm fairly new to GUI-development in C# .NET, using WinForms, and I'm creating an application with one form, (like a main menu), and with different "sub-menus", that can all be opened in the same form. This is what I currently have:
Application with UserPanel1 open.
Application without any UserPanel selected.
I've seen some shady youtuber use UserControls for this, and that's what I'm currently working with. I'm using a TableLayoutPanel, with the UserControls dropped into it.
But I have some problems with this. First, I don't know if this is the correct way to use UserControls and if this will throw any errors.
Second of all, I don't know the exact way to use multiple UserControls in one form, because I can't drag multiple UserControls in a single cell of a TableLayoutPanel, and if I instantiate a new UserControl using some simple code, it won't display all of the custom controls I previously placed (in that UserControl). Here is that code:
private void button1_Click(object sender, EventArgs e)
{
Form1_UserControl1 form1_UserControl1 = new Form1_UserControl1();
form1_UserControl1.Show();
form1_UserControl1.BringToFront();
form1_UserControl1.Dock = DockStyle.Fill;
}
I know that this is probably a simple thing that I'm doing wrong, but I only need some simple advice.
Am I using the right control, or is there a better/easier way to achieve what I'm looking for?
Initialize all UserControls to private fields
TableLayoutPanel add all these UserControls
Use buttons to hide/show them.
My sample code:
public partial class Form1 : Form
{
private UserControl[] myUserControls = new UserControl[3];
public Form1()
{
InitializeComponent();
InitializeUserControls();
}
private void InitializeUserControls()
{
for (int i = 0; i < myUserControls.Length; ++i)
{
myUserControls[i] = new UserControl();
myUserControls[i].Hide();
myUserControls[i].BringToFront();
myUserControls[i].BackColor = Color.Blue;
myUserControls[i].Dock = DockStyle.Fill;
tableLayoutPanel2.Controls.Add(myUserControls[i]);
}
}
private void button1_Click(object sender, EventArgs e)
{
myUserControls[0].Visible = !myUserControls[0].Visible;
}
private void button2_Click(object sender, EventArgs e)
{
myUserControls[1].Visible = !myUserControls[1].Visible;
}
private void button3_Click(object sender, EventArgs e)
{
myUserControls[2].Visible = !myUserControls[2].Visible;
}
}
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;
}
}
}
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'm using Visual Studio, so I already have drag-and-dropped some pictureBoxes in there, and they have names and pictures. I just want to be able to click them and have a label say the name of the person in the picture that was clicked.
This is what I currently have (for the pictureBox only)
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Show("Captain Falcon");
}
FINAL EDIT: Everything is working now, I just followed everyone's suggestions!
Is this what you need?
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Text = "Captain Falcon";
}
Something like below will work.
private void pictureBox1_Click(object sender, EventArgs e)
{
label1.Text = "Captain Falcon";
}
Obviously change the control names (pictureBox1 and label1) to match yours.
Use
private void captainFalcon_Click(object sender, EventArgs e)
{
xxxxxx.Text ="Captain Falcon";
}
and instead of xxxxx use your label name.
Select your label on designer, look at its properties, and use its "(Name)" property.
For example if your label name is characterName, then your code will be
characterName.Text ="Captain Falcon";
I have a listbox on my website, which contains some elements.
I made the event, OnSelectedIndexChanged, so when the user presses an element, this value will be put into a textbox
protected void Page_Load(object sender, EventArgs e)
{
listbox = new Listbox();
// Add to page etc.
listbox.SelectedIndexChanged += new EventHandler(listbox_SelectedIndexChanged);
}
void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
textbox_name.Text = listbox.SelectedItem.ToString();
}
catch
{
textbox_info.Text = "Choose employee";
}
}
It works in c# windows forms, but not in web forms for some reason.
Is it possible to get it to work?
Thank you
Set autopostback property of the listbox true