I have a winform on which I have tab control and I'm adding a picture box control to the tab page. I want to load an image to this picturebox control in the tab page. I'm adding the control to tab page is as follows:
// initializing the picture box control
m_Canvas = new PhotoCanvas();
m_Canvas.BackColor = Color.White;
m_Canvas.Width = 500;
m_Canvas.Height = 400;
m_Canvas.Left = 0;
m_Canvas.Top = 0;
//adding the picture box control to tabpage
this.MainTab.TabPages[0].Controls.Add(m_Canvas);
I have tried to load the image using an OpenFileDialog:
m_Canvas.Image = new Bitmap(m_OpenFileDialog.FileName);
but it's not showing the loaded image Can anyone help me please?
From comments:
I'm using a button to load the image and the code inside it's click event is
private void Openbutton_Click_1(object sender, EventArgs e) {
m_OpenFileDialog.Title = "Select Image";
if (m_OpenFileDialog.ShowDialog() == DialogResult.OK) {
m_Canvas.Image = new Bitmap(m_OpenFileDialog.FileName);
m_Canvas.Refresh();
}
}
Try:
string path = m_OpenFileDialog.FileName;
m_Canvas.Image = System.Drawing.Bitmap.FromFile(path);
Related
I have a little problem. When I click the button, I want the ListView to move to the next object. Also, when it moves to the new object, it needs to get the address of the new object.
The program will switch to the new picture in the ListView every time the save button is clicked.
button click codes:
int next=Convert.ToInt32(listView1.FocusedItem.Index) + 1;
listView1.Items[next].Selected = true;
degisken = secilen.SubItems[sayac].Text;
listbox1 SelectedIndexChanged codes:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
secilen = listView1.FocusedItem;
degisken = secilen.SubItems[sayac].Text;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
string alan = degisken;
pictureBox1.ImageLocation = yol + "/" + alan;
}
The program will switch to the new picture in the listview every time the save button is clicked.
I'm trying to change the text of a TextBox when I click a Button: both Controls are dynamically created as run-time.
The Buttons and the TextBoxes are created every time I click on another Button.
The Name Property for each control is specified by the User, using a TextBox.
For example, the user inputs "Test1", then the Button is named btn_Test1, and the TextBox is named txt_Test1.
The Button should open a FolderBrowserDialog and after a selection has been made, the TextBox shows the path selected.
I'm using the following code:
protected void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
folderBrowserDialog.ShowDialog();
string TextName = button.Name.Replace("btn_", "txt_");
TextBox selectText = new TextBox();
selectText = this.Controls[TextName] as TextBox;
selectText.Text = folderBrowserDialog.SelectedPath;
}
however this part gives me null:
selectText = this.Controls[TextName] as TextBox;
I did check with the debugger when I create the controls, so TextName is setting the correct Name.
The Buttons and TextBoxes are inserted in a TabControls, the Tab Name is set to the value the user inputs, so the main TabControl gets 2 controls.
I'm using a hidden TabControl named "TabFolders" that will be the main reference for creating tab clones
I'm using this code:
private void CreateDynamicPathButtons(string TabName)
{
TabPage MyNewTab = new TabPage(TabName);
TabPage TabCopy1;
tabControlEmpresas.TabPages.Add(MyNewTab);
TabControl tc = new TabControl();
tc.Location = new System.Drawing.Point(6, 6);
tc.Size = TabFolders.Size;
for (int i = 0; i < TabFolders.TabCount; i++) {
TabFolders.SelectTab(i);
TabCopy1 = new TabPage(TabFolders.SelectedTab.Text);
foreach (Control c in TabFolders.SelectedTab.Controls) {
Control cNew = (Control)Activator.CreateInstance(c.GetType());
cNew.Text = c.Text;
cNew.Size = c.Size;
cNew.Location = new System.Drawing.Point(c.Location.X, c.Location.Y);
cNew.Visible = true;
if (cNew is TextBox) {
cNew.Name = "txt_" + MyNewTab.Text + "_" + TabFolders.SelectedTab.Text;
}
if (cNew is Button) {
cNew.Name = "btn_" + MyNewTab.Text + "_" + TabFolders.SelectedTab.Text;
cNew.Click += new EventHandler(button_Click);
}
TabCopy1.Controls.Add(cNew);
}
tc.TabPages.Add(TabCopy1);
}
MyNewTab.Controls.Add(tc);
}
After many attempts I did find a very simple solution.
TextBox selectText = new TextBox();
selectText = button.Parent.Controls[TextName] as TextBox;
The button parent hast all the controls.
Assuming that button is the Button control you're creating at run-time you mentioned, you're creating a TextBox control but you're not adding it to the Form.Controls collection (this.Controls.Add([Control])).
Also, you should assign a Location, using a logic that fits your current Layout, to position the newly created Controls. Otherwise, all new controls will be positioned one on top of the other. In the example, the new Control position is determined using a field (int ControlsAdded) that keeps track of the number of Controls created at run-time and add some basic layout logic.
But, if you want to keep a reference of these new Controls, you should add them to a List<Control> or some other collection that allows to select them if/when required.
int ControlsAdded = 0;
protected void button_Click(object sender, EventArgs e)
{
TextBox selectedText = new TextBox();
selectedText.Size = new Size(300, this.Font.Height);
selectedText.Location = new Point(100, ControlsAdded * selectedText.Height + 30);
ControlsAdded += 1;
this.Controls.Add(selectedText);
selectedText.BringToFront();
using (var fBD = new FolderBrowserDialog()) {
if (fBD.ShowDialog() == DialogResult.OK)
selectedText.Text = fBD.SelectedPath;
}
}
with selectText = this.Controls[TextName] as TextBox;, you are trying to find button with replaced name which is not available in this case, and hence it returns null. This is logical mistake.
also string TextName = button.Name.Replace("btn_", "txt_"); does not replace button name, it just assigns replaced string to TextName.
The proper implementation would be
protected void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
folderBrowserDialog.ShowDialog();
button.Name = button.Name.Replace("btn_", "txt_");
TextBox selectText = new TextBox();
selectText = this.Controls[button.Name] as TextBox;
selectText.Text = folderBrowserDialog.SelectedPath;
}
I have a toolstrip control on a form and i programatically add buttons to the toolstrip control using the below code
toolStrip1.Visible = true;
ToolStripItem t = new ToolStripButton();
t.Text = client.EndPoint.ToString();
t.TextImageRelation = TextImageRelation.ImageAboveText;
t.BackgroundImage = Image.FromFile("" + Application.StartupPath + "ps1_new.PNG");
t.AutoSize = false;
t.Height = 67;
t.Width = 70;
t.BackgroundImageLayout = ImageLayout.Stretch;
t.TextAlign = ContentAlignment.BottomCenter;
toolStrip1.Items.Add(t);
Now i m trying to get the index of the toolstrip button when i click on it
note i can get the text of the clicked toolstripbutton using
e.ClickedItem.Text;
There isn't an index property on the toolstripitem click but you could do something like this
private void ToolStrip1_ItemClicked(object sender, EventArgs e)
{
MessageBox.Show(e.ClickedItem.Tag)
}
Where the Tag property is something you set as the index.
I'm creating a Windows Universal App which contains a ListView filled with User-Controls. User-Controls are added to ListView dynamically during runtime, based on the elements from the database.
public void ShowFavorites()
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), (Application.Current as App).DBPath))
{
var Favorites = conn.Table<Favorites>();
lvFavorites.Items.Clear();
foreach (var fav in Favorites)
{
FavoriteItem favItem = new FavoriteItem();
favItem.Favorite = fav;
lvFavorites.Items.Add(favItem);
}
}
}
So how can i create an event that that triggers when the user-control is pressed?
When you create the control, all you need to do is simply link the control to a new event:
// Dynamically set the properties of the control
btn.Location = new Point((lbl.Width + cmb.Width + 17), 5);
btn.Size = new System.Drawing.Size(90, 23);
btn.Text = "Add to Table";
// Create the control
this.Controls.Add(btn);
// Link it to an Event
btn.Click += new EventHandler(btn_Click);
Then, when you (in this case) click on the newly added button, it will call your btn_Click method:
private void btn_Click(object sender, EventArgs e)
{
//do stuff...
}
I got it working even for items with diffrent text etc.
I gonna explain it based on a button beeing added to a panel.
You need to have a List<> in which you store the items, in this case buttons.
List<Button> BtList = new List<Button>();
and I also have a Panel in this case.
Panel PanelForButtons = new Panel();
Here is my code I hope it helps you:
void AddItemToPanel()
{
//Creating a new temporary item.
Button TempBt = new Button();
TempBt.Text = "Hello world!";
//Adding the button to our itemlist.
BtList.Add(TempBt);
//Adding the event to our button.
//Because the added item is always the last we use:
PanelForButtons.Controls.Add(BtList.Last());
BtList.Last().Click += MyButtonClicked;
}
And here is the event:
void MyButtonClicked(object sender, EventArgs e)
{
//First we need to convert our object to a button.
Button ClickedButton = sender as Button;
//And there we have our item.
//We can change the text for example:
ClickedButton.Text = "The world says: \"Hello!\"";
}
I want to recreate a simple browser with tab capability. A new tab has to be created every time the user clicks on "button_addTab" or when the selected web site tries to open a new window.
Here is my code:
Add a new tab by pressing the dedicated button:
private void button_addTab_Click(object sender, EventArgs e)
{
TabPage addedTabPage = new TabPage("tab title"); //create the new tab
tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage, //add the new webBrowser to the new tab
Dock = DockStyle.Fill
}
addedWebBrowser.Navigate("www.google.com");
Prevent a site from opening new windows (if you know every webBrowsers name) :
private void specificWebBrowser_NewWindow(object sender, CancelEventArgs e)
{
WebBrowser thisWebBrowser = (WebBrowser)sender;
e.Cancel = true;
TabPage addedTabPage = new TabPage("tab title");
tabControl_webBrowsers.TabPages.Add(addedTabPage);
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage,
Dock = DockStyle.Fill
}
addedWebBrowser.Navigate(thisWebBrowser.StatusText.ToString());
}
Now my question is:
How can I modify the second branch of code in order to match all the web browsers created with no names when the first code is called (at one time)?
Try to modify the first function to attach an event handler to newly created browser:
private void button_addTab_Click(object sender, EventArgs e)
{
TabPage addedTabPage = new TabPage("tab title"); //create the new tab
tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage, //add the new webBrowser to the new tab
Dock = DockStyle.Fill
};
addedWebBrowser.NewWindow += specificWebBrowser_NewWindow;
addedWebBrowser.Navigate("www.google.com");
}