I've got a form with a combo to pick up a customer and a button.
When a customer is selected and you move the mouse over the button a ToolTip is shown with information about this customer.
I have customized the tooltip using ToolTip_Draw.
All of these works fine.
The problem is that when I change de selected customer and then move the mouse over the button several ToolTip texts are shown. One for each customer I've previously selected.
I've tried to somehow empty de ToolTip but nothing seems to work.
private void bttCitas_MouseHover(object sender, EventArgs e)
{
string mSQL = #" SELECT one, two, three
FROM customers
WHERE id = " + comboCliente.SelectedValue + ";";
DataTable tablaTemp = retrieveData(mSQL);
string customerText = ConvertDataTableToString(tablaTemp);
System.Windows.Forms.ToolTip Emergente = new System.Windows.Forms.ToolTip();
Emergente.OwnerDraw = true;
Emergente.Draw += new DrawToolTipEventHandler(ToolTip_Draw);
Emergente.AutoPopDelay = 150000;
Emergente.InitialDelay = 500;
Emergente.ReshowDelay = 500;
Emergente.SetToolTip(this.bttCitas, customerText);
}
void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (e.Graphics)
{
Font f = new Font("Courier New", 9.0f);
e.DrawBackground();
e.DrawBorder();
SolidBrush myBrush = new SolidBrush(GLOBALToolTipFontColor);
e.Graphics.DrawString(e.ToolTipText, f, myBrush, new PointF(2, 2));
}
}
I've finally figured out what happens.
I need to declare Emergente out of the MouseOver event and then dispose it in the MouseLeave:
private ToolTip Emergente;
private void bttCitas_MouseHover(object sender, EventArgs e)
{
string mSQL = #" SELECT one, two, three
FROM customers
WHERE id = " + comboCliente.SelectedValue + ";";
DataTable tablaTemp = retrieveData(mSQL);
string customerText = ConvertDataTableToString(tablaTemp);
Emergente = new System.Windows.Forms.ToolTip();
Emergente.OwnerDraw = true;
Emergente.Draw += new DrawToolTipEventHandler(ToolTip_Draw);
Emergente.AutoPopDelay = 150000;
Emergente.InitialDelay = 500;
Emergente.ReshowDelay = 500;
Emergente.SetToolTip(this.bttCitas, customerText);
}
void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (e.Graphics)
{
Font f = new Font("Courier New", 9.0f);
e.DrawBackground();
e.DrawBorder();
SolidBrush myBrush = new SolidBrush(GLOBALToolTipFontColor);
e.Graphics.DrawString(e.ToolTipText, f, myBrush, new PointF(2, 2));
}
}
private void bttCitas_MouseLeave(object sender, EventArgs e)
{
Emergente.Dispose();
}
Related
Okay so basically I have a calendar display and when you click on anyone of the dates on it, it creates a new panel with a label displaying the date selected. I also made it so when you click on a date and a new panel is made, a label, textbox and button is created and placed onto that new panel as well.
So what I want and have been struggling with is for me to enter something into that textbox then to press the button to submit it and then for it to show on the label.
I think I know what the issue is but I've been stuck at this for hours.
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void monthCalendar1_DateSelected_1(object sender, DateRangeEventArgs e)
{
Panel newPanel = new Panel();
this.Controls.Add(newPanel);
newPanel.Visible = true;
newPanel.Size = new Size(564, 831);
newPanel.Location = new Point(0, 190);
newPanel.BringToFront();
Label textLabel = new Label();
textLabel.Size = new Size(500, 500);
textLabel.Font = new Font(textLabel.Font.Name, 25, textLabel.Font.Style);
textLabel.Location = new Point(3, 3);
Label dateLabel = new Label();
dateLabel.Size = new Size(500, 500);
dateLabel.Font = new Font(dateLabel.Font.Name, 25, dateLabel.Font.Style);
dateLabel.Location = new Point(128, 3);
Button Submitbutton = new Button();
Submitbutton.Location = new Point(100, 500);
Submitbutton.Text = "Add Food";
Submitbutton.Size = new Size(400, 100);
Submitbutton.BackColor = Color.Aqua;
Submitbutton.BringToFront();
Submitbutton.Click += Button_Click;
TextBox textBox = new TextBox();
textBox.Location = new Point(100, 650);
textBox.Size = new Size(500, 500);
textBox.BackColor = Color.Aqua;
textBox.Visible = true;
textBox.Text = "Enter food here...";
textBox.BringToFront();
Label inputtedFood = new Label();
inputtedFood.Size = new Size(500, 500);
inputtedFood.Font = new Font(inputtedFood.Font.Name, 25, inputtedFood.Font.Style);
inputtedFood.Location = new Point(100, 600);
inputtedFood.Text = "placeholder";
newPanel.Controls.Add(dateLabel);
newPanel.Controls.Add(textLabel);
newPanel.Controls.Add(Submitbutton);
newPanel.Controls.Add(textBox);
newPanel.Controls.Add(inputtedFood);
String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString();
textLabel.Text = "Date:";
dateLabel.Text = myCalendar;
}
private void Button_Click(object sender, EventArgs e)
{
inputtedFood.Text = textBox.Text;
}
private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
I tried the above code and was met with errors that are shown in the post.
Totally agree with both LarsTech and Ňɏssa Pøngjǣrdenlarp, you should be building a UserControl in place of the Panel and placing the TextBox, Button, and Label inside of that.
Your immediate question, though:
So what I want and have been struggling with is for me to enter
something into that textbox then to press the button to submit it and
then for it to show on the label.
Can be accomplished with this simple code:
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
Here's a little example showing it in action:
private void button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flp = new FlowLayoutPanel();
TextBox textBox = new TextBox();
// ... more code ...
Label inputtedFood = new Label();
inputtedFood.Text = "placeholder";
// ... more code ...
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
flp.Controls.Add(textBox);
flp.Controls.Add(Submitbutton);
flp.Controls.Add(inputtedFood);
flowLayoutPanel1.Controls.Add(flp);
}
The output:
So basically i just add a label to a panel once i get a message and im tracking the y axis of the previous message and im adding 35 to it every time a new message is detected and i use that variable as the y axis for each label, it worked fine so far but after i added a scroll bar it is acting wierd, the numbers are as i expected, but the messages are put so far apart its hard to see. This is my code:
using Networking;
using System.Net;
using System.Threading;
using System.ComponentModel;
namespace socket
{
public partial class Form1 : Form
{
string name = string.Empty;
BackgroundWorker threader = new BackgroundWorker();
bool connected;
Connector client;
int CurrentMsgY;
public Form1()
{
Form prompt = new Form();
prompt.MaximumSize = new Size(500, 200);
prompt.MinimumSize = new Size(500, 200);
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = "Enter a nickname";
Label textLabel = new Label() { Left = 50, Top = 20, Text = "Nickname: " };
TextBox inputBox = new TextBox() { Left = 50, Top = 40, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox);
prompt.ShowDialog();
name = inputBox.Text;
InitializeComponent();
connected = false;
client = new Connector(Dns.GetHostByName(Dns.GetHostName()).AddressList[1]);
threader.DoWork += RecvMsg;
threader.WorkerReportsProgress = true;
threader.ProgressChanged += OnChange;
}
void OnChange(object sender, ProgressChangedEventArgs e)
{
displayMsg((string)e.UserState);
groupBox1.VerticalScroll.Value = groupBox1.VerticalScroll.Maximum;
}
void RecvMsg(object sender, DoWorkEventArgs e)
{
while (connected)
{
string msg = client.receive(1024);
if (msg == null) return;
Console.WriteLine(msg);
threader.ReportProgress(percentProgress: 0, userState: msg);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Connecting to host");
client.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1], 1111);
client.send("{\"name\": \"" + name + "\"}");
connected = true;
CurrentMsgY = 0;
groupBox1.AutoScroll = false;
groupBox1.VerticalScroll.Enabled = true;
groupBox1.VerticalScroll.Visible = true;
groupBox1.HorizontalScroll.Visible = false;
groupBox1.HorizontalScroll.Enabled = false;
groupBox1.AutoScroll = true;
threader.RunWorkerAsync();
if (client.receive(1024) == "test")
{
Console.WriteLine("connected succesfully");
}
}
private void label1_Click(object sender, EventArgs e)
{
}
void displayMsg(string msg)
{
Label msgLabel = new Label()
{
Text = msg,
Parent = groupBox1,
Location = new System.Drawing.Point(10, CurrentMsgY),
//Size = new System.Drawing.Size(new System.Drawing.Point(0, 15)),
Font = new Font("Segoe UF", 12f, FontStyle.Bold, GraphicsUnit.Point),
AutoSize = false,
Size = new Size(groupBox1.Width, 40)
};
CurrentMsgY += 35;
label1.Text = Convert.ToString(CurrentMsgY);
msgLabel.Show();
}
private void Send_Click(object sender, EventArgs e)
{
if(textBox1.Text == "disconnect__=230") client.send("disconnect__=23");
else if(textBox1.Text != "disconnect__=230") client.send(textBox1.Text);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
client.send("disconnect__=230");
connected = false;
threader.WorkerSupportsCancellation = true;
threader.CancelAsync();
client.Disconnect();
}
}
}
So I am creating forms using Visual Studio .NET.
I am stuck when I try to add tab pages dynamically on a button click which creates a text box in a new tab.
When I create multiple tabs I want to get the data from the text boxes of each newly created tabs and add it to the database. I am having problems as these text boxes have the same name as I create them dynamically. I am new to C# and I need help.
public void add()
{
aa.Add(txt.Text);
var a = 1;
var newTabPage = new TabPage()
{
Text = "Page"
};
txt = new System.Windows.Forms.TextBox();
this.tabControl1.TabPages.Add(newTabPage);
newTabPage.Controls.Add(this.txt);
System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
newTabPage.Controls.Add(lbl);
lbl.Text = "New";
txt.Name = "Get";
lbl.AutoSize = true;
lbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lbl.Location = new System.Drawing.Point(7, 389);
lbl.Name = "label263";
lbl.Size = new System.Drawing.Size(871, 13);
lbl.TabIndex = 317;
lbl.Text = "AA";
newTabPage.Controls.Add(lbl);
}
private void txt_TextChanged(object sender, EventArgs e)
private void button1_Click_1(object sender, EventArgs e)
{
add();
string value = txt.Text;
aa.Add(value);
}
private void saveToolStripMenuItem2_Click(object sender, EventArgs e)
{
SqlCommand command;
string insert1 = #"insert into testing(test) values(#testingt)";
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
for(int i= 0;i<aa.Count;i++)
{
if (aa[i]!= "" && aa[i]!="New Box")
{
command = new SqlCommand(insert1, conn);
command.Parameters.AddWithValue(#"testingt", aa[i]);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Maybe you can set the Name property to distinguish them when you create them by creating an increment variable.
int pagecount = 1;
private void btAddtab_Click(object sender, EventArgs e)
{
TabPage tabPage = new TabPage
{
Name = "Tabpage" + pagecount.ToString(),
Text = "Tabpage" + pagecount.ToString()
};
TextBox textBox = new TextBox
{
Name = "TextBox" + pagecount.ToString()
};
tabPage.Controls.Add(textBox);
tabControl1.TabPages.Add(tabPage);
pagecount++;
}
As for how to get the value of the specified TextBox, you can refer to the following code.
private void btGetValue_Click(object sender, EventArgs e)
{
foreach (TabPage page in tabControl1.TabPages)
{
foreach (Control control in page.Controls)
{
// get the first textbox's value
if (control is TextBox && control.Name == "TextBox1")
{
Console.WriteLine(((TextBox)control).Text);
}
}
}
}
I have this code to create a button:
private void CreateButton(){
Button createButton = new Button();
// Set Button properties
createButton.Height = 40;
createButton.Width = 300;
createButton.BackColor = Color.Red;
createButton.ForeColor = Color.Blue;
createButton.Location = new Point(20, 150);
createButton.Text = "I am Button";
createButton.Name = "Button";
createButton.Font = new Font("Georgia", 16);
createButton.Click += new EventHandler(create_Button_Click);
this.Controls.Add(createButton);
}
private void create_Button_Click(object sender, EventArgs e)
{
MessageBox.Show("button is clicked");
}
and i want to save it permanently in the project. what is the best way to do this?
private void showdate(DataGridViewCellEventArgs e)
{
dateTimePicker1.Size = vacation_transDataGridView.CurrentCell.Size;
dateTimePicker1.Top = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top + vacation_transDataGridView.Top;
dateTimePicker1.Left = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left + vacation_transDataGridView.Left;
if (!(object.Equals(Convert.ToString(vacation_transDataGridView.CurrentCell.Value),"")))
{
dateTimePicker1.Value = Convert.ToDateTime(vacation_transDataGridView.CurrentCell.Value);
//dateTimePicker1.Visible = false;
}
dateTimePicker1.Visible = true;
}
This code in dgv cell_click event
There is an example of exactly this thing on MSDN.
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
Unfortunately there isn't such a Cell or Column Type you can use (as far as i know). But Microsoft provides an example on how to get a NumericUpDown (that also doesn't exist) into a DataGridView.
Maybe you can adopt this example to get your DateTimePicker into a DataGridView.
private void dgtest_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dtp = new DateTimePicker();
dgtest.Controls.Add(dtp);
dtp.Format = DateTimePickerFormat.Short;
Rectangle Rectangle = dgtest.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
dtp.Size = new Size(Rectangle.Width, Rectangle.Height);
dtp.Location = new Point(Rectangle.X, Rectangle.Y);
dtp.CloseUp += new EventHandler(dtp_CloseUp);
dtp.TextChanged += new EventHandler(dtp_OnTextChange);
dtp.Visible = true;
}
}
private void dtp_OnTextChange(object sender, EventArgs e)
{
dgtest.CurrentCell.Value = dtp.Text.ToString();
}
void dtp_CloseUp(object sender, EventArgs e)
{
dtp.Visible = false;
}